Home >Operation and Maintenance >Docker >What is the difference between copy and add instructions in docker

What is the difference between copy and add instructions in docker

青灯夜游
青灯夜游Original
2021-11-29 16:57:0723854browse

Difference: The COPY instruction does not support obtaining resources from remote URLs. It can only read resources from the host where docker build is executed and copy them to the image; while the ADD instruction supports obtaining resources from remote URLs, which can be obtained through the URL. Read resources from the remote server and copy them to the image.

What is the difference between copy and add instructions in docker

The operating environment of this tutorial: linux5.9.8 system, docker-1.13.1 version, Dell G3 computer.

Both the COPY instruction and the ADD instruction in the Dockerfile can copy or add resources on the host to the container image, both of which are completed during the process of building the image.

The difference between ADD and COPY of DockerFile

The only difference between the COPY instruction and the ADD instruction is: whether it supports obtaining from a remote URL resource.

The COPY instruction can only read resources from the host where docker build is executed and copy them to the image. The ADD instruction also supports reading resources from the remote server through the URL and copying them to the image.

For the same requirements, it is recommended to use the COPY instruction. The ADD instruction is better at reading local tar files and decompressing them.

ADD and COPY instructions

COPY has the same syntax as ADD and copies files.

ADD

Copy the new file, directory, or remote file URL cbce1a3cf2f839037583dce8e845670b and add them to 6be58d1426a577c69ada50a4bc87dd69.

cbce1a3cf2f839037583dce8e845670bMultiple resources can be specified, but if they are files or directories, their paths are interpreted relative to the source of the build context, which is _WORKDIR_.

Each cbce1a3cf2f839037583dce8e845670b may contain wildcards, and matching will use Go's filepath.Match rules. For example:

Add all files starting with "hom":

ADD hom* /mydir/

In the example below, ? is replaced with any single character, such as "home.txt".

ADD hom?.txt /mydir/

6be58d1426a577c69ada50a4bc87dd69 is an absolute path, or a relative path relative to WORKDIR.

ADD instruction has the following advantages:

  • 1. If the source path is a file and the target path ends with /, then docker The target path will be treated as a directory and the source files will be copied to the directory.
    If the target path does not exist, the target path will be automatically created.

  • 2. If the source path is a file and the target path does not end with /, docker will treat the target path as a file.
    If the target path does not exist, a file will be created with the name of the target path, and the content will be from the same source file;
    If the target file is an existing file, it will be overwritten with the source file. Of course, only the content will be overwritten, and the file name will be the same. Target file name.
    If the target file is actually an existing directory, the source file will be copied to the directory. Note that in this case it is best to end the display with / to avoid confusion.

  • 3. If the source path is a directory and the target path does not exist, docker will automatically create a directory with the target path and copy the files in the source path directory.
    If the target path is an existing directory, docker will copy the files in the source path directory to the directory.

  • 4. If the source file is an archive file (compressed file), docker will automatically decompress it.

The above advantages are also the disadvantages of ADD

Recommended learning: "docker video tutorial"

The above is the detailed content of What is the difference between copy and add instructions in docker. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn