简体   繁体   中英

Do intermediate Docker images occupy disk space?

If there are RUN commands in docker file then it does create some intermediate images. My question is whether such intermediate images occupy any memory of your hard drive? If yes, docker build --rm should be enough?

RUN does not create intermediate images , but intermediate layers / containers .

If you run docker build --rm these containers will be deleted after they're generated, so yes it will save space in your disk but will need more time each time you rebuild the same image, since it will create those layers everytime as they are not cached.

Edit (thanks @Shashank V): The --rm option is set by default, so having it or not makes no difference.

Yes intermediate layers occupy disk space and it is a good thing usually. This facilitates re-use of layers and speedy builds. What you should be concentrating on instead is to reduce the number of layers by optimising the dockerfile. Your final docker image is actually a combination of all the layers. So you cannot remove the layers unless you remove the final image and no other image is using the layers.

docker build --rm does not save any extra disk space. To understand why, you should know how docker build works - Each instruction (eg, RUN) in a dockerfile starts a new container, after the instruction completes, the container exits, and is committed to an image.

docker build --rm removes these intermediate containers . --rm option is true by default and so docker build --rm has not extra affect compared to docker build . For some reason if you want to keep the intermediate containers then you can turn it off with --rm=False .

If there are any layers which are not being used by any other images, you can remove them. These are called dangling layers. You can remove them with following command -

docker rmi $(docker images -f "dangling=true" -q)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM