Difference between ADD and COPY

Hi everyone!

I’m currently learning Docker and working on writing Dockerfiles, and I have a question I’d really appreciate some help with.

Could someone explain the difference between “ADD” and “COPY” instructions? I’ve seen both being used, but I’m not entirely clear on when it’s better to use one over the other in real-world scenarios.

In particular:

  • What are the key differences in behaviour between “ADD” and “COPY”?
  • When should I prefer “COPY” over “ADD” (or vice versa)?
  • Are there any best practices or common mistakes I should be aware of?

Thanks in advance for your help!

COPY:

  • when you copy files/folders from the build context into the image
  • when you want to copy files/folders from another stage in a multi-staged build
  • performs only copy operation, nothing else

ADD:

  • when you copy a tar archive (havinggzip , bzip2 or xz compression) from the build context into the image and want it’s content unpacked into the target path (does not unpack zip files)
  • when you want to download files or tar archives to be extracted from a public URL
  • when you want to clone a public git repository

It has been years I used the ADD instruction in any of my Dockerfiles. Instead of using ADD you will more often see chained RUN instructions that clone a git repo, or download an archive, do something with it and then delete the checked out repo or the compressed archive, so it doesn’t end up in the image layer the RUN instruction creates.

1 Like

Thank you for reading my post.