Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

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>.

like image 201
Steve Avatar asked Jul 25 '14 14:07

Steve


People also ask

What is the difference between copy and add?

First, the ADD directive can accept a remote URL for its source argument. The COPY directive, on the other hand, can only accept local files. Note that using ADD to fetch remote files and copying is not typically ideal. This is because the file will increase the overall Docker image size.

What is ADD command in Dockerfile?

The ADD command is used to copy files/directories into a Docker image. It can copy data in three ways: Copy files from the local storage to a destination in the Docker image. Copy a tarball from the local storage and extract it automatically inside a destination in the Docker image.

What is the Copy command in Docker?

This Docker copy (cp) command takes a file that exists inside a Docker container and saves it to your computer's local file system. The container-name specified in the example command can be either the name given to the Docker container when it was started, or the unique ID that Docker assigned to the container.

What is the difference between run and CMD command in a Dockerfile?

RUN is an image build step, the state of the container after a RUN command will be committed to the container image. A Dockerfile can have many RUN steps that layer on top of one another to build the image. CMD is the command the container executes by default when you launch the built image.


2 Answers

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.

like image 115
icecrime Avatar answered Sep 17 '22 21:09

icecrime


COPY is

Same as 'ADD', but without the tar and remote URL handling.

Reference straight from the source code.

like image 28
caike Avatar answered Sep 20 '22 21:09

caike