Categories
docker dockerfile

What is the difference between the ‘COPY’ and ‘ADD’ commands in a Dockerfile?

2794

What is the difference between the COPY and ADD commands in a Dockerfile, and when would I use one over the other?

COPY <src> <dest>

The COPY instruction will copy new files from <src> and add them to the container’s filesystem at path <dest>

ADD <src> <dest>

The ADD instruction will copy new files from <src> and add them to the container’s filesystem at path <dest>.

3

  • 13

    See best practices: docs.docker.com/engine/userguide/eng-image/…

    – acanimal

    Jul 1, 2017 at 8:28

  • 20

    As at June 2018 the reference says that ADD adds to the image (i.e a static file) whereas COPY adds to the container (i.e. a runtime instance of the image). Surely this implies that COPY is executed each time the image is Docker run’d, or maybe this is simply a case of inconsistent terminology?

    Jun 5, 2018 at 13:24

  • @ChrisRobinson running docker cp on the host (Docker command line) will copy files to/from a running container. The COPY directive is the Dockerfile is for building the image.

    – Jurn Ho

    Jul 17 at 10:52

2712

You should check the ADD and COPY documentation for a more detailed description of their behaviors, but in a nutshell, the major difference is that ADD can do more than COPY:

  • ADD allows <src> to be a URL
  • Referring to comments below, the ADD documentation states that:

If is a local tar archive in a recognized compression format (identity, gzip, bzip2 or xz) then it is unpacked as a directory. Resources from remote URLs are not decompressed.

Note that the Best practices for writing Dockerfiles suggests using COPY where the magic of ADD is not required. Otherwise, you (since you had to look up this answer) are likely to get surprised someday when you mean to copy keep_this_archive_intact.tar.gz into your container, but instead, you spray the contents onto your filesystem.

0

    683

    COPY is

    Same as ‘ADD’, but without the tar and remote URL handling.

    Reference straight from the source code.

    0

      159

      There is some official documentation on that point: Best Practices for Writing Dockerfiles

      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.

      RUN mkdir -p /usr/src/things \
        && curl -SL http://example.com/big.tar.gz \
          | 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.

      0