Development

What is the Difference Between Copy and Add Commands in Dockerfile

Captain Salem 2 min read

What is the Difference Between Copy and Add Commands in Dockerfile

Copying files in Docker is a pretty common tasks for any Docker user. However, when composing a Dockerfile, you might come across the ADD and COPY commands both of which allow you to copy files from a source within the local fs into a Docker image.

Have you ever wondered what is the difference between this two commands? Well, in this tutorial, we will explore what is the main difference between them and why you might use over the other.

Dockerfile Add vs Copy Commands

The following simply explains the difference between the ADD and the COPY commands:

TheCOPY: The COPY command in in a Dockerfile allows you to copy new files or directories from <src> to the filesystem of the container at the path specified in the<dest> parameter. If the path provided in the src parameter is a relative path, Docker will consider it as relative to the source of the context of the image build. Hence, no download or extraction is supported.

The ADD command, on the other hand,, does what COPY does but also provides the ability to:

  1. Download a file from a URL at the build time.
  2. Automatically extract a tarball at the destination.

Example Demonstration

To better understand how both of these commands work, let us look at some basic examples.

Example Copy Command Usage

The following shows a section of a Dockerfile using the copy command to copy a file from source to destination.

FROM ubuntu:18.04
COPY ./config.ini /config.ini

In this case, the Dockerfile will copy the local file config.ini into the new image at location /config.ini.

Example Add Command Usage

Consider the example usage of the add command:

FROM ubuntu:18.04
ADD http://dl.geekbits.io/logs.tar.gz /opt/custom/logs

In this case, the Dockerfile will download logs.tar.gz from the provided URL and it store it in /opt/custom/logs directory.

The command will also extract the tarball into that directory instead of storing it as an archive.

Point of Order

Although it may seem like the add command is more favorable over the copy command due to the added functionality, we recommend using the copy command for file and directory operations. If you need to download or extract archive later in the Dockerfile, you can use other commands such as RUN wget and RUN tar as neccessary. This is because the more features of the add command can lead to unexpected results or failure when downloading and extracting archives.

Conclusion

In this tutorial, we learned the main difference between the COPY and the ADD commands in the context of a Dockerfile. We also looked at examples of how we can use both commands and when one is more favorable over the other.

Happy containerization!!!

Share
Comments
More from Cloudenv

Cloudenv

Developer Tips, Tricks and Tutorials.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Cloudenv.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.