Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does docker --rm=true affect caching adversly?

Tags:

docker

docker build --rm=true

This is the default option, which makes it to delete all intermediate images after a successful build.

Does it affect the caching adversely? Since cache relies on the intermediate images I think?

like image 281
colinfang Avatar asked Jun 04 '26 00:06

colinfang


1 Answers

Why not try it and find out?

$ cat Dockerfile 
FROM debian

RUN touch /x
RUN touch /y

$ docker build --rm .
Sending build context to Docker daemon 2.048 kB
Sending build context to Docker daemon 
Step 0 : FROM debian
 ---> df2a0347c9d0
Step 1 : RUN touch /x
 ---> Running in 2e5ff13506e5
 ---> fd4dd6845e31
Removing intermediate container 2e5ff13506e5
Step 2 : RUN touch /y
 ---> Running in b2a585989fa5
 ---> 0093f530941b
Removing intermediate container b2a585989fa5
Successfully built 0093f530941b

$ docker build --rm .
Sending build context to Docker daemon 2.048 kB
Sending build context to Docker daemon 
Step 0 : FROM debian
 ---> df2a0347c9d0
Step 1 : RUN touch /x
 ---> Using cache
 ---> fd4dd6845e31
Step 2 : RUN touch /y
 ---> Using cache
 ---> 0093f530941b
Successfully built 0093f530941b

So no, the cache still works. As you pointed out, --rm is actually on by default (you would have to run --rm=false to turn it off), but it refers to the intermediate containers not the intermediate images. These are the containers that Docker ran your build commands in to create the images. In some cases you might want to keep those containers around for debugging, but normally the images are enough. In the above output, we can see the containers 2e5ff13506e5 and b2a585989fa5, which are deleted, but also the images fd4dd6845e31 and 0093f530941b which are kept.

You can't delete the intermediate images as they are needed by the final image (an image is the last layer plus all ancestor layers).

like image 93
Adrian Mouat Avatar answered Jun 06 '26 17:06

Adrian Mouat