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>
.
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.
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.
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.
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.
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 URLADD
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.
COPY
is
Same as 'ADD', but without the tar and remote URL handling.
Reference straight from the source code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With