Confused between ADD and COPY instructions in Dockerfile ?
Ashutosh Sharma

Ashutosh Sharma @ashusharmatech

About: 📚 Tech explorer and knowledge seeker. From coding adventures to the latest gadgets, I'm here to share my experiences and discoveries in the world of technology.

Location:
Pune, India
Joined:
Jul 6, 2020

Confused between ADD and COPY instructions in Dockerfile ?

Publish Date: May 12 '22
11 0

When you have to create a Dockerfile there are two instructions that you can use to copy the files to the Container file system. The commands are ADD and COPY, both work quite similar but there are some specifics that we should consider before using them.

COPY

  • Copy instruction will only allow copying the local source file to the container’s file system
COPY foo.properties /var/tmp/
Enter fullscreen mode Exit fullscreen mode

ADD

  • It will copy the local files and directories to the container's file system
  ADD foo.properties /var/tmp/
Enter fullscreen mode Exit fullscreen mode
  • It will also allow copying the file from a remote URL.
  ADD http://someserver.com/file/foo.properties /var/tmp
Enter fullscreen mode Exit fullscreen mode
  • Add instructions will also unpack the compressed file after copying it.
ADD test.tar /var/tmp/
Enter fullscreen mode Exit fullscreen mode

The outcome of the above instruction would be that test.tar will be copied first and then it will be extracted at location

/var/tmp/
Enter fullscreen mode Exit fullscreen mode

Be careful that ADD doesn't unpack the compressed when you copy it from a remote URL. It will only download and copy it. Unpacking works only for local files.

Recommendation
If you need to copy from the local build context into a container, stick to using COPY.

Also, when you need to download and copy from a URL instead of ADD, it’s safer and more efficient to use wget or curl within a RUN command. By doing so, you avoid creating an additional image layer.

ADD config.tar.gz /
Enter fullscreen mode Exit fullscreen mode
COPY config.tar.gz /
RUN tar -zxvf config.tar.gz
RUN rm -rf config.tar.gz
Enter fullscreen mode Exit fullscreen mode

References:

Comments 0 total

    Add comment