C# .NET 5.0 部署到Docker
VS中存在Docker部署工具
1.創建項目時,勾選Docker支持,如果沒有選擇,創建項目后也可支持,如下所示
2.選擇項目==》右鍵添加==》Docker支持
<Project Sdk="Microsoft.NET.Sdk.Web"> <PropertyGroup> <TargetFramework>net5.0</TargetFramework> <UserSecretsId>eb830265-ac01-4476-86d5-58fcac7d3973</UserSecretsId> <DockerDefaultTargetOS>Linux</DockerDefaultTargetOS> <DockerfileContext>.</DockerfileContext> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.11.1" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" /> </ItemGroup> <ItemGroup> <ProjectReference Include="..\Models\Models.csproj" /> </ItemGroup> </Project>
注: <DockerDefaultTargetOS>Linux/Windows</DockerDefaultTargetOS>
當添加或啟用 Docker 支持時,Visual Studio 會向項目添加以下各項:
- Dockerfile** 文件
- .dockerignore 文件
- 對 Microsoft.VisualStudio.Azure.Containers.Tools.Targets 的 NuGet 包引用
同時,launchSettings.json 文件新增Docker,可在Docker中調試
{ "iisSettings": { "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { "applicationUrl": "http://localhost:48098", "sslPort": 44372 } }, "$schema": "http://json.schemastore.org/launchsettings.json", "profiles": { "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, "launchUrl": "swagger", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } }, "Asp.Netcore.Demos": { "commandName": "Project", "launchBrowser": true, "launchUrl": "swagger", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" }, "dotnetRunMessages": "true", "applicationUrl": "https://localhost:5001;http://localhost:5000" }, "Docker": { "commandName": "Docker", "launchBrowser": true, "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/swagger", "publishAllPorts": true, "useSSL": true } } }
Dockerfile文件解釋:
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging. #Depending on the operating system of the host machines(s) that will build or run the containers, the image specified in the FROM statement may need to be changed. #For more information, please see https://aka.ms/containercompat #--------------------多階段構建 4個階段,提升效率----------------------------- #基礎階段 FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base WORKDIR /app EXPOSE 80 EXPOSE 443 #構建階段,包含所有構建工具 FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build WORKDIR /src COPY ["Asp.Netcore.Demos.csproj", "."] COPY ["../Models/Models.csproj", "../Models/"] RUN dotnet restore "./Asp.Netcore.Demos.csproj" COPY . . WORKDIR "/src/." RUN dotnet build "Asp.Netcore.Demos.csproj" -c Release -o /app/build #發布階段 FROM build AS publish RUN dotnet publish "Asp.Netcore.Demos.csproj" -c Release -o /app/publish #最終階段 FROM base AS final WORKDIR /app COPY --from=publish /app/publish . ENTRYPOINT ["dotnet", "Asp.Netcore.Demos.dll"]
有了Dockerfile就可以創建鏡像:
注:需要下載並安裝 Docker Desktop應用,略
安裝過程遇到問題:
發生原因(參考:https://blog.csdn.net/u013374164/article/details/115841910):
如果下載的是官網中最新的版本的docker的話,會出現該問題,原因是最新版本docker需要安裝在window10系統環境下。
如果是win7安裝的話,可使用:http://mirrors.aliyun.com/docker-toolbox/windows/docker-toolbox/ 進行安裝,親測有效
解決步驟:
第一步:下載 DockerToolbox-18.03.0-ce.exe、VirtualBox-6.1.30-148432-Win.exe
第二步:安裝DockerToolbox-18.03.0-ce.exe
第三步:C:\Users\當前用戶\AppData\Roaming\Kitematic\Local Storage 刪除文件夾內容
第四步:安裝路徑下運行:D:\Program Files\Docker Toolbox\kitematic Kitematic.exe
第五步:卸載 安裝路徑下:D:\Program Files\Docker Toolbox\installers\virtualbox virtualbox.msi
第六步:安裝VirtualBox-6.1.30-148432-Win.exe
第七步:啟動Docker Quickstart Terminal,如下所示,表示成功
注:
其中VirtualBox提供了linux虛擬機的運行環境;
Docker Quickstart Terminal用於快速介入linux虛擬機,提供命令行交互;
Kitematic是docker GUI很少用到;
選擇Dockerfile文件==》右鍵==》生成Docker映像,如下所示:
1.查看docker images 鏡像
cmd==》docker images