COPY vs. ADD

Difficulty: Beginner

Question

What is the difference between COPY and ADD in a Dockerfile, and when should you use ADD instead of COPY?

Answer

COPY copies files and directories from the build context (or another stage) into the image and does nothing else. ADD does the same but also has extra behaviour: it auto-extracts local tar archives and can fetch remote URLs and Git repositories. Docker's own best practice is to use COPY by default and reach for ADD only when you specifically need one of those extra features.

What each instruction does

CapabilityCOPYADD
Copy local files from build contextYesYes
Copy from another build stage (--from)YesNo
Auto-extract local .tar, .tar.gz, .tar.xzNoYes
Download from an HTTP(S) URLNoYes, not extracted
Clone a Git repositoryNoYes, with BuildKit
Verify remote file checksumNot applicable--checksum=sha256:...
--chown, --chmod, --linkYesYes

Why COPY is the default

  • Predictability: COPY archive.tar.gz /app/ copies the archive as-is. ADD archive.tar.gz /app/ silently unpacks it. Someone reading the Dockerfile can be surprised by ADD.
  • Remote URLs are not extracted: a common misconception is that ADD https://.../file.tar.gz /x/ will unpack it. It will not; only local archives are extracted.
  • Cache and supply chain: downloading with ADD pulls content at build time from a URL that can change. Without --checksum, you get no integrity check, so builds may not be reproducible.
  • Layer control: if you download an archive with ADD and then extract and delete it in a later RUN, the original archive is still stored in the earlier layer. Doing RUN curl -fsSL ... | tar -xz in a single step keeps the image smaller.

When ADD is the right choice

  1. You have a local tarball, such as a vendored root filesystem or prebuilt binaries, and want it extracted into the image in one step.
  2. You need a remote file and want BuildKit to verify it with ADD --checksum=sha256:<hash> <url> <dest>.
  3. You want to pull a Git repository at a specific ref without installing git in the image, using ADD https://github.com/org/repo.git#v1.2.0 /src.

Common mistakes

  • Using ADD everywhere out of habit from old tutorials.
  • Forgetting that both instructions only see files inside the build context; COPY ../shared /app fails because paths outside the context are not sent to the builder.
  • Forgetting .dockerignore, so COPY . . drags in node_modules, .git and .env.
  • Copying files as root and then switching USER; use COPY --chown=app:app so the app user can write where it needs to.

Code examples

COPY for normal files, ADD for its special cases

# syntax=docker/dockerfile:1
FROM alpine:3.20
WORKDIR /app

# Default: plain copy with correct ownership
COPY --chown=1000:1000 package.json ./
COPY --chown=1000:1000 src/ ./src/

# ADD auto-extracts a LOCAL tarball into the target directory
ADD vendor/tools.tar.gz /opt/tools/

# ADD a remote file with integrity verification (not extracted)
ADD --checksum=sha256:<expected-sha256> \
    https://example.com/releases/cli-linux-amd64 /usr/local/bin/cli

# Alternative to ADD URL: download and extract in one layer
RUN wget -qO- https://example.com/releases/app.tar.gz | tar -xz -C /opt

COPY handles everyday files. ADD is used only for local archive extraction or a checksum-verified remote file, and a single RUN pipeline avoids storing the downloaded archive in a layer.

Key points

  • COPY is the default; use ADD only for local tar extraction, remote URLs or Git sources.
  • ADD extracts local archives but does not extract files downloaded from URLs.
  • Use ADD --checksum for remote files so builds are verifiable.
  • Only COPY supports --from to copy between build stages.
  • Both can only read files inside the build context.

Common follow-up questions

  • Why is COPY preferred over ADD for most cases?
  • How do you keep image layers small when downloading and extracting files?
  • Does ADD extract a .tar.gz downloaded from a URL?
  • What is the build context and how does .dockerignore affect COPY?

Interview tips

  • Give the rule (COPY by default) and then the exact three cases where ADD is justified; precision impresses more than a vague 'ADD has magic'.
  • Mention that remote archives are not auto-extracted; it is a common trick question.
  • Bring up reproducibility and --checksum to show supply chain awareness.

Concepts covered

COPY, ADD, Build Context, Tar Auto-extraction, Remote URLs

More Docker & Containers questions