Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
422 views
in Technique[技术] by (71.8m points)

docker - 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>的容器文件系统中。)

  ask by Steve translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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复制到你的容器中,而是将内容喷到你的文件系统上。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...