DEV Community

Cover image for Confused between ADD and COPY instructions in Dockerfile ?
Ashutosh Sharma
Ashutosh Sharma

Posted on

Confused between ADD and COPY instructions in Dockerfile ?

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:

Top comments (0)