在 Dockerfile 中的单个 RUN 指令中指定多个 UNIX 命令的目的 [英] Purpose of specifying several UNIX commands in a single RUN instruction in Dockerfile
问题描述
我注意到许多 Dockerfiles
试图通过单个 RUN
指令中的多个 UNIX
命令来最小化指令数量.那么有什么原因吗?
下面的两个 Dockerfile 的结果也有区别吗?
Dockerfile1
来自 ubuntu维护者 demousr@example.com运行 apt-get 更新运行 apt-get install –y nginxCMD [回声",创建的图像"]
Dockerfile2
来自 ubuntu维护者 demousr@example.com运行 apt-get 更新 &&apt-get install –y nginxCMD [回声",创建的图像"]
粗略地说,一个 Docker 镜像包含一些元数据和一个层数组,并通过添加一个容器层(读写)在这些层上构建一个正在运行的容器,此时底层图像中的层是只读的.
这些层可以根据配置的驱动程序以不同的方式存储在磁盘中.例如,从官方 Docker 文档中获取的以下图像说明了使用
接下来,Dockerfile指令RUN
、COPY
和ADD
创建层,Docker网站上提到的最佳实践特别推荐给将连续的 RUN
命令合并到一个 RUN
命令中,以减少层数,从而减小最终图像的大小:p>
https://docs.docker.com/develop/dev-best-实践/
<块引用>[...] 尝试通过最小化 Dockerfile 中单独的 RUN
命令的数量来减少映像中的层数.您可以通过将多个命令合并到单个 RUN
行并使用 shell 的机制将它们组合在一起来做到这一点.[…]
另请参阅:https://docs.docker.com/develop/develop-images/dockerfile_best-practices/
此外,在您的示例中:
运行 apt-get update -y -q运行 apt-get install -y nginx
如果你对这个 Dockerfile
执行 docker build -t your-image-name .
,然后在一段时间后编辑 Dockerfile
,添加nginx
之外的另一个包,然后再次执行 docker build -t your-image-name .
,由于 Docker 缓存机制,apt-get update -y-q
不会再次执行,因此 APT 缓存将被废弃.所以这是合并两个 RUN
命令的另一个好处.
I have noticed that many Dockerfiles
try to minimize the number of instructions by several UNIX
commands in a single RUN
instruction. So is there any reason?
Also is there any difference in the outcomes between the two Dockerfiles below?
Dockerfile1
FROM ubuntu
MAINTAINER demousr@example.com
RUN apt-get update
RUN apt-get install –y nginx
CMD ["echo", "Image created"]
Dockerfile2
FROM ubuntu
MAINTAINER demousr@example.com
RUN apt-get update && apt-get install –y nginx
CMD ["echo", "Image created"]
Roughly speaking, a Docker image contains some metadata & an array of layers, and a running container is built upon these layers by adding a container layer (read-and-write), the layers from the underlying image being read-only at that point.
These layers can be stored in the disk in different ways depending on the configured driver. For example, the following image taken from the official Docker documentation illustrates the way the files changed in these different layers are taken into account with the OverlayFS storage driver:
Next, the Dockerfile instructions RUN
, COPY
, and ADD
create layers, and the best practices mentioned on the Docker website specifically recommend to merge consecutive RUN
commands in a single RUN
command, to reduce the number of layers, and thereby reduce the size of the final image:
https://docs.docker.com/develop/dev-best-practices/
[…] try to reduce the number of layers in your image by minimizing the number of separate
RUN
commands in your Dockerfile. You can do this by consolidating multiple commands into a singleRUN
line and using your shell’s mechanisms to combine them together. […]
See also: https://docs.docker.com/develop/develop-images/dockerfile_best-practices/
Moreover, in your example:
RUN apt-get update -y -q
RUN apt-get install -y nginx
if you do docker build -t your-image-name .
on this Dockerfile
, then edit the Dockerfile
after a while, add another package beyond nginx
, then do again docker build -t your-image-name .
, due to the Docker cache mechanism, the apt-get update -y -q
won't be executed again, so the APT cache will be obsolete. So this is another upside for merging the two RUN
commands.
这篇关于在 Dockerfile 中的单个 RUN 指令中指定多个 UNIX 命令的目的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!