Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to remove all the build history of a docker image?

Tags:

docker

When you do docker history <image_name>, it will display the full history of the docker image build. Is there a way to remove this history?

I've tried docker build --squash ... but it did not work. The history persists.

like image 495
ethanjyx Avatar asked Apr 22 '19 03:04

ethanjyx


People also ask

How do I clear a docker image?

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> . Then you can confirm that image has been removed or not by list all the images and check.

Can I delete docker build cache?

The conclusion is very simple, you can delete it with the following command ( reference URL ). After executing this command, docker system df try ... I was able to erase it safely. If you're using Docker and you're worried about remaining storage space, it might be worth a try.


1 Answers

You can use a multistage build. This is an example for a tomcat image:

docker pull tomcat:7-jre8
docker history tomcat:7-jre8

This shows you the full history of the image.

I now create a Dockerfile like this:

FROM tomcat:7-jre8 as orig

FROM alpine:latest

COPY --from=orig / /

I build it:

docker build -t mytomcat:1.0 .

If I check the history this is what I see now:

docker history mytomcat:1.0

IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
c3cde992658a        6 minutes ago       /bin/sh -c #(nop) COPY dir:f31f2e5f414562467…   454MB
5cb3aa00f899        6 weeks ago         /bin/sh -c #(nop)  CMD ["/bin/sh"]              0B
<missing>           6 weeks ago         /bin/sh -c #(nop) ADD file:88875982b0512a9d0…   5.53MB

Test the new image:

docker run -ti --rm mytomcat:1.0 bash

root@62d8c9934bd4:/# /usr/local/tomcat/bin/startup.sh
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
root@62d8c9934bd4:/# curl http://localhost:8080
...

Hope this is what you are looking for. If not let me know.

like image 60
Mihai Avatar answered Oct 13 '22 05:10

Mihai