Dockerfile中的`COPY`和`ADD`命令有什么区别?

本文翻译自:What is the difference between the `COPY` and `ADD` commands in a Dockerfile?

What is the difference between the COPY and ADD commands in a Dockerfile, and when would I use one over the other? Dockerfile中的COPYADD命令有什么区别,我何时使用另一个?

COPY <src> <dest>

The COPY instruction will copy new files from <src> and add them to the container's filesystem at path <dest> COPY指令将从<src>复制新文件,并将它们添加到路径<dest>的容器文件系统中

ADD <src> <dest>

The ADD instruction will copy new files from <src> and add them to the container's filesystem at path <dest> . ADD指令将从<src>复制新文件,并将它们添加到路径<dest>的容器文件系统中。


#1楼

参考:https://stackoom.com/question/1gike/Dockerfile中的-COPY-和-ADD-命令有什么区别


#2楼

You should check the ADD and COPY documentation for an exhaustive description of their behaviours, but in a nutshell the major difference is that ADD can do more than COPY : 您应该查看ADDCOPY文档以获得有关其行为的详尽描述,但简而言之,主要区别在于ADD可以做的不仅仅是COPY

  • ADD allows <src> to be a URL ADD允许<src>成为URL
  • Referring to comments bellow, the ADD documentation clearly states that: 参考下面的评论, ADD 文件明确指出:

    If is a local tar archive in a recognized compression format (identity, gzip, bzip2 or xz) then it is unpacked as a directory. 如果是以识别的压缩格式(identity,gzip,bzip2或xz)的本地tar存档,则将其解压缩为目录。 Resources from remote URLs are not decompressed. 远程URL中的资源不会被解压缩。

Note that the Best practices for writing Dockerfiles suggests using COPY where the magic of ADD is not required. 请注意, 编写Dockerfiles最佳实践建议使用COPY ,其中不需要ADD的魔力。 Otherwise you (since you had to lookup this answer) are likely to get surprised someday when you mean to copy keep_this_archive_intact.tar.gz into your container, but instead you spray the contents onto your filesystem. 否则你(因为你必须查找这个答案)有一天你可能会感到惊讶,因为你的意思是将keep_this_archive_intact.tar.gz复制到你的容器中,而是将内容喷到你的文件系统上。


#3楼

COPY is COPY

Same as 'ADD', but without the tar and remote URL handling. 与“ADD”相同,但没有tar和远程URL处理。

Reference straight from the source code . 直接从源代码引用。


#4楼

There is some official documentation on that point: Best Practices for Writing Dockerfiles 关于这一点,有一些官方文档: 编写Dockerfiles的最佳实践

Because image size matters, using ADD to fetch packages from remote URLs is strongly discouraged; 由于图像大小很重要,因此强烈建议不要使用ADD从远程URL获取包。 you should use curl or wget instead. 你应该使用curlwget代替。 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. 这样,您可以删除提取后不再需要的文件,并且不必在图像中添加其他图层。

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

For other items (files, directories) that do not require ADD 's tar auto-extraction capability, you should always use COPY . 对于不需要ADD的tar自动提取功能的其他项目(文件,目录),应始终使用COPY


#5楼

From Docker docs: 来自Docker文档:

ADD or COPY 添加或复制

Although ADD and COPY are functionally similar, generally speaking, COPY is preferred. 尽管ADD和COPY在功能上相似,但一般来说,COPY是优选的。 That's because it's more transparent than ADD. 那是因为它比ADD更透明。 COPY only supports the basic copying of local files into the container, while ADD has some features (like local-only tar extraction and remote URL support) that are not immediately obvious. COPY仅支持将本地文件基本复制到容器中,而ADD具有一些功能(如仅限本地的tar提取和远程URL支持),这些功能并不是很明显。 Consequently, the best use for ADD is local tar file auto-extraction into the image, as in ADD rootfs.tar.xz /. 因此,ADD的最佳用途是将本地tar文件自动提取到图像中,如ADD rootfs.tar.xz /中所示。

More: Best practices for writing Dockerfiles 更多: 编写Dockerfiles的最佳实践


#6楼

If you want to add a xx.tar.gz to a /usr/local in container, unzip it, and then remove the useless compressed package. 如果要将xx.tar.gz添加到容器中的/usr/local ,请将其解压缩,然后删除无用的压缩包。

For COPY: 对于COPY:

COPY resources/jdk-7u79-linux-x64.tar.gz /tmp/
RUN tar -zxvf /tmp/jdk-7u79-linux-x64.tar.gz -C /usr/local
RUN rm /tmp/jdk-7u79-linux-x64.tar.gz

For ADD: 对于ADD:

ADD resources/jdk-7u79-linux-x64.tar.gz /usr/local/

ADD supports local-only tar extraction. ADD支持仅本地tar提取。 Besides it, COPY will use three layers, but ADD only uses one layer. 除此之外,COPY将使用三层,但ADD仅使用一层。

发布了0 篇原创文章 · 获赞 136 · 访问量 83万+

猜你喜欢

转载自blog.csdn.net/xfxf996/article/details/105181882