Docker ADD vs. COPY: What are the Differences?

By | June 21, 2022
Docker ADD vs. COPY: What are the Differences

When building Dockerfiles, it is common to move files from the host system into the Docker image. Examples include property files, native libraries, and other static information that our applications may need at runtime.

The COPY and ADD directives in the Dockerfile specification give two ways to copy files from the source system into an image. Both instructions have the same fundamental structure and accomplish the pretty same goal:

ADD <src>… <dest>

COPY <src>… <dest>

Directories or files get copied and added to the container’s filesystem at the given path in both scenarios. Although the extent of their functions differs slightly, they fundamentally execute the same purpose. Let’s look through the blog to discover them and their differences along with deciding which one is best.

What is Docker?

Docker is a containerization platform that is free and open-source, allowing developers and organizations to bundle applications into containers. These containers are standard deployable pieces that combine application source code with the OS libraries and dependencies needed to run that code in any environment. More and more businesses are switching to cloud-based development and hybrid multi-cloud environments, making it easier to deploy dispersed applications.

An individual can build containers without Docker as well, but the platform makes building, deploying, and managing containers easier, simpler, and secure. Docker is a platform that allows developers to use a single API to build, deploy, operate, update, and stop containers using simple commands and work-saving automation.

Features of Docker

Enhanced and flawless portability: Unlike traditional containers, Docker containers run without modification on any desktop, network infrastructure, or cloud environment.

Lightweight containers with granular updates: Docker container allows the functioning of only one process in one container. This enables individuals to create an application that can keep operating while one of its components is updated or repaired.

Automatic container formation: Docker can construct a container based on the source code of an application automatically. This saves humans from doing lots of manual work.

Container versioning: Docker can track several versions of a container image, roll back to earlier versions, and monitor who produced which version and how. It can even upload simply the differences between an old and new version.

Container reuse: Existing containers can be utilized as foundation images, effectively serving as templates for creating new containers.

Shared container libraries: Users may utilize an open-source registry composed of thousands of user-contributed containers with the help of shared container libraries.

Docker ADD Command

ADD command predates the COPY command. This command has been included in the Docker platform’s command set since its introduction. It is used for transferring files and directories to the given container’s file system. The ADD command has the following basic syntax:

ADD <src> … <dest>

It starts with the resource you would like to copy (src>) and ends with the location you want to save it to (dest>). ADD copies everything inside a directory if the source is a directory (including file system metadata). For example, if a file is available locally and you wish to add it to an image’s directory, type:

ADD /source/file/path /destination/path

In addition, ADD may copy files from a URL. It can download a file from the internet and copy it to the desired location. Consider the following scenario:

ADD http://source.file/url /destination/path

Another advantage is that it replicates compressed files, extracting the information in the specified location automatically. This feature is only accessible for locally stored compressed files and folders.

Simply enter the source and the location in which the content should be extracted:

ADD source.file.tar.gz /temp

Please remember that you can’t retrieve and unzip a zipped file or directory using a URL. When copying foreign packages to the local filesystem, the command does not unpack them.

Docker Copy Command

Docker has to add an extra command for copying content – COPY – due to certain functionality problems in the ADD directive. Unlike the ADD command, COPY has only one function allocated to it. Its job is to replicate files/directories in their original format in a given place. This implies it doesn’t deal with extracting compressed files; instead, it copies them as-it-is.

Only locally saved files can be utilized with this instruction. As a result, you can’t use it to copy external files to your container using URLs. Follow the basic command format to utilize the COPY instruction:

COPY <src> … <dest>

For instance:

COPY /source/file/path /destination/path

Docker ADD vs. COPY: The Difference

Both ADD and COPY commands copy files and directories from the host computer into a Docker image; the difference is that ADD can also extract and copy local tar archives, as well as download and copy files from URLs (a.k.a. the internet).

The fact that ADD had so many functions proved to be an issue in practice since it acted in unpredictable ways. Therefore, the most effective method is to utilize COPY. Because of ADD inconsistency, it was often necessary to copy when you intended to extract and extract when you wanted to copy.

Due to the command’s numerous uses, Docker was unable to entirely replace it. To prevent backward compatibility, the safest choice was to add the COPY command — a less versatile but more trustworthy command.

Best Practices

Given the circumstances surrounding the introduction of the COPY command, it seems clear that maintaining ADD was a requirement. Docker published an official guide detailing recommended practices for developing Dockerfiles, which specifically states that the ADD command should ‘not’ be used.

As mentioned in Docker’s official documentation, COPY must be executed instead of ADD because it is more explicit. If you need to copy something from the local build context to containers, use COPY. The Docker team also strongly advises against the use of ADD to download and copy a package from a URL. It’s safer and more economical to use wget or curl within a RUN command instead. You will also save storage by avoiding the creation of an additional picture layer.

For downloading a zipped package through URL, retrieve the contents, and then clean up the archive, for example. Instead of using the ADD command, you should use the following command:

RUN curl http://source.file/package.file.tar.gz \

| tar -xjC /tmp/ package.file.tar.gz \

&& make -C /tmp/ package.file.tar.gz

The —from=name|index> flag in transfer allows you to copy files from a previous build stage in a multi-stage build, which is also one of the critical disparities between ADD and COPY. This feature is not provided with the ADD command.

Download and unpack archives from a URL without ADD

Curl or wget are superior alternatives for obtaining and unpacking archives from the internet since they only require one picture layer to get the desired results. You’d use ADD to retrieve the archive in one layer, then RUN to uncompress it in another, which is not an effective and secure method.

As mentioned below, you may create a Dockerfile to curl and uncompress an archive.

FROM alpine:3.10

RUN apk add –no-cache curl && \

curl -SL | tar -xJC /tmp

You just need one picture layer for this, and you have complete control over the process.


Docker is a free and open-source containerization platform that enables developers and organizations to bundle applications into containers. For moving files from the host system into the Docker image while building Dockerfiles, along with property files, native libraries, and other static information, an individual has to make use of either ADD or COPY commands.

In the core, both commands have similar functionality- to copy files from the source system to an image. Nevertheless, ADD was largely unpredictable in practice due to its numerous functions. Therefore, COPY is the most effective method. We have covered all the issues in detail for individuals to fully understand how they differ as well as how to deal with them.

Leave a Reply

Your email address will not be published.