Skip to content
tezvyn:

Dockerfile COPY versus ADD

Source: interviewEasyHow cards are made

Summary

Dockerfile clarity and best practice.

Key points

COPY just copies local files; ADD also auto-extracts local tarballs and can fetch remote URLs; prefer COPY for predictability, use ADD for local archive extraction.

WHAT THIS TESTS It checks whether you know both instructions and follow Docker's own guidance to prefer the predictable one, plus the one narrow case where ADD earns its place.

A GOOD ANSWER COVERS COPY takes files or directories from the build context and copies them into the image filesystem, nothing more. ADD does the same but adds two behaviors: if the source is a recognized local compressed archive such as a tar.gz, it is automatically extracted into the destination; and if the source is a URL, it downloads the file. COPY is generally preferred precisely because it has no surprises, which makes Dockerfiles easier to read and audit. The justified scenario for ADD is unpacking a local tarball you already have in the context, where its auto-extraction saves a separate RUN tar step.

COMMON WRONG ANSWERS Saying they are identical. Recommending ADD with a remote URL to fetch dependencies, which is discouraged because you cannot verify checksums easily and you cannot remove the download in the same layer. Forgetting that ADD's auto-extraction can be surprising when you actually wanted the archive file itself.

LIKELY FOLLOW-UPS Why is downloading via RUN curl preferred over ADD URL? How does layer caching treat each? What about .dockerignore?

ONE CONCRETE EXAMPLE To bring a vendored tarball into the image and unpack it, ADD vendor.tar.gz /opt/app does extraction in one line. For ordinary source files you write COPY . /app, keeping behavior obvious; to fetch a remote artifact you use RUN curl -fsSL url -o file and verify its checksum, not ADD.

Interview question

Which scenario is the legitimate reason to choose ADD over COPY in a Dockerfile?

  • a.Auto-extracting a local tar archive into the destinationCorrect
  • b.Setting file ownership during the copy
  • c.Copying a directory of plain source files into the image
  • d.Renaming files as they are copied into the image
Why?

ADD's distinguishing legitimate feature is auto-extracting local tar archives; plain file copies are exactly what COPY is for, and both support ownership flags so that is not a differentiator.

Just read this? Test yourself on what you have been reading.

Read the original → docs.docker.com

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.

See open roles