Difficulty: Beginner
What is the difference between COPY and ADD in a Dockerfile, and when should you use ADD instead of COPY?
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.
| Capability | COPY | ADD |
|---|---|---|
| Copy local files from build context | Yes | Yes |
Copy from another build stage (--from) | Yes | No |
Auto-extract local .tar, .tar.gz, .tar.xz | No | Yes |
| Download from an HTTP(S) URL | No | Yes, not extracted |
| Clone a Git repository | No | Yes, with BuildKit |
| Verify remote file checksum | Not applicable | --checksum=sha256:... |
--chown, --chmod, --link | Yes | Yes |
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.ADD https://.../file.tar.gz /x/ will unpack it. It will not; only local archives are extracted.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.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.ADD --checksum=sha256:<hash> <url> <dest>.git in the image, using ADD https://github.com/org/repo.git#v1.2.0 /src.ADD everywhere out of habit from old tutorials.COPY ../shared /app fails because paths outside the context are not sent to the builder..dockerignore, so COPY . . drags in node_modules, .git and .env.USER; use COPY --chown=app:app so the app user can write where it needs to.# 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.
COPY, ADD, Build Context, Tar Auto-extraction, Remote URLs