Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

docker

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 .

like image 727
Tandi Sunarto Avatar asked May 15 '18 18:05

Tandi Sunarto


People also ask

How do I remove a specific image from docker?

Docker rmi To remove the image, you first need to list all the images to get the Image IDs, Image name and other details. By running simple command docker images -a or docker images . After that you make sure which image want to remove, to do that executing this simple command docker rmi <your-image-id> .

Does docker build remove old image?

If you are fine with keeping a single image, you can use docker image prune -f , which will remove all images but the ones associated with a container, so if you run this command while the container is running, it will remove the rest of the images.

How does multi stage docker build work?

A multistage build allows you to use multiple images to build a final product. In a multistage build, you have a single Dockerfile, but can define multiple images inside it to help build the final image.


1 Answers

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

like image 154
trust512 Avatar answered Sep 30 '22 16:09

trust512