Sub topic 10
ADD compared with COPY
This chapter assumes you can write a basic Dockerfile and know what the build context is. Both ADD and COPY put files into an image. They look almost the same, but ADD does extra work that can surprise you.
What COPY does
COPY takes files from the build context (or from an earlier build stage with --from) and writes them into the image. It does only that. The source path is copied as it is, and the behavior is easy to predict.
COPY requirements.txt /app/What ADD adds
ADD has the same syntax and does the same copy. It also has two extra behaviors:
- Archive extraction. If the source is a local tar archive in a recognized compression format (gzip, bzip2, xz, or uncompressed),
ADDunpacks it into the destination. - Remote sources. The source can be an HTTP(S) URL or a Git repository URL.
ADDfetches it during the build. A downloaded file is not extracted, even if it is an archive.
Seeing the difference
The following commands build a small archive and add it to an image with both instructions.
mkdir demo && cd demo
mkdir data && echo "hello" > data/hello.txt
tar -czf data.tar.gz data
printf 'FROM alpine:3.20\nADD data.tar.gz /add/\nCOPY data.tar.gz /copy/\nCMD ["ls", "-R", "/add", "/copy"]\n' > Dockerfile
docker build -t addcopy .
docker run --rm addcopyThe output lists /add/data/hello.txt, because ADD extracted the archive. It also lists /copy/data.tar.gz, because COPY kept the archive as a single file.
Why COPY is the default choice
Docker's own guidance is to use COPY unless you need an ADD feature. The reasons are practical:
- No surprise extraction. If you
ADDan archive that you meant to ship as a file, it is unpacked. If a file happens to look like an archive, you get a different result than you intended. - Clear intent. A reader seeing
COPYknows files are copied. A reader seeingADDhas to check whether extraction or a download is involved. - Remote downloads leave data behind. A file fetched with
ADDbecomes part of its layer. You cannot delete it in a later instruction to shrink the image, because the earlier layer still holds it.
For downloads, a RUN instruction lets you fetch, verify, unpack, and delete in one layer:
FROM alpine:3.20
RUN apk add --no-cache curl \
&& curl -fsSL https://example.com/tool.tar.gz -o /tmp/tool.tar.gz \
&& tar -xzf /tmp/tool.tar.gz -C /opt \
&& rm /tmp/tool.tar.gzIf you want ADD for a remote file, BuildKit supports ADD --checksum=sha256:<hash> <url> <dest>. It fails the build when the downloaded content does not match the hash, so a changed or tampered file is caught.
Common errors
- Using
ADDfor plain files. It works, but it hides your intent and risks accidental extraction. - Expecting
ADDto extract a downloaded archive. Only local archives from the build context are unpacked. - Forgetting that both instructions respect
.dockerignore. A source excluded there is not in the context, so the build fails with a "not found" error.
Exercise
A Dockerfile contains ADD backup.tar.gz /srv/ and backup.tar.gz is in the build context. What ends up in /srv/, and which instruction would you use to keep the archive as a single file?
Show answer
The contents of the archive are extracted into /srv/, and backup.tar.gz itself does not appear there. Use COPY backup.tar.gz /srv/ to keep it as one file.