Dockerfile中的“ COPY”和“ ADD”命令有什么区别? [英] What is the difference between the 'COPY' and 'ADD' commands in a Dockerfile?
问题描述
Dockerfile中的 COPY
和 ADD
命令之间有什么区别,何时使用
What is the difference between the COPY
and ADD
commands in a Dockerfile, and when would I use one over the other?
COPY <src> <dest>
COPY指令将从
复制新文件< src>
并将它们添加到容器的文件系统中,路径为< dest>
The COPY instruction will copy new files from
<src>
and add them to the container's filesystem at path<dest>
ADD <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
和 COPY
文档以更详细地描述其行为,但简而言之,主要区别在于 ADD
可以完成 COPY
:
You should check the ADD
and COPY
documentation for a more detailed description of their behaviors, but in a nutshell, the major difference is that ADD
can do more than COPY
:
-
ADD
允许< src>
作为URL - 请参阅下面的注释,
ADD
文档指出:
ADD
allows<src>
to be a URL- Referring to comments below, the
ADD
documentation states that:
如果记录中是本地tar存档压缩格式(身份,gzip,bzip2或xz),然后将其解压缩为目录。不会解压缩来自远程URL的资源。
If is a local tar archive in a recognized compression format (identity, gzip, bzip2 or xz) then it is unpacked as a directory. Resources from remote URLs are not decompressed.
请注意,编写Dockerfile的最佳做法建议使用 COPY
,其中<$ c不需要$ c> ADD 。否则,有一天(当您打算将 keep_this_archive_intact.tar.gz
复制到其中时,您(因为您必须查找此答案),您可能会感到惊讶。您的容器,而是将内容喷到文件系统上。
Note that the Best practices for writing Dockerfiles suggests using COPY
where the magic of ADD
is not required. Otherwise, you (since you had to look up 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.
这篇关于Dockerfile中的“ COPY”和“ ADD”命令有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!