Copying Files (COPY vs ADD & When to Use Each)
Prefer COPY for local files; reserve ADD for rare archive/URL cases with eyes open.
In plain words
Default to COPY; use ADD only when automatic archive extraction or a clearly reviewed URL feature is intentional.
Think of it like…
COPY is handing a parcel across the counter; ADD can also open the parcel and import inventory from a delivery truck.
Where you’ve seen it
In practice, COPY package.json package-lock.json /app/; COPY --chown=10001:10001 dist/ /app/dist/ is the concrete move for this job: prefer COPY for local files; reserve ADD for rare archive/URL cases with eyes open. The release is not accepted until docker history --no-trunc app:release and tar -tf local-bundle.tar.gz before and after a comparative ADD build confirms the result.
Words you’ll hear
- COPY
- The implementation control in
COPY package.json package-lock.json /app/; COPY --chown=10001:10001 dist/ /app/dist/for prefer COPY for local files; reserve ADD for rare archive/URL cases with eyes open. - ADD
- The result to observe with
docker history --no-trunc app:release and tar -tf local-bundle.tar.gz before and after a comparative ADD build, rather than infer from a successful command alone. - Context Paths
- The production boundary exposed when using ADD to copy ordinary source directories, or fetching an unpinned URL without checksum verification and expecting stable builds.
Visual diagram
Theory & deep dive
COPY control path
The concrete mechanism is COPY package.json package-lock.json /app/; COPY --chown=10001:10001 dist/ /app/dist/. The observable result is docker history --no-trunc app:release and tar -tf local-bundle.tar.gz before and after a comparative ADD build; if those disagree, inspect the runtime evidence before changing unrelated settings.
Prefer COPY for local files; reserve ADD for rare archive/URL cases with eyes open. Keep COPY, ADD and Context Paths in the same deployment record so the intent and the observed state cannot drift apart.
COPY package.json package-lock.json /app/; COPY --chown=10001:10001 dist/ /app/dist/
docker history --no-trunc app:release and tar -tf local-bundle.tar.gz before and after a comparative ADD build, not with command success alone.Context Paths failure boundary
A maintainer reading COPY can predict which context path changed the layer. ADD can unpack nested content, preserve unexpected paths or depend on a remote response, making security review and cache invalidation harder. The narrow failure to design for is using ADD to copy ordinary source directories, or fetching an unpinned URL without checksum verification and expecting stable builds.
Accept this compromise explicitly: COPY is boring and auditable; ADD can save an extraction step but usually moves complexity into implicit builder behavior. The controlled check-docker history --no-trunc app:release and tar -tf local-bundle.tar.gz before and after a comparative ADD build-also identifies the point where a different design is safer.
Exercise this failure safely: using ADD to copy ordinary source directories, or fetching an unpinned URL without checksum verification and expecting stable builds.
How it works, step by step
Step 1 of 41. State the boundary
Write down why the workload needs this: A maintainer reading COPY can predict which context path changed the layer. ADD can unpack nested content, preserve unexpected paths or depend on a remote response, making security review and cache invalidation harder.
- Intent
- COPY
When to use it & trade-offs
What it is
COPY moves local files from the build context to the image with straightforward semantics. ADD additionally recognizes local tar archives and can fetch remote URLs, which makes the side effects less visible and often less reproducible.
Why it matters: A maintainer reading COPY can predict which context path changed the layer. ADD can unpack nested content, preserve unexpected paths or depend on a remote response, making security review and cache invalidation harder.
When to reach for it
- The fit is strongest for this job: prefer COPY for local files; reserve ADD for rare archive/URL cases with eyes open. Pair it with
docker history --no-trunc app:release and tar -tf local-bundle.tar.gz before and after a comparative ADD buildin the runbook. - Production COPY changes should make ADD observable through
COPY package.json package-lock.json /app/; COPY --chown=10001:10001 dist/ /app/dist/, rather than trusting the runtime default. - Keep a rollback example for the case where using ADD to copy ordinary source directories, or fetching an unpinned URL without checksum verification and expecting stable builds.
When to avoid or be careful
- Do not accept command success while the system can still fail by using ADD to copy ordinary source directories, or fetching an unpinned URL without checksum verification and expecting stable builds.
- Avoid erasing Context Paths protection merely to dodge this cost: “COPY is boring and auditable; ADD can save an extraction step but usually moves complexity into implicit builder behavior”.
- Do not replace the evidence path-
docker history --no-trunc app:release and tar -tf local-bundle.tar.gz before and after a comparative ADD build-with a screenshot or a mutable tag.
COPY package.json package-lock.json /app/; COPY --chown=10001:10001 dist/ /app/dist/: practical choices
Strengths: COPY moves local files from the build context to the image with straightforward semantics. ADD additionally recognizes local tar archives and can fetch remote URLs, which makes the side effects less visible and often less reproducible.
Watch out: COPY is boring and auditable; ADD can save an extraction step but usually moves complexity into implicit builder behavior.
Strengths: It can begin with COPY package.json package-lock.json /app/; COPY --chown=10001:10001 dist/ /app/dist/ and fewer surrounding controls.
Watch out: It silently accepts using ADD to copy ordinary source directories, or fetching an unpinned URL without checksum verification and expecting stable builds, which makes the result hard to reproduce after promotion.
Strengths: A maintainer reading COPY can predict which context path changed the layer. ADD can unpack nested content, preserve unexpected paths or depend on a remote response, making security review and cache invalidation harder.
Watch out: The platform adds an ownership and debugging layer; retain docker history --no-trunc app:release and tar -tf local-bundle.tar.gz before and after a comparative ADD build as the direct check.
Interview signals & numbers to know
When this topic comes up
- COPY package.json package-lock.json /app/; COPY --chown=10001:10001 dist/ /app/dist/
- docker history --no-trunc app:release and tar -tf local-bundle.tar.gz before and after a comparative ADD build
- Failure signature: using ADD to copy ordinary source directories, or fetching an unpinned URL without checksum verification and expecting stable builds
- Trade-off: COPY is boring and auditable; ADD can save an extraction step but usually moves complexity into implicit builder behavior.
Benchmarks & rules of thumb
- 1 explicit operation
- COPY always performs a local copy
- 2 hidden ADD abilities
- archive extraction and remote URL fetching
- 10001 UID
- a non-root destination owner in the example
Common mistakes & follow-ups
Common interview mistakes
- Treating a successful
COPY package.json package-lock.json /app/; COPY --chown=10001:10001 dist/ /app/dist/as proof and skippingdocker history --no-trunc app:release and tar -tf local-bundle.tar.gz before and after a comparative ADD build. - Using ADD to copy ordinary source directories, or fetching an unpinned URL without checksum verification and expecting stable builds
- Optimizing COPY without accepting that cOPY is boring and auditable; ADD can save an extraction step but usually moves complexity into implicit builder behavior.
Likely interviewer follow-ups
- Why does
COPY package.json package-lock.json /app/; COPY --chown=10001:10001 dist/ /app/dist/address COPY here rather than through application code? - How would you distinguish healthy ADD from the failure “using ADD to copy ordinary source directories, or fetching an unpinned URL without checksum verification and expecting stable builds” using
docker history --no-trunc app:release and tar -tf local-bundle.tar.gz before and after a comparative ADD build? - When would this trade-off-“COPY is boring and auditable; ADD can save an extraction step but usually moves complexity into implicit builder behavior”-justify a different Context Paths design?
COPY package.json package-lock.json /app/; COPY --chown=10001:10001 dist/ /app/dist/-and one proof-docker history --no-trunc app:release and tar -tf local-bundle.tar.gz before and after a comparative ADD build. Then name the boundary: using ADD to copy ordinary source directories, or fetching an unpinned URL without checksum verification and expecting stable builds.Knowledge check
Put it into practice
Practice related interview Q&A
Apply Copying Files (COPY vs ADD & When to Use Each) with generative AI interview Q&A and hands-on practice.