Tandi Sunarto
Tandi Sunarto

Reputation: 251

How to delete image from previous stage on Docker multi-stage build

Here is my Dockerfile

FROM microsoft/aspnetcore-build as build-stage
ARG source=.
WORKDIR /app
COPY $source/sample-api.csproj .
RUN dotnet restore
COPY $source .
RUN dotnet publish -o /publish

FROM microsoft/aspnetcore
WORKDIR /sample-api
COPY --from=build-stage /publish .
EXPOSE 3000
ENTRYPOINT dotnet sample-api.dll

That will create 2 images

REPOSITORY     TAG          IMAGE ID
sample-api     latest       
<none>         <none>

Is it possible to delete the first stage image <none> from the second stage? It is not needed anymore after the COPY on the second stage is executed COPY --from=build-stage /publish .

Upvotes: 8

Views: 6882

Answers (1)

trust512
trust512

Reputation: 2254

This Docker behaviour is here on purpose, it leaves this image for caching purposes - for when a build will be triggered next time.

For now there is no option to delete it while building.

The only option to get rid of this image is to delete it manually. One of the many possibilities:

$ docker image prune

Executed afterwards. This command removes all dangling images. Specifically, it removes all images not referenced by any local container, which includes the <none> image you mentioned.

ticket ref

Upvotes: 13

Related Questions