difference between the COPY and ADD commands in a Dockerfile

0 votes

When do I use Copy command and Add command? Can anyone explain me this?


COPY <src> <dest>

The COPY instruction will copy new files from <src> and add them to the container's filesystem at path <dest>


ADD <src> <dest>

The ADD instruction will copy new files from <src> and add them to the container's filesystem at path <dest>.

Aug 24, 2018 in DevOps & Agile by Damon Salvatore
• 5,980 points
5,028 views

2 answers to this question.

0 votes

Here is what you can do. Just check the ADD and COPY documentation for an clear description, but in short, the major difference is that ADD can do more than COPY:

  • ADD allows <src> to be a URL
  • If the <src> parameter of ADD is an archive in a recognised compression format, it will be unpacked

Note that the Best practices for writing Dockerfiles suggests using COPY where the magic of ADD is not required. Otherwise you (since you had to lookup this answer) are likely to get surprised someday when you mean to copy keep_this_archive_intact.tar.gz into your container, but instead you spray the contents onto your file system.

I hope it would resolve your query.

Thanks

answered Aug 24, 2018 by Damon Salvatore
• 5,980 points
I would like to add more point over this :

1) ADD will creates a non existing directory where as COPY won't  do it.

2) I have read some where that if you use URL as a src in ADD with tar file as a download link, you won't get extracted tar file in the destination(I have not checked it)
Thank you @Saurav! Can you make your comment an answer? It'll be easier for other readers to understand.
0 votes

Hi,

COPY and ADD are both Dockerfile instructions that serve similar purposes. They let you copy files from a specific location into a Docker image.

COPY takes in an src and destination. It only lets you copy in a local file or directory from your host (the machine building the Docker image) into the Docker image itself.

ADD lets you do that too, but it also supports 2 other sources. First, you can use a URL instead of a local file/directory. Secondly, you can extract a tar file from the source directly into the destination.

answered Dec 23, 2020 by akhtar
• 38,260 points

Related Questions In DevOps & Agile

+2 votes
1 answer

What is the difference between a container and an image in Docker?

 Below is reference which you can follow to know ...READ MORE

answered Sep 20, 2018 in DevOps & Agile by shubham
• 7,340 points
1,259 views
0 votes
1 answer

How can a script identify the difference between Docker Toolbox and Docker for Windows?

Now, the Toolbox works through docker-machine. The way ...READ MORE

answered Aug 24, 2018 in DevOps & Agile by Tyrion anex
• 8,700 points
850 views
0 votes
0 answers

What is the difference between Azure DevOps and AWS DevOps?

Could someone perhaps explain how Azure DevOps ...READ MORE

Feb 16, 2023 in DevOps & Agile by Edureka
• 12,690 points
562 views
+13 votes
2 answers
+2 votes
1 answer
+2 votes
1 answer

Deploy Docker Containers from Docker Cloud

To solve this problem, I followed advice ...READ MORE

answered Sep 3, 2018 in AWS by Priyaj
• 58,020 points
2,739 views
0 votes
1 answer
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP