DevOps

What is the Docker ADD and Copy Command? Explain the Differences

Introduction

If you have ever used paint color sets that come with paint bottles, brushes, and palettes, you know how convenient it is to have everything you need in one box to complete a painting. Similarly, Dockers provides a container that bundles all the important utilities required to run specific software. This makes it easy to move the application from one computer or server to another without worrying about compatibility issues.

This article focuses on the Docker add command and the Docker copy command, explaining the differences between the two commands in an easy-to-understand language. We have done our best to make it clear and informative. 

Docker ADD Command

When building a Docker container, the Docker ADD command is used to copy files and directories from your computer’s filesystem into the container. This command helps you add files from your local machine to the container during the build process. To use the ADD command, you need to write it in your Dockerfile, followed by the source (file or directory on your computer) and the destination (where it should be placed in the container). Essentially, ADD takes files or directories from your computer and adds them to the Docker image you’re creating. It’s a simple way to transfer files from your local machine to a Docker container. Let’s have a look at the syntax below. 

ADD <source> <destination>

You can use the ADD Docker command to copy files from a file or directory on your system to a path within a container. If the <destination> path does not exist, Docker will create it for you. The ADD command also lets you download files from URLs and automatically decompress certain file types.

Docker Copy Command

When building a Docker container using a Dockerfile, the COPY command is used to transfer files and directories from your local machine’s filesystem to the Docker container’s filesystem. The COPY command is specifically designed to copy files or directories from your computer to the Docker container during the build process. In your Dockerfile, you should use the COPY command, followed by the source (the file or directory on your computer) and the destination (the location where it should be placed in the container).

COPY <source> <destination>

<source> can be a file or a directory on your computer, while <destination> is the path within the container. If the <destination> path does not exist, Docker will create it. Unlike the ADD command, COPY is explicitly used for copying local files into the container and doesn’t perform URL fetching or automatic extraction.

Differences between Docker Copy vs ADD

Differences between Docker Copy vs ADD

Both COPY and ADD are Dockerfile commands used to copy files and directories into a Docker container, but they have some key differences:

COPY:

Basic Functionality: 

COPY is straightforward and used specifically for copying local files and directories from the Docker host into the container.

Syntax: 

COPY <src> <dest>

Behavior: 

It copies files from the host machine to the container, and it’s specifically meant for copying local files into the image. It doesn’t extract or unpack the files.

Usage: 

Ideal for straightforward file copying, where you just need to copy files from the host system into the container without any processing or modification.

COPY ./src/app /app

ADD:

Enhanced Functionality: 

The Docker ADD command does everything COPY does, plus additional functionalities like extracting TAR files and remote URL support.

Syntax: 

ADD <src> <dest>

Behavior: 

It can do more than just copy files—it can also retrieve files from remote URLs and unpack compressed files (such as tar files) into the destination directory.

Usage: 

Suitable when you need more advanced functionalities like fetching remote resources or automatically unpacking compressed files during the build process.

ADD https://example.com/archive.tar.gz /destination/

It is advisable to use the Docker COPY command when you only need to copy local files or directories into the container. Reserve the use of the ADD command for situations where you require its extra features, such as unpacking or remote URL support. COPY has better caching benefits since it only copies files, whereas ADD performs more complex operations that may cause caching issues, resulting in prolonged build times.

Also Read: How to Kill a Process in Linux Command

Conclusion 

As a software developer, you might need to configure your applications with specific settings and dependencies to ensure they run smoothly. Docker provides a solution whereby you can package your software together with all necessary components like code, libraries, tools, and configurations in a standardized container. When it comes to copying files into the container, the Docker COPY command is more straightforward and transparent in its purpose, making it a better option than ADD for clarity in Dockerfiles. ADD, on the other hand, comes with added features like handling remote URLs and unpacking compressed files, which can be useful in certain instances. However, it is essential to use the Docker ADD command only when these additional features are necessary, as it can cause caching issues and longer build times due to its more complex operations.

Arpit Saini

He is the Chief Technology Officer at Hostbillo Hosting Solution and also follows a passion to break complex tech topics into practical and easy-to-understand articles. He loves to write about Web Hosting, Software, Virtualization, Cloud Computing, and much more.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

[sc name="footer"][/sc]