Docker COPY vs Docker ADD?

Since the inception of the docker, it has undoubtedly eased the application development for developers like us. With Docker, you no longer bounded with Operating system, architecture, frameworks, and compatibility.

With docker you can pretty much prepare your development environment with required libs and avoiding unnecessary stuffing of other libraries.

But with growing popularity of docker some docker commands makes me confuse such COPY and ADD

In the nutshell both COPY and ADD commands behave the same but still I felt a significant difference between these commands. Here is the Official Guide from docker.

But lets take a deep dive into these commands for more better understanding

Docker Copy Docker Add
Is command syntax same YES YES
Copy multiple files YES YES
Copy multiple files in one layer YES YES
Copy files from URL NO YES
Copy Archive file (.gz, .tar) NO YES



1. Syntax of Docker COPY vs Docker ADD

Speaking of syntax I would say its exactly same

COPY

1COPY hello.txt /destination-dir/

Add

1ADD hello.txt /destination-dir/

As you can see there is no difference at all between these two commands.

By the way, if you face the error - Docker COPY failed: no source files were specified Then click on the above link for troubleshooting the issue



2. Is it possible to copy multiples files?

In the step no 1 you have seen how to copy single file from source to destination. But "Is it possible to copy multiple files?"

The answer is yes, you can copy multiples files using both Docker COPY as well as Docker ADD command.

Using wild card .i.e. *

COPY - Add multiple files with name starting with hello

1COPY hello* /destination-dir/

ADD

1ADD hello* /destination-dir/

Using wild card .i.e. ?

Suppose you want to copy all the files which has name starting with "hello"

COPY

1COPY hello? /destination-dir/

ADD

1COPY hello? /destination-dir/

As you can see there is no difference at all when you try to copy single file or multiple files with similar names.



3. Does both command support multiple files in one layer

Now you might be wondering "Is there a way to copy multiple files with different names?"

I would yes its possible with the docker.

COPY

1COPY hello1.txt test1.txt jhooq.txt  /destination-dir

or

1COPY ["hello1.txt", "test1.txt", "jhooq.txt", "/destination-dir"]

ADD

1ADD hello1.txt test1.txt jhooq.txt  /destination-dir

or

1ADD ["hello1.txt", "test1.txt", "jhooq.txt", "/destination-dir"]

But the Directories are little special here

COPY

1COPY dir1 dir2 /destination-dir/

or

1COPY dir1/* dir2/* /destination-dir/

ADD

1ADD dir1 dir2 /destination-dir/

or

1COPY dir1/* dir2/* /destination-dir/

So again there is no difference if you would like to copy multiple files or directory using both the commands



4. Can you copy files from URL?

In the previous steps you have seen how to copy the files and directories from your host system which are available to you locally.

But is it possible to copy the files from URL ?

Hmm now things will get interesting for your because here comes the winner and which is Docker ADD

ADD

1ADD https://jhooq.com/hello.txt /destination-dir/

You can not do the same with Docker COPY command



5. Can you copy and extract archive files .tar.gz?

Alright now lets go a little more further.

Can we copy and extract archive files such as .tar.gz ?

Well yes, we can copy as well as extract the archive files using Docker ADD command.

ADD

1ADD hello.tar.gz /destination-dir/

The ADD the command will COPY the content to the destination-dir as well as it will extract the content of the files.

So here Docker ADD wins over COPY.

What are the best practices and recommendations?

After going through all the points I could make a guess that you would prefer to use ADD over COPY.

But wait Docker community doesn't promote Docker ADD over Docker COPY. It is always preferred to use Docker COPY over Docker ADD.

The reason is "Extra overhead for process when you use Docker ADD" because behind the scene it is trying to copy the file as well as extract the file, so which ultimate leads towards the poor performance.

If you want to download the archive file from the URL then you could use wget or curl along with tar -xjf to extract it.

1ADD http://jhooq.com/hello.tar.bz2 /destination/
2RUN tar -xjf /destination/hello.tar.bz2 \
3  && make -C /destination/package \
4  && rm /destination/hello.tar.bz2

Conclusion

  1. Although Docker ADD has more feature but sometimes it hard to guess what happened during Docker ADD because it automatically extracts the archives files.
  2. If there is need to COPY the files from URL then Docker ADD is definitely going to be my first choice but I could use wget or curl instead
  3. But on the other hand Docker COPY makes more sense by its name and what it does.

Finally my recommendation would always be to use Docker COPY where ever its possible.

Posts in this Series