The ADD instruction in Dockerfile allows you to copy files or directories from a source on your local system to a destination in the Docker image. But there’s more. Unlike the COPY command, ADD can do a few extra things. It can automatically extract compressed files and even download files from URLs.
In this guide, we will explain what is ADD in Dockerfile with practical examples.
Table of Contents
When to Use ADD vs. COPY
Both ADD and COPY serve the basic purpose of copying files from the host machine into the Docker image, but ADD offers more flexibility with extra features like handling URLs and compressed files. However, using ADD unnecessarily can lead to performance issues or unintentional behavior, so it’s crucial to know when to use each.
Use COPY when:
- You just need to copy files or directories from the local filesystem into the container. It’s faster, simpler, and clearer in intent.
Use ADD when:
- You need to download files from a URL.
- You automatically want to extract compressed files during the build process.
Basic Syntax
The syntax of ADD is straightforward.
ADD source destination
Explanation.
- Source: Specifies the source file, directory, or URL. This can either be from your local machine or a remote URL.
- Destination: Defines the destination path inside the Docker container where the content will be placed.
Now that we understand the syntax, let’s look at how to use ADD in practical scenarios.
Example 1: Copying Local Files to a Docker Container
You can use ADD to copy files or directories from your local machine to the Docker container:
ADD ./app /usr/src/app
In this example, the content of the local app directory is copied into the /usr/src/app directory inside the container. This simple use case is similar to how the COPY instruction works.
Example 2: Extracting a Compressed Archive into a Container
ADD also supports automatic extraction of compressed files like .tar.gz into the specified directory:
COPY archive.tar.gz /usr/src/app/
In this example, the archive.tar.gz file is extracted into the /user/src/app/ directory without needing to be manually extracted using a separate command. This is a powerful feature when dealing with large compressed datasets.
Example 3: Downloading a File from a URL
One of the unique features of ADD is the ability to download a file from a URL directly into the Docker image:
ADD https://example.com/file.tar.gz /tmp/
Here, the file from the URL https://example.com/file.tar.gz is downloaded and placed in the /tmp/ directory inside the container. This feature makes it convenient to fetch resources from remote servers during image build time.
Example 4: Copying Multiple Files and Directories
You can use ADD to copy multiple files and directories in one go:
ADD ./config.json ./app ./logs /var/www/
This command copies config.json, app, and logs into the /var/www/ directory inside the container. Using ADD in this way can simplify the Dockerfile by reducing the number of separate instructions.
Example 5: Copying Files with Wildcards
ADD supports wildcards, which is useful for copying multiple files with a specific pattern:
ADD ./static/* /usr/share/nginx/html/
This example copies all the files from the static folder into the /usr/share/nginx/html/ directory in the container. This feature is especially useful when you want to copy a specific subset of files into the container.
Conclusion
The ADD instruction in Dockerfile is a versatile tool. It allows you to copy, extract, and even download files directly into your Docker image. However, with great power comes great responsibility. Always consider whether you need ADD’s extra features or if COPY would be a better choice.
Experiment with ADD in your Dockerfiles and see how it can streamline your build process. Happy Dockerizing!
FAQs
1. Can ADD extract compressed files?
Yes, ADD can automatically extract compressed .tar archives into the specified directory in the image.
2. Can I use ADD to download files from a URL?
Yes, ADD supports downloading files from a remote URL and adds them to the specified path in the image.
3. Can I use ADD to copy directories in a Dockerfile?
Yes, ADD can copy entire directories from the host to the image by specifying the source directory and the destination path.
4. Can ADD handle wildcards or glob patterns?
Yes, ADD supports wildcards for matching multiple files or directories in the source path.