When creating a Dockerfile, there are two commands that you can use to copy files/directories into it – ADD
and COPY
. Although there are slight differences in the scope of their function, they essentially perform the same task.
So, why do we have two commands, and how do we know when to use one or the other?
DOCKER ADD
COMMAND
===
Let’s start by noting that the ADD
command is older than COPY
. Since the launch of the Docker platform, the ADD
instruction has been part of its list of commands.
The command copies files/directories to a file system of the specified container.
The basic syntax for the ADD
command is:
ADD <src> … <dest>
It includes the source you want to copy (<src>
) followed by the destination where you want to store it (<dest>
). If the source is a directory, ADD
copies everything inside of it (including file system metadata).
For instance, if the file is locally available and you want to add it to the directory of an image, you type:
ADD /source/file/path /destination/path
ADD
can also copy files from a URL. It can download an external file and copy it to the wanted destination. For example:
ADD http://source.file/url /destination/path
An additional feature is that it copies compressed files, automatically extracting the content to the given destination. This feature only applies to locally stored compressed files/directories.
ADD source.file.tar.gz /temp
Bear in mind that you cannot download and extract a compressed file/directory from a URL. The command does not unpack external packages when copying them to the local filesystem.
DOCKER COPY
COMMAND
===
Due to some functionality issues, Docker had to introduce an additional command for duplicating content – COPY
.
Unlike its closely related ADD
command, COPY
only has only one assigned function. Its role is to duplicate files/directories in a specified location in their existing format. This means that it doesn’t deal with extracting a compressed file, but rather copies it as-is.
The instruction can be used only for locally stored files. Therefore, you cannot use it with URLs to copy external files to your container.
To use the COPY
instruction, follow the basic command format:
Type in the source and where you want the command to extract the content as follows:
COPY <src> … <dest>
For example:
COPY /source/file/path /destination/path
Which command to use? (Best Practice)
Considering the circumstances in which the COPY
command was introduced, it is evident that keeping ADD
was a matter of necessity. Docker released an official document outlining best practices for writing Dockerfiles, which explicitly advises against using the ADD
command.
Docker’s official documentation notes that COPY
should always be the go-to instruction as it is more transparent than ADD
.
If you need to copy from the local build context into a container, stick to using COPY
.
The Docker team also strongly discourages using ADD
to download and copy a package from a URL. Instead, it’s safer and more efficient to use wget or curl within a RUN
command. By doing so, you avoid creating an additional image layer and save space.
Ref: https://phoenixnap.com/kb/docker-add-vs-copy
docker cp
on the host (Docker command line) will copy files to/from a running container. The COPY directive is the Dockerfile is for building the image. – Trephine