简体   繁体   English

中间 Docker 镜像是否占用磁盘空间?

[英]Do intermediate Docker images occupy disk space?

If there are RUN commands in docker file then it does create some intermediate images.如果 docker 文件中有 RUN 命令,那么它确实会创建一些中间映像。 My question is whether such intermediate images occupy any memory of your hard drive?我的问题是这样的中间图像是否占用了您硬盘的任何内存? If yes, docker build --rm should be enough?如果是, docker build --rm应该足够了吗?

RUN does not create intermediate images , but intermediate layers / containers . RUN不创建中间映像,而是创建中间/容器

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.如果您运行docker build --rm这些容器将在它们生成后被删除,所以是的,它会节省您的磁盘空间,但每次重建相同的图像时都需要更多时间,因为它每次都会创建这些层不被缓存。

Edit (thanks @Shashank V): The --rm option is set by default, so having it or not makes no difference.编辑(感谢@Shashank V): --rm选项是默认设置的,所以有没有没有区别。

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.您应该专注于通过优化 dockerfile 来减少层数。 Your final docker image is actually a combination of all the layers.您最终的 docker 镜像实际上是所有层的组合。 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. docker build --rm不会节省任何额外的磁盘空间。 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是如何工作的 - dockerfile 中的每条指令(例如,RUN)都会启动一个新容器,在指令完成后,容器退出并提交给映像。

docker build --rm removes these intermediate containers . docker build --rm删除这些中间容器 --rm option is true by default and so docker build --rm has not extra affect compared to docker build . --rm选项是默认,因此真正的docker build --rm没有额外的影响相比, docker build For some reason if you want to keep the intermediate containers then you can turn it off with --rm=False .出于某种原因,如果您想保留中间容器,则可以使用--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.这些被称为dangling层。 You can remove them with following command -您可以使用以下命令删除它们 -

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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