4
votes

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"]
1

1 Answers

7
votes

There is no functional difference, it's only name for build stage. The image built in this stage of dockerfile (stage starts with the FROM keyword and ends before the next FROM) will be later accessible using that name.

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.

For example, names foo and bar

FROM image AS foo
...
...

FROM foo AS bar
...
...

FROM foo
COPY --from=bar

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