DEV Community

Ajeeth thangarasu
Ajeeth thangarasu

Posted on • Updated on

The difference between COPY and ADD in dockerfile

When I was seeing some examples for docker file image building, I came across two things of same functionalities. They are COPY and ADD instructions. This article gives the difference between ADD and COPY in dockerfile. Next it explains how similar they are, then the best practice for using the RUN instead of ADD instruction.

ADD instruction

ADD instruction is an older one, which job is to copy the file or directory from a source to destination.
The ADD instruction can also do operations such as extraction of compressed files or download from an URL path.
Here the source may be a local compressed tar file or the URL path.
If it is a tar file, it extracts the contents to the destination else if it is an URL, then it download the file and extract to the destination in a similar way.
If authentication needs for URL file we can use RUN with curl or wget to download the files.
Since, ADD degrades in performance of the docker containers, COPY is been introduced for simple job.
Syntax for ADD:

ADD <src> <dest>

A simple example to ADD used for local tar file called source.tar.xz to the destination folder /dest.

ADD sourcefile.tar.xz /dest

This syntax example gives how the URL path called https://example.com/source.tar.xz file is downloaded and copied to the /dest.

ADD https://example.com/source.tar.xz /dest

COPY instruction

COPY instruction copies the file or directory from a source path to the destination.
Its job is simple as it duplicates the source file or directory to the destination.
It doesn't include the operation of extraction or downloading files as ADD instruction.
Syntax for COPY:

COPY <src> <dest>

An example to COPY a file called source.txt to the destination folder /dest.

COPY sourcefile.txt /dest

What to use either COPY or ADD?

In dockerfile we use COPY many time because it only copies the file from a source to destination.ADD used when there is a purpose such as local tar files or URL file source.
For best docker practice, we can use RUN instruction instead of creating an extra layer.
RUN instruction with curl or wget used to get the file direct into the destination.
The RUN instruction will execute any commands in a new layer on top of the current image and commit the results. The resulting committed image will be used for the next step in the Dockerfile.

ADD https://example.com/source.tar.xz /usr/src/things/
RUN tar -xJf /usr/src/things/source.tar.xz -C /usr/src/things

For instance take the above sentence, where a file called source.tar.gz is downloaded using ADD and extracted using tar with RUN instruction.
This makes two layers that worse the docker performance.
It simply done using RUN with curl tool as follows within a single layer.
If, there is direct copy operation needed than COPY instruction is recommended.
The below dockerfile only takes single layer of operation of file download using curl and then extraction of file using tar with RUN instruction.

RUN mkdir -p /usr/src/things \
    && curl -SL https://example.com/source.tar.xz \
    | tar -xJC /usr/src/things 

Reference to the Best docker practices -> go.
For my docker archives click here -> go.
If any queries please comment it.

Top comments (0)