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.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github