多阶段Docker:RUN WGET与ADD [英] Multi-stage Docker: RUN wget vs ADD

查看:497
本文介绍了多阶段Docker:RUN WGET与ADD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Docker文档的最佳做法部分

The best practices section of the Docker docs says this:


由于图像大小很重要,因此强烈建议不要使用ADD从远程
URL获取软件包;您应该使用curl或wget代替。
这样,您可以删除提取
之后不再需要的文件,而不必在图像中添加另一层。
例如,您应该避免做以下事情:

Because image size matters, using ADD to fetch packages from remote URLs is strongly discouraged; you should use curl or wget instead. That way you can delete the files you no longer need after they’ve been extracted and you won’t have to add another layer in your image. For example, you should avoid doing things like:

ADD http://example.com/big.tar.xz /usr/src/things/
RUN tar -xJf /usr/src/things/big.tar.xz -C /usr/src/things
RUN make -C /usr/src/things all

而是执行以下操作:

RUN mkdir -p /usr/src/things \
    && curl -SL http://example.com/big.tar.xz \
    | tar -xJC /usr/src/things \
    && make -C /usr/src/things all


在后面的注释中:


在Docker 17.05之前,甚至更多,在Docker 1.10之前,
很重要以最小化图像中的层数。 [...] Docker
17.05及更高版本增加了对多阶段构建的支持,使您仅可以将所需的工件复制到最终映像中。

Prior to Docker 17.05, and even more, prior to Docker 1.10, it was important to minimize the number of layers in your image. [...] Docker 17.05 and higher add support for multi-stage builds, which allow you to copy only the artifacts you need into the final image.

甚至


使用Bash& amp;压缩两个RUN命令

compress[ing] two RUN commands together using the Bash && operator [is] failure-prone and hard to maintain.

在我看来,如果您使用的是多运算符,阶段构建,有关 ADD 的建议是不准确的。除非您要下载真正巨大的内容,否则额外的层就不太可能成为问题,因为 local 磁盘空间很便宜,而且很容易清除旧图像。确实,当编码人员通常没有构建命令时,会清理它们的中间工件以节省空间!

It seems to me that if you are using multi-stage builds, the advice about ADD is inaccurate. The extra layers are unlikely to be a problem unless you are downloading something truly huge, as local disk space is cheap and it's easy to clean out old images. Indeed, when coding one doesn't usually have build commands clean their intermediate artefacts to save space!

此外, ADD RUN wget 相比,code>有一个主要优势:它检测到目标何时更改

In addition, ADD has a major advantage over RUN wget: it detects when its target has changed.

我丢失了某些东西吗,还是多阶段构建可以恢复 ADD

Am I missing something, or do multi-stage builds rehabilitate ADD?

推荐答案

它的确像这样: ADD 用于 Katakoda使用多阶段构建来创建优化的Docker映像 示例(对于第一个映像,第二个之前

It does look like it: ADD is used for instance in the Katakoda "Creating optimized Docker Images using Multi-Stage Builds" example (for the first image, before the second stage.

通过两个步骤,您可以专注于将( 42- aufs的最大值硬限制127 )层只是在第二阶段。

With two steps, you can focus on minimizing the (42-max for aufs, hard-limit 127) layers in the second stage only.

这篇关于多阶段Docker:RUN WGET与ADD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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