Search code examples
dockerasp.net-coredockerfiledocker-multi-stage-build

Why does the ASP.NET Core Multi-Stage Dockerfile use 4 Stages


This is the default multi-stage Dockerfile when you click on 'Add Docker Support' in Visual Studio on an ASP.NET Core site.

FROM microsoft/aspnetcore:2.0 AS base
WORKDIR /app
EXPOSE 80

FROM microsoft/aspnetcore-build:2.0 AS build
WORKDIR /src
COPY WebApplication1.sln ./
COPY WebApplication1/WebApplication1.csproj WebApplication1/
RUN dotnet restore
COPY . .
WORKDIR /src/WebApplication1
RUN dotnet build -c Release -o /app

FROM build AS publish
RUN dotnet publish -c Release -o /app

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

Why have they chosen to use four stages, starting and finishing with the base stage. Also, why create a publish stage using the same build base image. Why does the Dockerfile not look like this with three stages:

FROM microsoft/aspnetcore-build:2.0 AS build
WORKDIR /src
COPY WebApplication1.sln ./
COPY WebApplication1/WebApplication1.csproj WebApplication1/
RUN dotnet restore
COPY . .
WORKDIR /src/WebApplication1
RUN dotnet build -c Release -o /app

FROM build AS publish
RUN dotnet publish -c Release -o /app

FROM microsoft/aspnetcore:2.0 AS final
WORKDIR /app
EXPOSE 80
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "WebApplication1.dll"]

Is there some advantage to this that I am missing?


Solution

  • The file effectively equivalent to below

    FROM microsoft/aspnetcore-build:2.0 AS build
    WORKDIR /src
    COPY WebApplication1.sln ./
    COPY WebApplication1/WebApplication1.csproj WebApplication1/
    RUN dotnet restore
    COPY . .
    WORKDIR /src/WebApplication1
    RUN dotnet build -c Release -o /app
    RUN dotnet publish -c Release -o /app
    
    FROM microsoft/aspnetcore:2.0 AS base
    EXPOSE 80
    WORKDIR /app
    COPY --from=build /app .
    ENTRYPOINT ["dotnet", "WebApplication1.dll"]
    

    Now the reason they may have chosen 4 build stage might be any of the two

    • Presentational
    • Future changes

    So it may be that it depicts a

    base -> build -> publish -> deploy the build
    

    The size with 2 build stage would also the same as this one. So there is no obvious difference between the 2 stage and the 4 stage. It becomes a matter preference, representation and all. Nothing technologically different