在VS2019中創(chuàng)建的項(xiàng)目如何生成docker鏡像:
1.在項(xiàng)目中添加docker支持,會(huì)自動(dòng)生成一個(gè)Dockerfile文件
一個(gè)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"]
從中一看就頭大,發(fā)現(xiàn)里面包含了很多步驟。按照這個(gè)默認(rèn)文件去docker中生成鏡像多半會(huì)失敗,太復(fù)雜了。實(shí)際上根本不需要再來一遍編譯發(fā)布等操作。編譯發(fā)布在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發(fā)布的項(xiàng)目文件放在一起,然后將這些文件全部拷貝至docker宿主機(jī)上(一般為linux系統(tǒng))。
最后在宿主機(jī)上找到相應(yīng)的文件路徑,在含有Dockerfile文件的文件夾下執(zhí)行:docker build -t webapi .
等待執(zhí)行完畢,查看一下docker鏡像就能發(fā)現(xiàn)"webapi"鏡像了,然后用鏡像生成容器運(yùn)行,vs2019項(xiàng)目就算是部署到docker容器上啦。