VS2019 项目部署生成Docker镜像

在VS2019中创建的项目如何生成docker镜像:

1.在项目中添加docker支持,会自动生成一个Dockerfile文件
一个demo中的示例文件:

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/core/aspnet:3.0-buster-slim AS base
WORKDIR /app
EXPOSE 80

FROM mcr.microsoft.com/dotnet/core/sdk:3.0-buster AS build
WORKDIR /src
COPY ["Samples/ApiGateway/ApiGateway.csproj", "Samples/ApiGateway/"]
COPY ["Jimu.Modules/Jimu.Client.Discovery.Consul/Jimu.Client.Discovery.Consul.csproj", "Jimu.Modules/Jimu.Client.Discovery.Consul/"]
COPY ["Jimu.Client/Jimu.Client.csproj", "Jimu.Client/"]
COPY ["Jimu.Core/Jimu.Core.csproj", "Jimu.Core/"]
COPY ["Jimu.Modules/Jimu.Client.ApiGateway.Swagger/Jimu.Client.ApiGateway.Swagger.csproj", "Jimu.Modules/Jimu.Client.ApiGateway.Swagger/"]
COPY ["Jimu.Modules/Jimu.Client.Diagnostic.Skywalking/Jimu.Client.Diagnostic.Skywalking.csproj", "Jimu.Modules/Jimu.Client.Diagnostic.Skywalking/"]
RUN dotnet restore "Samples/ApiGateway/ApiGateway.csproj"
COPY . .
WORKDIR "/src/Samples/ApiGateway"
RUN dotnet build "ApiGateway.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "ApiGateway.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "ApiGateway.dll"]

从中一看就头大,发现里面包含了很多步骤。按照这个默认文件去docker中生成镜像多半会失败,太复杂了。实际上根本不需要再来一遍编译发布等操作。编译发布在vs2019中做不是更香么。所以需要改造一下。
2.改造如下:

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/core/aspnet:3.0-buster-slim 
FROM mcr.microsoft.com/dotnet/core/sdk:3.0-buster 
EXPOSE 80
WORKDIR /app
COPY .  /app
ENTRYPOINT ["dotnet", "ApiGateway.dll"]

将改造后的Dockerfile文件与VS2019发布的项目文件放在一起,然后将这些文件全部拷贝至docker宿主机上(一般为linux系统)。
最后在宿主机上找到相应的文件路径,在含有Dockerfile文件的文件夹下执行:docker build -t webapi .
等待执行完毕,查看一下docker镜像就能发现"webapi"镜像了,然后用镜像生成容器运行,vs2019项目就算是部署到docker容器上啦。

你可能感兴趣的:(VS2019 项目部署生成Docker镜像)