DockerFile单行与多行指令 [英] DockerFile one-line vs multi-line instruction

查看:96
本文介绍了DockerFile单行与多行指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,docker构建的工作方式是针对每条指令行,它创建一个单独的图像/层.但是,如果没有任何变化,在重新使用各层或避免重建这些层方面非常有效.

To my knowledge of the way docker build works is that for each line of instruction, it creates a separate image/layer. However, it is very efficient in managing to reuse the layers or avoid rebuilding those layers if nothing has changed.

所以我在同一行还是多行下面放置指令是否重要?为了方便起见,我宁愿使用单行选项,除非它不是一个有效的选项.

So does it matter if I put below instruction either on same line or multi-line? For convenience, I would prefer the single line option unless it is not an efficient option.

多行指令

RUN apt-get -y update
RUN apt-get -y install ...

单行指令

RUN apt-get -y update && apt-get -y install

推荐答案

在这种特定情况下,将 apt-get update apt-get install 放在一起很重要.从更广泛的角度来看,较少的层被认为是更好"的层,但是几乎没有任何明显的区别.

In this specific case it is important to put apt-get update and apt-get install together. More broadly, fewer layers is considered "better" but it almost never has a perceptible difference.

在实践中,我倾向于将相关"命令组合到同一条 RUN 命令中.如果我需要从源代码配置和安装软件包,则可以将它们分组在一起,即使我更改了 make 参数,我也不介意重新运行 configure .如果我需要配置和安装三个软件包,它们将进入单独的 RUN 行.

In practice I tend to group together "related" commands into the same RUN command. If I need to configure and install a package from source, that can get grouped together, and even if I change make arguments I don't mind re-running configure. If I need to configure and install three packages, they'd go into separate RUN lines.

此特定 apt-get 示例中的重要区别在于层缓存.假设您的Dockerfile具有

The important difference in this specific apt-get example is around layer caching. Let's say your Dockerfile has

FROM ubuntu:18.04
RUN apt-get update
RUN apt-get install package-a

如果第二次运行 docker build ,它将确定它已经运行了所有这三个命令,并且输入没有更改,因此它将非常快速地运行,并且您将获得一个相同的图像.

If you run docker build a second time, it will decide it's already run all three of these commands and the input hasn't changed, so it will run very quickly and you'll get an identical image out.

现在,您在一两天后回来,意识到自己缺少了某些东西,因此您进行了更改

Now you come back a day or two later and realize you were missing something, so you change

FROM ubuntu:18.04
RUN apt-get update
RUN apt-get install package-a package-b

再次运行 docker build 时,Docker决定它已经运行 apt-get update ,可以直接跳转到 apt-get install 线.在这种特定情况下,您会遇到麻烦:Debian和Ubuntu相当频繁地更新其存储库,并且当他们这样做时,旧版本的软件包会被删除.因此,您两天前的 apt-get update 指向一个不再存在的软件包,您的构建将失败.

When you run docker build again, Docker decides it's already run apt-get update and can jump straight to the apt-get install line. In this specific case you'll have trouble: Debian and Ubuntu update their repositories fairly frequently, and when they do the old versions of packages get deleted. So your apt-get update from two days ago points at a package that no longer exists, and your build will fail.

通过始终将两个 apt-get 命令放到同一 docker run

You'll avoid this specific problem by always putting the two apt-get commands together in the same docker run line

FROM ubuntu:18.04
RUN apt-get update \
 && DEBIAN_FRONTEND=noninteractive \
    apt-get install --assume-yes --no-install-recommends \
      package-a \
      package-b

这篇关于DockerFile单行与多行指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆