Cloud Native 4 min read

Understanding Dockerfile ADD Instruction: Syntax, Benefits, Drawbacks, and Practical Usage

This article explains Docker's ADD instruction, covering its syntax, automatic archive extraction, advantages and disadvantages, detailed usage rules for files and directories, and provides a concrete Dockerfile example with build and run commands.

Practical DevOps Architecture
Practical DevOps Architecture
Practical DevOps Architecture
Understanding Dockerfile ADD Instruction: Syntax, Benefits, Drawbacks, and Practical Usage

The ADD instruction copies files, directories, or URL‑referenced resources from the build‑context into a Docker image.

Its basic syntax is ADD <src> <dest> , and it also supports a JSON array form: ADD ["<src>", "...", "<dest>"] .

Unlike COPY, ADD can automatically unpack compressed archives (e.g., tar.gz, tar.bz2) into the target directory, which is useful when you need the contents extracted during the build.

Advantages of ADD include automatic extraction of tar archives, simplifying image construction when you need to unpack files on the fly.

Drawbacks are that ADD cannot copy a tar archive without extracting it, which may invalidate the build cache and slow down image creation; therefore, use COPY for plain file copies whenever possible.

Key usage notes: 1. If the source is a file and the destination ends with '/', Docker treats the destination as a directory and copies the file into it; missing directories are created automatically. 2. If the destination does not end with '/', Docker treats it as a file name, creating or overwriting the file accordingly. 3. When the source is a directory and the destination does not exist, Docker creates the destination directory and copies the source contents inside. 4. If the source is an archive, Docker automatically extracts it.

Example Dockerfile using both COPY and ADD:

FROM centos7 MAINTAINER hahashen RUN yum -y install nginx COPY index.html /usr/share/nginx/html/ EXPOSE 8088 ENTRYPOINT ["/usr/sbin/nginx", "-g", "daemon off;"]

Build and run commands:

docker build -t "dockerfile/nginx:v1" . docker run -itd -p 80:8088 dockerfile/nginx:v1

When installing Nginx from source (nginx.tar.gz), use ADD to extract it directly:

ADD nginx.tar.gz /opt

--- End of article ---

DockercontainerDockerfileImageADD
Practical DevOps Architecture
Written by

Practical DevOps Architecture

Hands‑on DevOps operations using Docker, K8s, Jenkins, and Ansible—empowering ops professionals to grow together through sharing, discussion, knowledge consolidation, and continuous improvement.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.