🎓 A1Learner
🔍
0 of 50 lessons completed

COPY vs ADD

COPY copies files into an image, and ADD does the same plus two extras: it fetches a web address and unpacks archives. COPY is the default, because it does exactly one job.

COPY a file

This line copies greet.py into the image.

COPY greet.py .

ADD unpacks an archive

This line copies and unpacks data.tar.gz in one step.

ADD data.tar.gz /data/

Fetch with curl instead

A RUN step fetches a remote file plainly.

RUN curl -sS -o file.txt https://example.com/file.txt

Example

docker build -t g4-copy:1.0 . >/dev/null
docker run --rm g4-copy:1.0

Key points

  • Use COPY to copy files and folders from the build folder.
  • ADD also copies files, and it fetches URLs and unpacks archives.
  • COPY is plain and clear. A reader sees at once what it does.
  • Use ADD only to unpack a local archive into the image.
  • Fetch a remote file with curl in a RUN step instead of ADD.

In this example, a Dockerfile uses COPY to add greet.py to the image. The container runs the copied file and prints its greeting. The greeting proves COPY put the file where the program expects it.

Note

COPY never follows a URL and never unpacks anything. That plainness is the point.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Rust Playground

Exercise

When is ADD the right choice over COPY?