Search code examples
dockerdockerfile

What is the difference between `AS base` and `AS build` in the Dockerfile?


I want to know the difference between

FROM mcr.microsoft.com/dotnet/core/aspnet:2.1-stretch-slim AS base

and

FROM mcr.microsoft.com/dotnet/core/sdk:2.1-stretch AS build

Could you explain the difference between AS base and AS build?

Here is a default Dockerfile generated by Visual Studio:

FROM mcr.microsoft.com/dotnet/core/aspnet:2.1-stretch-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/core/sdk:2.1-stretch 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"]

Solution

  • There is no functional difference. it's a name for the build stage.

    A stage is a part of the dockerfile that starts at the FROM keyword and ends before the next FROM keyword.

    Image built in a stage of the dockerfile will be later accessible using that stage's name.

    For example

    FROM image AS foo
    ...
    ...
    
    FROM foo AS bar
    RUN touch /example
    ...
    ...
    
    FROM foo
    COPY --from=bar /example /var/example
    

    Optionally a name can be given to a new build stage by adding AS name to the FROM instruction. The name can be used in subsequent FROM and COPY --from=<name|index> instructions to refer to the image built in this stage.

    It was added in 17.05 for multistage builds, more on that: https://docs.docker.com/develop/develop-images/multistage-build/