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