Dockerfile 中的“COPY"和“ADD"命令有什么区别? [英] What is the difference between the 'COPY' and 'ADD' commands in a Dockerfile?

查看:43
本文介绍了Dockerfile 中的“COPY"和“ADD"命令有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Dockerfile 中的 COPYADD 命令之间有什么区别,我什么时候应该使用一个?

COPY <目标>

<块引用>

COPY 指令将从 复制新文件并将它们添加到容器的文件系统中

ADD <目标>

<块引用>

ADD 指令将从 <src> 复制新文件,并将它们添加到容器的文件系统中 <dest> 路径.

解决方案

你应该检查 ADDCOPY 文档以获得对其行为的更详细描述,但简而言之,主要区别在于 ADD 可以比 COPY 执行更多操作:

  • ADD 允许 成为 URL
  • 参考下面的评论,ADD 文档 声明:
<块引用>

如果是采用可识别压缩格式(identity、gzip、bzip2 或 xz)的本地 tar 存档,则将其解压缩为目录.来自远程 URL 的资源不会被解压.

请注意编写 Dockerfile 的最佳实践 建议在不需要 ADD 魔法的地方使用 COPY.否则,当您打算将 keep_this_archive_intact.tar.gz 复制到您的容器中时,您(因为您必须查找此答案)可能会在某一天感到惊讶,但相反,你把内容喷到你的文件系统上.

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>

The COPY instruction will copy new files from <src> and add them to the container's filesystem at path <dest>

ADD <src> <dest>

The ADD instruction will copy new files from <src> and add them to the container's filesystem at path <dest>.

解决方案

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 allows <src> to be a URL
  • Referring to comments below, the ADD documentation states that:

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.

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屋!

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