Docker Interview Question 7: Copy vs Add Archive
Docker Interview Questions · Test 1 · question 7 of 10
A Dockerfile contains ADD vendor.tar.gz /opt/vendor/, where vendor.tar.gz sits in the build context. Following a "prefer COPY over ADD" guideline, a teammate changes it to COPY vendor.tar.gz /opt/vendor/. The image builds, but the application now fails because /opt/vendor/ holds only the file vendor.tar.gz instead of the extracted libraries. Why?
Show answer and explanation
Answer:
ADD and COPY are functionally similar, but ADD has extra behaviour: when the source is a local tar archive in a recognized compression format (gzip, bzip2, xz, zstd, or uncompressed), it is decompressed and extracted into the destination. COPY does basic copying only, so the archive lands unchanged. To switch to COPY, extract the archive in a RUN step (or keep ADD on purpose for this line).
Option A is false: COPY copies single files as well as directories.
Option B is false: COPY reads from the build context by default. COPY --from is only an option for copying from another stage or image.
Option C is false: .dockerignore removes files from the build context itself, so excluded files are invisible to *both* instructions.
References: Dockerfile reference - ADD; Building best practices - ADD or COPY