Difference Between Copy and Add Commands in Dockerfile

A Dockerfile is a text document containing all commands used to build a Docker image. We have seen an extensive adoption of Docker as the default tool for managing configurations and automating deployments in the recent past.

Advanced features of using Dockerfiles, such as automatic builds, are why developers are adopting Docker. Others include isolating applications from environments they run and security quickly.

When creating Docker containers, it is often necessary to transfer files from the host to the Docker image. These files can include libraries or property files necessary for your application to run.

Difference Between COPY and ADD Commands in Dockerfile

In the Dockerfile, we can copy these files using the COPY or ADD command. These commands are functionally the same; however, some differences exist.

The COPY and ADD commands follow the syntax below.

COPY <src> <dest>
ADD <src> <dest>

Both instructions copy files or directories located at <src> in the localhost and add them to the location <dest> in the container’s file system. For example, in the Dockerfile below, we copy files from the current directory to the directory /var/www in the Docker images.

#  base image
FROM python

# Set your working directory
WORKDIR /var/www/
# Copy the necessary files
COPY ./app.py /var/www/app.py
ADD./requirements.txt /var/www/requirements.txt

# Install the necessary packages
RUN pip install -r /var/www/requirements.txt
ADD mkdir -p /var/www/new_directory
# Run the app
CMD ["echo", "Hello, Developer"]

We want to copy the app.py and requirements.txt files. Now, if we build this Docker image and use it to create a Docker container, we will certainly be able to locate the two files in the files system of the Docker container.

The example below will build a Docker image based on the Dockerfile above.

~/my-app$ Docker build -t new-image .

build a Docker image based on the Dockerfile

Once we have an image, we will create a Docker container using the docker run command. In addition to this, we will also launch bash within the container.


~/my-app$ Docker run -it new-image bash

If we list the files within the /var/www directory, you should see both the app.py and the requirements.txt files.


root@841d1e8d8c25:/var/www# ls
app.py  new_directory  requirements.txt

In the Docker container above, both the ADD and COPY commands have allowed us to copy files from the host’s directory to the Docker’s directory.

However, when performing such basic copying of files into the Docker container, we recommend using the COPY command.

According to the docker-file best practices, the COPY command is more appropriate when we don’t require more functionality than copying local files.

The ADD command, on the other hand, has more functionality. For instance, you can use this command to extract a local tar file into a Docker image.

In addition to this, the ADD command has support for remote URLs, and these two operations are not possible with the COPY command. The former command may not be desirable if you’re trying to reduce the size of the Docker image you’re building.

This is because the ADD command may significantly inflate the size of your Docker image, especially if it is fetching packages from remote URLs.

Using the ADD command to merely copy files from the host can lead to files being copied unexpectedly into the filesystem of your Docker image.

In conclusion, while these two commands share similarities and can be used interchangeably, you should stick to using the COPY command. On the other hand, the ADD command may only be used when needed and with utmost care.

Related Article - Docker Command

  • Docker Build Command With Multiple Arguments
  • Difference Between CMD and ENTRYPOINT in Docker