skip to Main Content

I’m new to docker and I’m following Microsoft’s tutorial for building a .net core console app with docker at https://learn.microsoft.com/en-us/dotnet/core/docker/build-container?tabs=windows

This is the docker file definition for a simple Hello World app

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-env
WORKDIR /app

COPY . ./
# Restore as distinct layers
RUN dotnet restore
# Build and publish a release
RUN dotnet publish -c Release -o out

# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "DotNet.Docker.dll"]

In the above example, what is build-env from this line COPY --from=build-env /app/out . ?
I tried google, but no relevant articles are returned and can’t find any help in the official docs either.

In the tutorial page it says

The COPY command tells Docker to copy the specified folder on your
computer to a folder in the container. In this example, the publish
folder is copied to a folder named app in the container.

I assume the author means we copy the contents of /app/out to the /app folder, as there is no folder named /publish. So the destination folder is defined by the previous line: WORDIR /app. When I initially read COPY --from=build-env /app/out . I assumed it copies from /build-env to app/out but this doesn’t seem to be the case.

Also, what is the purpose of the COPY . ./ line?

2

Answers


  1. build-env is defined in the first line of the dockerfile: mcr.microsoft.com/dotnet/sdk:6.0

    The copy . ./ means to copy all files from src location (current) to destination (./) . You can get more details on dockerfile in https://docs.docker.com/engine/reference/builder/

    Login or Signup to reply.
  2. build-env this context is just an alias to mcr.microsoft.com/dotnet/sdk:6.0 image.

    The syntax to create an alias for an image is as follows:
    [image_name] AS [Alias]

    Now you can use the Alias instead of the lengthy image name in your Dockerfile.

    COPY --from=build-env /app/out . copies /app/out from build-env based container to The WORKDIR /app on the new image that will be built from this dockerfile.

    COPY . ./ will copy the contents of the directory where this dockerfile is located to the WORKDIR /app on the new image that will be built from this dockerfile.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search