• Post author:
  • Post category:Docker
  • Post last modified:May 9, 2024
  • Reading time:13 mins read

What is the difference between copy and add commands in a dockerfile? Docker has revolutionized the way we develop, deploy, and manage applications by providing a platform for containerization. Central to the containerization process is the Dockerfile, a script-like configuration file that contains instructions for building a Docker image.

Advertisements

Among the various Docker commands available in a Dockerfile, two frequently used but sometimes misunderstood commands are ‘COPY’ and ‘ADD’. In this article, we will delve into the differences between COPY and ADD commands, their use cases, and best practices.

In Docker, both COPY and ADD commands in dockerfile are used to copy files from the host to the container however, the ADD command is also used to copy the files from the URL to the container. Below are some of the additional differences between COPY and ADD commands.

AspectADDCOPY
UsageUsed for copying files and extracting compressed archives (e.g., .tar, .tar.gz)Used for copying files and directories
SyntaxADD <src> <dest>COPY <src> <dest>
Support for URLsSupports URLs as source (downloads files)Does not support URLs as source
ExtractionCan automatically extract compressed archivesDoes not automatically extract archives
PermissionsPreserves the file ownership and permissions from the sourcePreserves the file ownership and permissions from source
Local-onlyWhen used with local files, may have unintended behavior when the source is a directory with a trailing slash.Does not have the trailing slash issue for local files
Use casesUseful for adding remote files, extracting compressed archives, and copying local filesSuitable for copying local files and directories
Preserves the file ownership Use COPY for straightforward file copying, and avoid using ADD unless you specifically need its additional featuresPreferred for most file copying tasks

Both ADD and COPY can be used to copy files and directories into a Docker image, but COPY is generally preferred for straightforward file copying tasks, while ADD is better suited when you need its additional features, such as automatic extraction of compressed archives and support for URLs. However, when using ADD with local files that are directories, you should be cautious about potential issues related to trailing slashes.

Let’s look at these in detail starting with the basics of dockerfile where we use the ADD and COPY commands.

1. Dockerfile Basics

Before we delve into the ‘COPY’ and ‘ADD’ commands, let’s briefly review the purpose and structure of a Dockerfile. A Dockerfile is essentially a set of instructions that Docker uses to create a container image. It typically begins with a base image, often an official image from the Docker Hub, and then adds layers of instructions to customize the image according to your application’s requirements.

Here is a simplified example of a Dockerfile. Please read the self-explanatory comments to understand the steps.


# Use an official Python runtime as a parent image
FROM python:3.8-slim

# Set the working directory in the container
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Install any needed packages specified in requirements.txt
RUN pip install -r requirements.txt

# Make port 80 available to the world outside this container
EXPOSE 80

# Define environment variable
ENV NAME World

# Run app.py when the container launches
CMD ["python", "app.py"]

Now, let’s focus on the ‘COPY’ and ‘ADD’ commands within this context.

2. The Docker ‘COPY’ Command

The ‘COPY’ command in Docker is used to copy files and directories from the host machine into the image at the specified destination. Its basic syntax is:


# Copy files or directories from the host machine to the image
COPY <src> <dest> 
  • <src> can be either a file or a directory on the host machine.
  • <dest> is the path inside the container where the file or directory will be copied.

Here is the COPY command command works in Dockerfile:


# Copy the current directory contents into the container at /app
COPY . /app

In this case, it copies the contents of the current directory (the context where you run the docker build command) into the /app directory within the image.

Key Points About ‘COPY’:

  1. ‘COPY’ is primarily used for copying files and directories from the host to the image.
  2. It is typically used for static files, such as application code, configuration files, or assets.
  3. ‘COPY’ is more straightforward and predictable, making it suitable for most use cases.
  4. It does not have any built-in decompression or extraction capabilities.

3. The Docker ‘ADD’ Command

The ‘ADD’ command is similar to ‘COPY’ but comes with additional functionality. It can copy files and directories from the host into the image, but it also has some special features. The basic syntax of ‘ADD’ is:


# Add files or directories from the host machine to the image
ADD  <src> <dest>

Much like ‘COPY’, <src> represents the source file or directory on the host, and <dest> is the destination path within the image.

However, ‘ADD’ goes beyond simple copying:

URL Support: ‘ADD’ can fetch files or archives from URLs and automatically copy them into the image. For instance:


# Add a file or archive from a URL (http://example.com/file.tar.gz) to a specific path (/path/in/container/) within the image
ADD http://example.com/file.tar.gz /path/in/container/

This will download file.tar.gz from the specified URL and place it in the container.

Automatic Extraction: If <src> is a compressed archive (e.g., a .tar, .tar.gz, or .zip file), ‘ADD’ will automatically extract it into the destination directory in the container. For instance:


# Add and automatically extract the 'myapp.tar.gz' archive from the host machine to the '/myapp/' directory within the image
ADD myapp.tar.gz /myapp/
  1. In this case, ‘ADD’ would decompress myapp.tar.gz and place its contents in /myapp/ within the container.
  2. Local Files: Just like ‘COPY’, ‘ADD’ can also copy local files and directories.

Key Points About ‘ADD’:

  1. ‘ADD’ is a more versatile command that can handle URL downloads and automatic extraction of compressed files.
  2. It can be used when you need to copy files, and you want Docker to handle archive extraction for you.
  3. ‘ADD’ should be used with caution because its behavior can be less predictable than ‘COPY’, especially when working with URLs and compressed archives.

4. Best Practices and When to Use Docker COPY vs ADD Commands

Now that we understand the differences between ‘COPY’ and ‘ADD’, let’s discuss best practices and when to use each command.

Use ‘COPY’ for Simplicity and Transparency

  • ‘COPY’ is recommended for most use cases, especially when you want simplicity and transparency in your Dockerfile.
  • It is well-suited for copying code, configuration files, and assets.
  • When using ‘COPY’, it’s clear what files are being added to the image, making it easier to maintain and troubleshoot Dockerfiles.

Use ‘ADD’ for Advanced Scenarios

  • ‘ADD’ is suitable for more advanced scenarios where you need to download files from the internet or automatically extract archives.
  • It can be helpful when dealing with compressed files, but use it judiciously, and be aware of its automatic extraction behavior.
  • If you need to download files from the internet within your Dockerfile, ‘ADD’ is the way to go.

Be Mindful of Security

Regardless of whether you choose ‘COPY’ or ‘ADD’, security should be a top priority. Be cautious about what you include in your image, especially when fetching content from the internet. Make sure to validate the sources of files and consider using checksums or digital signatures for added security.

Minimize the Number of Layers

Each ‘COPY’ or ‘ADD’ instruction creates a new layer in the Docker image. While this doesn’t have a significant impact on the final image size, it can affect build times and image management. Therefore, it’s a good practice to combine multiple ‘COPY’ or ‘ADD’ commands whenever possible to minimize the number of layers.

5. Conclusion

In summary, both ‘COPY’ and ‘ADD’ commands in a Dockerfile serve the purpose of copying files and directories into a Docker image. ‘COPY’ is straightforward and recommended for most use cases, providing transparency and predictability. On the other hand, ‘ADD’ offers additional features like URL support and automatic extraction, making it suitable for more advanced scenarios.

When choosing between ‘COPY’ and ‘ADD’, consider the specific requirements of your Docker image and use the command that best fits your needs. Remember to prioritize security, minimize the number of layers, and maintain a clean and efficient Dockerfile for a smoother development and deployment process.

By understanding the differences between these commands and following best practices, you’ll be better equipped to create Docker images that meet your application’s requirements efficiently and securely.