hieuhtr / Blog

Don’t be lazy. Don’t make excuses. No one cares. Work fucking harder.
Other
6 stars 2 forks source link

ADD or COPY in Dockerfile #65

Open hieuhtr opened 7 years ago

hieuhtr commented 7 years ago

Concept & fundamental

ADD and COPY are functionally similar

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 won’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.

Reference:

  1. https://docs.docker.com/engine/userguide/eng-image/dockerfile_best-practices/#add-or-copy
  2. https://stackoverflow.com/questions/24958140/what-is-the-difference-between-the-copy-and-add-commands-in-a-dockerfile