Dockerfile: ADD vs COPY

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…


This content originally appeared on DEV Community and was authored by Grigor Khachatryan

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 use curl or wget 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!


This content originally appeared on DEV Community and was authored by Grigor Khachatryan


Print Share Comment Cite Upload Translate Updates
APA

Grigor Khachatryan | Sciencx (2022-03-17T07:23:39+00:00) Dockerfile: ADD vs COPY. Retrieved from https://www.scien.cx/2022/03/17/dockerfile-add-vs-copy/

MLA
" » Dockerfile: ADD vs COPY." Grigor Khachatryan | Sciencx - Thursday March 17, 2022, https://www.scien.cx/2022/03/17/dockerfile-add-vs-copy/
HARVARD
Grigor Khachatryan | Sciencx Thursday March 17, 2022 » Dockerfile: ADD vs COPY., viewed ,<https://www.scien.cx/2022/03/17/dockerfile-add-vs-copy/>
VANCOUVER
Grigor Khachatryan | Sciencx - » Dockerfile: ADD vs COPY. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/03/17/dockerfile-add-vs-copy/
CHICAGO
" » Dockerfile: ADD vs COPY." Grigor Khachatryan | Sciencx - Accessed . https://www.scien.cx/2022/03/17/dockerfile-add-vs-copy/
IEEE
" » Dockerfile: ADD vs COPY." Grigor Khachatryan | Sciencx [Online]. Available: https://www.scien.cx/2022/03/17/dockerfile-add-vs-copy/. [Accessed: ]
rf:citation
» Dockerfile: ADD vs COPY | Grigor Khachatryan | Sciencx | https://www.scien.cx/2022/03/17/dockerfile-add-vs-copy/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.