CodePark/ questions / Dockerfile instructions
START PRACTICINGPRACTICE
Backend question · Docker › Dockerfile instructions

What is the difference between COPY and ADD, and which should you use by default?

Use COPY by default. Use ADD only when you need its extra features.

COPY copies files and folders into the image. The source is the build context or, with --from, another build stage. That is all it does.

ADD does the same, plus two extra things:

  • If the source is a local tar archive in a known compression format, ADD extracts it into the destination automatically.
  • The source can be a remote URL. ADD downloads it at build time.

COPY is better by default because its behaviour is clear and predictable. To download files, a RUN with curl or wget often gives you more control. For example, you can check the file's checksum and delete the archive in the same layer.

  • COPY only copies from the build context or another stage.
  • ADD extracts local tar archives automatically.
  • ADD can download files from remote URLs.
  • Prefer COPY because it is clear and predictable.