0
0
DockerConceptBeginner · 3 min read

What is ADD in Dockerfile: Simple Explanation and Usage

The ADD instruction in a Dockerfile copies files, directories, or remote URLs from your build context into the image. It can also automatically extract compressed files like tar archives when added. ADD is similar to COPY but has extra features like unpacking archives and fetching remote files.
⚙️

How It Works

The ADD command in a Dockerfile works like a helper that moves files from your computer (called the build context) into the Docker image you are building. Imagine packing a suitcase: ADD helps you put clothes (files) inside the suitcase (image) so they are ready to use later.

Besides just copying, ADD can also unpack compressed files like .tar archives automatically. This is like unpacking a zipped bag of clothes directly into your suitcase without extra steps. Additionally, ADD can download files from the internet if you give it a URL, saving you the step of downloading manually before building.

However, because ADD does extra things, it can sometimes cause unexpected results or bigger images. For simple copying, Docker recommends using COPY instead.

💻

Example

This example shows how ADD copies a local file and unpacks a tar archive into the image.

dockerfile
FROM alpine:latest

# Add a local file into /app inside the image
ADD hello.txt /app/hello.txt

# Add and unpack a tar archive into /app/data
ADD data.tar.gz /app/data/
🎯

When to Use

Use ADD when you need to copy files into your Docker image and want to automatically unpack compressed archives or download files from URLs during build. For example, if you have a tarball of configuration files, ADD can unpack it directly inside the image.

However, if you only need to copy files or folders without extra features, prefer COPY because it is simpler and more predictable. Use ADD only when its extra features are necessary.

Key Points

  • ADD copies files, directories, or URLs into the image.
  • It can automatically unpack compressed files like tar archives.
  • It can download files from remote URLs during build.
  • For simple copying, COPY is recommended instead.
  • Using ADD may increase image size if not used carefully.

Key Takeaways

ADD copies files and can unpack archives or fetch URLs into the Docker image.
Use ADD only when you need its extra features like auto-extract or remote download.
For simple file copying, prefer COPY for clarity and smaller images.
ADD helps automate file preparation inside images during build.
Be mindful that ADD can increase image size if used with large archives.