Categories
tech

Shall I Use ADD or COPY within the Dockerfile – What’s the Difference?

Author profile picture

Every developer and each staff faces confusion about 

COPY

 and 

ADD

 within the Dockerfile in the future. When I am getting this query, first I generally give the technical background, which is that this:

Both 

ADD

 and 

COPY

 replica recordsdata and directories from the host system right into a Docker symbol, the adaptation is that 

ADD

 too can extract and replica native tar archives and it could additionally download recordsdata from URLs (a.okay.a. the web), and replica them into the Docker symbol. The easiest apply is to make use of 

COPY

.

So 

COPY

 equals 

ADD

 minus the unpacking and URL fetching options.

COPY

 is the most popular approach, apart from for those who unpack a neighborhood tar archive right into a Docker symbol and you might be sure that the native archive has the correct layout.

You can perceive why that is the case taking a look at some background information. Read on…

Why COPY is most popular

The core objective of 

ADD

 and 

COPY

 is to let Dockerfile builders replica recordsdata and directories from the host system into the Docker symbol right through symbol construct.

Extracting archives and downloading recordsdata from the web are commonplace use-cases, those options are constructed into 

ADD

.

The uncompression characteristic is described within the respectable documentation as follows:

If <src> is a neighborhood tar archive in a identified compression layout (identification, gzip, bzip2 or xz) then it’s unpacked as a listing.

The following word at the similar web page additional explains the habits:

Note: Whether a record is recognized as a identified compression layout is completed only in line with the contents of the record, no longer the identify of the record. For instance, if an empty record occurs to finish with .tar.gz this might not be identified as a compressed record, and won’t generate any more or less decompression error message, slightly the record will merely be copied to the vacation spot.

This implies that your ultimate result depends upon the contents of the record you propose to replicate, and you do not get warnings if one thing is going flawed. This would possibly make your construct pipeline unpredictable.

To make existence extra dependable, now we have the COPY instruction, which is “the similar as ADD, however with out the tar and URL dealing with”.

COPY

does one factor and it does it smartly.

The easiest apply

Docker easiest practices counsel to all the time use 

COPY

 whilst you are not looking for extraction capability, as a result of 

COPY

 is extra clear.

In real-life initiatives 

COPY

 is enough in maximum eventualities, principally as a result of we hardly upload tarballs to our programs’ supply code. The primary use-case for tarballs, thus 

ADD

, is once we create a base symbol from a tar archive. This does not occur very steadily. In this example 

ADD

 is most popular.

For all different use instances, we use 

COPY

;

  • We desire 
    COPY

     for copying recordsdata from the host system right into a Docker symbol.

  • We use 
    RUN

     with 

    curl

     or 

    wget

     to fetch recordsdata from URLs. 

    ADD

     does no longer unpack recordsdata from the internet anyway, so we’re at an advantage averting it solely.

Let’s see how you’ll be able to accomplish unpacking and URL fetching.

Unpacking native archives

ADD

 unpacks archives from the host system, it does no longer unpack recordsdata from URLs. To unpack an archive you simply use it in its default shape; 

ADD <src>... <dest>

. Check out this pattern Dockerfile:

FROM alpine:3.10 ADD bigfile.tar.xz /tmp/

When you construct the picture Docker will unpack the archive.

docker construct -t yourname/alpine-bigfile .
Sending construct context to Docker daemon 4.096kB
Step 1/2 : FROM alpine:3.10 ---> 4d90542f0623
Step 2/2 : ADD bigfile.tar.xz /tmp/ ---> 32cfa3eb41f7
Successfully constructed 32cfa3eb41f7
Successfully tagged yourname/alpine-bigfile:newest

Since the layout of 

ADD

 is the very same whilst you simply replica a record otherwise you unpack an archive, this may get difficult. As we discussed previous, if Docker does no longer acknowledge the archive layout right through the construct, it’s going to replica the archive as it’s into the Docker symbol with out caution. You can mitigate the hazards by means of including a test into your construct pipeline.

Our archive within the instance was once identified by means of Docker, so the record is uncompressed in our symbol:

docker run --rm -ti yourname/alpine-bigfile /bin/ash
/ # ls -al /tmp
general 12
drwxrwxrwt 1 root root 4096 Jan 31 09:49 .
drwxr-xr-x 1 root root 4096 Jan 31 09:50 ..
-rw-r--r-- 1 501 dialout 29 Jan 31 09:46 bigfile

If you wish to have a strategy to proportion your symbol as an archive, take a look at our article How to Transfer/Move a Docker Image to Another System?.

Downloading and unpacking archives from a URL

For downloading and unpacking archives from the web 

curl

 or 

wget 

are the easier choices, as it takes most effective one symbol layer to get the consequences you wish to have. With 

ADD

 you would seize the archive first in one layer, then uncompress it with 

RUN

 in any other. This isn’t so environment friendly.

You can construct a Dockerfile to 

curl

 an archive and uncompress it like proven beneath.

FROM alpine:3.10 RUN apk upload --no-cache curl &&  curl -SL https://github.com/yikaus/docker-alpine-base/uncooked/grasp/rootfs.tar.xz | tar -xJC /tmp

This takes one symbol layer and you have got complete keep watch over over the method.

One thing more

One extra noteworthy distinction between 

ADD

 and 

COPY

 is that

COPY

has the 

--from=<identify|index>

 flag that permits you to replica recordsdata from a prior construct degree in a multi-stage construct. 

ADD

 does no longer have this feature.

This is one more reason to make use of 

COPY

 as your most popular choice.

This article was once at the beginning revealed on https://appfleet.com/weblog/shall-i-use-add-or-copy-in-the-dockerfile/.

Tags

The Noonification banner

Subscribe to get your day-to-day round-up of best tech tales!