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 instruction will only allow copying the local source file to the container’s file system
COPY foo.properties /var/tmp/
- It will copy the local files and directories to the container's file system
ADD foo.properties /var/tmp/
- It will also allow copying the file from a remote URL.
ADD http://someserver.com/file/foo.properties /var/tmp
- Add instructions will also unpack the compressed file after copying it.
ADD test.tar /var/tmp/
The outcome of the above instruction would be that test.tar will be copied first and then it will be extracted at location
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.
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
curl within a RUN command. By doing so, you avoid creating an additional image layer.
ADD config.tar.gz /
COPY config.tar.gz / RUN tar -zxvf config.tar.gz RUN rm -rf config.tar.gz