1

I'm using the following Dockerfile to build an asp.net project using docker containers.

FROM mcr.microsoft.com/dotnet/aspnet:3.1 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build
WORKDIR /src
COPY . .
WORKDIR "/src/IdentityANSP/"
RUN dotnet build "IdentityANSP.csproj" -c Release -o /app

FROM build AS publish
RUN dotnet publish "IdentityANSP.csproj" -c Release -o /app

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

Here, the artifacts of the build will be placed in a new docker image. Instead of that, I want the artifacts to copied to a directory in Docker host machine (localhost). To get that done, how should I change the Dockerfile?

PS: I'm new to Docker as well as stackoverflow. So, please ignore my mistakes in the question. Thanks

Lakizuru
  • 13
  • 2

1 Answers1

1

Long story short (see this answer for details), you cannot make Dockefile to extract build artefacts out. However, you can copy files out of a running container:

# Build the image
docker build -t my_image .

# Spin up a container from the image
docker run --rm -d --name my_temporary_container my_image

# Copy file
docker cp my_temporary_container:/path/in/container /host/path

# Stop the container
docker stop my_temporary_container

If the image in question requires a lot of time to start or some external files/variables/etc, you can override the default command so that it will be gracefully doing nothing instead:

# Start a container that endlessly reads its own stdout stream
docker run --rm -d --name my_temporary_container --entrypoint="" my_image /bin/cat /dev/stdout
anemyte
  • 17,618
  • 1
  • 24
  • 45