为什么在同时发布时也将构建和发布分为两个步骤?

时间:2018-11-16 06:25:55

标签: c# .net asp.net-core visual-studio-2017 dockerfile

很抱歉,如果这是一个愚蠢的问题,但是当发布也生成时,为什么dockerfile包含生成和发布的步骤?

在我的Web应用程序中创建了以下Dockerfile:

FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80

FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY ["WebApplication1/WebApplication1.csproj", "WebApplication1/"]
RUN dotnet restore "WebApplication1/WebApplication1.csproj"
COPY . .
WORKDIR "/src/WebApplication1"
RUN dotnet build "WebApplication1.csproj" -c Release -o /app

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

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

2 个答案:

答案 0 :(得分:0)

在我们在代码中添加一些参考/服务/实体并且需要部分调试的代码进行调试时,才使用构建。在调试模式下,每次我们需要在代码100%完成时进行build编译时,都无需构建。通过发布直接构建。

答案 1 :(得分:-1)

根据.NET Microservices: Architecture for Containerized .NET Applications (Microsoft EBook)这本书,第一条构建指令是多余的,因为发布指令也可以构建,并且紧随第一条构建指令之后。页面94(86),第10行。

这是本书的简短摘录:

1 FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
2 WORKDIR /app
3 EXPOSE 80
4
5 FROM microsoft/dotnet:2.1-sdk AS build
6 WORKDIR /src
7 COPY src/Services/Catalog/Catalog.API/Catalog.API.csproj …
8 COPY src/BuildingBlocks/HealthChecks/src/Microsoft.AspNetCore.HealthChecks …
9 COPY src/BuildingBlocks/HealthChecks/src/Microsoft.Extensions.HealthChecks …
10 COPY src/BuildingBlocks/EventBus/IntegrationEventLogEF/ …
11 COPY src/BuildingBlocks/EventBus/EventBus/EventBus.csproj …
12 COPY src/BuildingBlocks/EventBus/EventBusRabbitMQ/EventBusRabbitMQ.csproj …
13 COPY src/BuildingBlocks/EventBus/EventBusServiceBus/EventBusServiceBus.csproj …
14 COPY src/BuildingBlocks/WebHostCustomization/WebHost.Customization …
15 COPY src/BuildingBlocks/HealthChecks/src/Microsoft.Extensions …
16 COPY src/BuildingBlocks/HealthChecks/src/Microsoft.Extensions …
17 RUN dotnet restore src/Services/Catalog/Catalog.API/Catalog.API.csproj
18 COPY . .
19 WORKDIR /src/src/Services/Catalog/Catalog.API
20 RUN dotnet build Catalog.API.csproj -c Release -0 /app
21
22 FROM build AS publish
23 RUN dotnet publish Catalog.API.csproj -c Release -0 /app
24
25 FROM base AS final
26 WORKDIR /app
27 COPY --from=publish /app
28 ENTRYPOINT ["dotnet", "Catalog.API.dll"]
  

对于最终优化,恰好发生了第20行是多余的,   因为第23行也构建了应用程序,实质上   在第20行之后,因此还有另一个耗时的命令。