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
The COPY instruction copies new files or directories from <src>
and adds them to the filesystem of the container at the path <dest>
.
COPY has two forms:
COPY [--chown=<user>:<group>] <src>... <dest>
COPY [--chown=<user>:<group>] ["<src>",... "<dest>"] (this form is required for paths containing whitespace)
ADD
ADD has two forms:
ADD [--chown=<user>:<group>] <src>... <dest>
ADD [--chown=<user>:<group>] ["<src>",... "<dest>"] (this form is required for paths containing whitespace)
Dockerfile best practice for copying from a URL
Docker suggests that it is often not efficient to copy from a URL using ADD
, and it is best practice to use other strategies to include the required remote files.
COPY
only supports the basic copying of local files into the container, while ADD has some features (like local-only tar extraction and remote URL support) that are not immediately obvious. Consequently, the best use for ADD
is local tar file auto-extraction into the image, as in ADD rootfs.tar.xz /.
— Dockerfile Best Practices
Because image size matters, using ADD to fetch packages from remote URLs is strongly discouraged; you should usecurl
orwget
instead. That way you can delete the files you no longer need after they’ve been extracted and you don’t have to add another layer in your image. For example, you should avoid doing things like:
ADD http://example.com/big.tar.xz /usr/src/things/
RUN tar -xJf /usr/src/things/big.tar.xz -C /usr/src/things
RUN make -C /usr/src/things all
And instead, do something like:
RUN mkdir -p /usr/src/things \
&& curl -SL http://example.com/big.tar.xz \
| tar -xJC /usr/src/things \
&& make -C /usr/src/things all
For other items (files, directories) that do not require ADD
’s tar auto-extraction capability, you should always use COPY
.
Like to learn?
Follow me on twitter where I post all about the latest and greatest AI, DevOps, VR/AR, Technology, and Science! Connect with me on LinkedIn too!
Top comments (1)
Great article! Kudos