Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference with AS base and AS build in 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"]
like image 621
Frédéric Fect Avatar asked May 01 '19 17:05

Frédéric Fect


1 Answers

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.

  • https://docs.docker.com/engine/reference/builder/#from

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/

like image 112
michalhosna Avatar answered Oct 26 '22 07:10

michalhosna