What is the difference between the `COPY` and `ADD` commands in a Dockerfile?
dockerfile中的
1 | COPY <src> <dest> |
The COPY instruction will copy new files from
and add them to the container's filesystem at path
1 | ADD <src> <dest> |
The ADD instruction will copy new files from
and add them to the container's filesystem at path .
您应该查看
ADD 允许 作为URL- 如果
ADD 的 参数是公认压缩格式的存档文件,则将对其进行解包。
注意,编写dockerfiles的最佳实践建议使用
Same as 'ADD', but without the tar and remote URL handling.
直接引用源代码。
有一些关于这一点的官方文档:编写dockerfiles的最佳实践
Because image size matters, using
ADD to fetch packages from remote URLs is strongly discouraged; you should usecurl orwget 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.
1 2 3 4 | 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 useCOPY .
从Docker文档:
ADD or COPY
Although ADD and COPY are functionally similar, generally speaking, COPY is preferred. That’s because it’s more transparent than 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. Consequently, the best use for ADD is local tar file auto-extraction into the image, as in ADD rootfs.tar.xz /.
更多:编写dockerfiles的最佳实践
如果要将xx.tar.gz添加到
复印件:
1 2 3 | 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 |
用于添加:
1 | ADD resources/jdk-7u79-linux-x64.tar.gz /usr/local/ |
添加仅支持本地焦油提取。此外,复制将使用三层,但添加仅使用一层。
从Docker文档:https://docs.docker.com/engine/userguide/eng image/dockerfile_best-practices/添加或复制
"尽管"添加"和"复制"功能相似,但一般来说,还是首选"复制"。这是因为它比添加更透明。复制仅支持将本地文件基本复制到容器中,而添加具有一些不立即明显的功能(如仅本地tar提取和远程url支持)。因此,添加的最佳用途是将本地tar文件自动提取到图像中,如add rootfs.tar.xz/。
如果有多个dockerfile步骤使用来自上下文的不同文件,请分别复制它们,而不是一次全部复制。这将确保每个步骤的构建缓存仅在特定所需文件发生更改时失效(强制重新运行该步骤)。
例如:
1 2 3 | COPY requirements.txt /tmp/ RUN pip install --requirement /tmp/requirements.txt COPY . /tmp/ |
与放置副本相比,运行步骤的缓存无效次数更少。/TMP/之前。
因为图像大小很重要,所以强烈建议不要使用add-to-fetch从远程URL获取包;您应该使用curl或wget。这样,您就可以删除提取后不再需要的文件,而无需在图像中添加其他层。例如,您应该避免执行以下操作:
1 2 3 | 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 |
相反,做一些类似的事情:
1 2 3 4 | RUN mkdir -p /usr/src/things \ && curl -SL htt,p://example.com/big.tar.xz \ | tar -xJC /usr/src/things \ && make -C /usr/src/things all |
对于不需要添加的tar自动提取功能的其他项目(文件、目录),应始终使用copy。"
使用简单文件
使用远程资源
copyP></
这一个或多个本地拷贝你的目的地在一个or Folders码头的图像。P></
1 2 3 | COPY < src> < dest > COPY ["< source >",..."< destination >"] |
(this form is required for路径含有空格)P></
dockerfile an example,uses
1 2 3 4 5 6 | FROM ruby:2.5.1 WORKDIR /usr/src/app COPY Gemfile Gemfile.lock ./ RUN bundle install COPY . . CMD ["./your-daemon-or-script.rb"] |
在后续的图像层builds en the starting with the parent,红宝石:2.5.1图像,从定义的使用。P></
the码头指令
这意味着如果你app' any of the change the S文件,你可以使用高速缓存的图像重建the parent码头和中间层。This is much of them建设高效的黑莓比从划痕。P></
addP></
This has similar to
1 2 3 | ADD < src> < dest > ADD ["< source >",..."< destination >"] (this form is required for paths containing whitespace) |
as as Copying本地文件和目录阱into the destination within the image has some额外的码头,它的特点:P></
如果是在局域网中
最佳实践dockerfile for Copying from URLP></
suggests that is not often码头/高效的URL使用add to copy from a en is to和最佳实践,包括使用其他策略来to the远程文件。P></
因为图像校正使用add to size,取packages从偏远的discouraged urls is You should强;而不是使用wget或curl。that the Way You can删除你不需要的文件我已经在他们的中央银行和extracted have to add another *在你的图像层。?-?最佳dockerfileP></
for example,你应该做的事情:avoid类P></
1 2 3 | 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 |
Do something like和instead,:P></
1 2 3 4 | 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 |
for other items(文件,目录
开源:HTTPS:/ / / / nickjanetakis.com水龙头码头-博客- 2 -差分之间的拷贝和文件:--- dockerileP></
COPY and ADD are both Dockerfile instructions that serve similar purposes. They let you copy files from a specific location into a Docker image.
COPY takes in a src and destination. It only lets you copy in a local file or directory from your host (the machine building the Docker image) into the Docker image itself.
ADD lets you do that too, but it also supports 2 other sources. First, you can use a URL instead of a local file / directory. Secondly, you can extract a tar file from the source directly into the destination
A valid use case for ADD is when you want to extract a local tar file into a specific directory in your Docker image.
If you’re copying in local files to your Docker image, always use COPY because it’s more explicit.
重要注意事项
我必须在我的DoCKER图像中使用EDCOX1 1和UNTAR Java包。当我比较使用add创建的Docker图像大小时,它比使用copy、tar-xzf*.tar.gz和rm*.tar.gz创建的Docker图像大180MB。
这意味着尽管add删除了tar文件,但它仍然保留在某个地方。它使图像更大!!
1 | docker build -t {image name} -v {host directory}:{temp build directory} . |
这是将文件复制到图像中的另一种方法。-v选项临时创建一个在生成过程中使用的卷。
这与其他卷不同,因为它只为构建装载主机目录。可以使用标准的cp命令复制文件。
此外,与curl和wget一样,它可以在命令堆栈中运行(在单个容器中运行),而不必乘以图像大小。添加和复制不可堆叠,因为它们在独立的容器中运行,在附加容器中执行的这些文件上的后续命令将使图像大小成倍增加:
选项设置如下:
1 | -v /opt/mysql-staging:/tvol |
以下操作将在一个容器中执行:
1 2 3 4 5 6 7 8 9 10 11 12 | RUN cp -r /tvol/mysql-5.7.15-linux-glibc2.5-x86_64 /u1 && \ mv /u1/mysql-5.7.15-linux-glibc2.5-x86_64 /u1/mysql && \ mkdir /u1/mysql/mysql-files && \ mkdir /u1/mysql/innodb && \ mkdir /u1/mysql/innodb/libdata && \ mkdir /u1/mysql/innodb/innologs && \ mkdir /u1/mysql/tmp && \ chmod 750 /u1/mysql/mysql-files && \ chown -R mysql /u1/mysql && \ chgrp -R mysql /u1/mysql |