1. Introduction

Aside from the COPY directive, we can use ADD when creating a Dockerfile to transfer folders from our local machine to a Docker image.

This command can also function in different ways, which include automatically extracting local compressed files and downloading files from remote sources to the Docker image.

However, in this article, we’ll focus on using the ADD directive to copy folders to a Docker image.

2. How Does Dockerfile ADD Work?

Basically, to copy folders from our host into a Docker image, we need to specify the folder path as the source and the destination path it drops within the Dockerfile.

Let’s examine the basic syntax for the ADD directive:

ADD <src> <dest>
  • : specifies the source path of the file or directory to be transferred. It could be a relative path, an absolute path, or a URL
  • : specifies the destination path in the Docker image’s filesystem where the file or directory moves to

Understanding this syntax helps not only to successfully copy files but also to easily organize files and directories in our Docker image.

3. Copying a Folder to Docker Image Using Dockerfile ADD

Generally, to effectively copy a local folder into our Docker image, we need to ensure that the folder is in the same directory as the Dockerfile. This is because the docker build command is run where the Dockerfile resides since Docker uses it as a build context.

Let’s show an example of what a simple file tree should look like:

package/
├── Nginx-App/
│   ├── Dedicated-to-Amara.txt
│   ├── index.html
├── Dockerfile

From this example, we can see that package is the parent directory, while Nginx-App folder and Dockerfile are objects in it.

In addition, keeping the folder in the same directory as the Dockerfile ensures an easier operation. This is because we can reference the entire folder through a relative path when using the Dockerfile ADD directive.

Now, let’s edit our Dockerfile:

$ nano Dockerfile

Next, let’s copy our folder into the Docker image through the Dockerfile:

# Base Image
FROM nginx:alpine

# Adds the Nginx-App directory to the NGINX document root
ADD Nginx-App/ /usr/share/nginx/html/Nginx-App

# Exposes port 80
EXPOSE 80

# Starts Nginx
CMD ["nginx", "-g", "daemon off;"]

Essentially, the ADD directive used in line 2 copies the Nginx-App folder from the parent directory into /usr/share/nginx/html/ in the Docker image.

Now, let’s run the docker build command with the parent directory:

$ docker build -t nginx-image .

This command tells Docker to build an image using the instructions in the file. Once the process is complete, we run the following command to verify:

$ docker images                
REPOSITORY                 TAG       IMAGE ID       CREATED              SIZE
nginx-image                latest    40b1d9d4a8a5   About a minute ago   43.2MB

From the output of the command, we successfully built the image. Now, let’s verify that the folder is in the image:

$ docker run -it --rm nginx-image /bin/ash

This command starts a new container from the nginx-image and opens an ash shell within. Additionally, note that the ash shell is relevant here because the image used is based on the Alpine Linux distribution.

Next, we check for the folder:

$ docker run -it --rm nginx-image /bin/ash
/ # cd /usr/share/nginx/html/Nginx-App
/usr/share/nginx/html/Nginx-App # ls
Dedicated-to-Amara.txt  index.html 

Evidently, the folder and its entire content were copied to the Docker image.

4. Copying a Compressed Folder

We can also copy a local compressed folder into a Docker image using the same logic. For these types of files, ADD will automatically extract their contents to the specified destination within the image.

This automatic extraction speeds up and simplifies the process.

For example, let’s copy a tar folder into a Docker image:

# Base Image
FROM nginx:alpine

# Adds the compressed Nginx-App folder to the NGINX document root
ADD Nginx-App.tar.gz /usr/share/nginx/html/Nginx-App/

# Exposes port 80
EXPOSE 80

# Starts Nginx
CMD ["nginx", "-g", "daemon off;"]

Then, run the build image command:

$ docker build -t nginx-image .           
Sending build context to Docker daemon  5.632kB
Step 1/4 : FROM nginx:alpine
 ---> 1ae23480369f
Step 2/4 : ADD Nginx-App.tar.gz /usr/share/nginx/html/Nginx-App/
...
Successfully built c6f60de9c196

Clearly, we’ve successfully added the tar folder to the Docker image. We can verify using the same method as above.

5. Conclusion

In this article, we covered all methods of using the ADD directive to copy folders into our image. These methods are efficient and easy to use.

However, it should be used with caution, especially when copying compressed folders to the image. This is because the ADD command automatically expands compressed formats and can introduce unwanted files in the Docker environment.