What Is the Difference between the ‘COPY’ and ‘ADD’ Commands in a Dockerfile?

Better Stack Team
Updated on August 1, 2022

When creating a Docker file, you may need to transfer some files from the host system to the docker image. Those files may be for example libraries or any other files that your application may need at runtime.

To do that, docker provides two commands - COPY and ADD

What is the difference?

Both COPY and ADD commands allow you to add files to your docker image from a specific location. Both of these commands take to arguments <src> (location of the source file) and <dest> (copy destination).

In the case of the COPY command, it only allows you to copy files from the local machine (the machine creating the Docker image).

Whereas ADD command allows the source path to be a URL address. It can also extract tar files from the source to the destination.

Which one should you use?

It is a best practice to always prefer COPY over ADD unless you especially need one of the additional features of ADD. As mentioned above, ADD command automatically expands which can lead to unexpected wiles being written to the docker image.

Got an article suggestion? Let us know
Explore more
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

We are hiring.

Software is our way of making the world a tiny bit better. We build tools for the makers of tomorrow.

Explore all positions →

Reliability is the
ultimate feature

Delightful observability tools that turn your logs & monitoring into a secret weapon for shipping better software faster.

Explore Better Stack