COPY vs ADD: File Operations
You'll learn when to use COPY versus ADD in Dockerfiles and how each instruction handles files differently.
What They Do
Both COPY and ADD move files from your local filesystem (the build context) into the container image. They look similar but have important differences.
COPY is straightforward: it copies files or directories from your build context into the image, period.
COPY app.py /app/
COPY ./config /app/config
ADD has extra features: it can copy local files and automatically extract tar archives, plus it can fetch files from URLs.
ADD archive.tar.gz /app/ # Extracts automatically
ADD https://example.com/file.txt /tmp/ # Downloads from URL
When to Use Each
Use COPY by default. It's explicit, predictable, and does exactly what you expect—no surprises.
Use ADD only when you need:
- Automatic extraction of local
.tar,.tar.gz,.tgz, etc. - (Though URL downloads are possible, better tools exist for that)
Think of COPY as a simple photocopy machine—it duplicates what you give it. ADD is like a smart assistant that sometimes "helps" by unzipping things, which can be confusing if you didn't want that.
File Permissions
Both preserve file permissions from the source. If your script is executable locally (chmod +x), it stays executable in the image. If you need to change permissions, use RUN chmod after copying.
COPY script.sh /app/
RUN chmod +x /app/script.sh
Key Takeaway: Prefer COPY for clarity and predictability; reserve ADD only for extracting local tar archives where its auto-extraction saves you a step.