Common DockerFile directives:
Estimated reading: 5 minutes 7 views

๐Ÿ“ฆ DockerFile COPY and ADD Command โ€“ Complete Guide with Examples & FAQ

When building Docker images, adding files and directories from your local environment into the container is a common task. DockerFile provides two instructions to accomplish this: COPY and ADD.

While they might seem similar at first glance, they have subtle differences and best-use scenarios that every developer should know. In this guide, weโ€™ll break them down, compare their functionality, and walk you through usage examples.


๐Ÿ“Œ What is the COPY Command in DockerFile?

The COPY instruction is used to copy files or directories from the local build context into the Docker image.

โœ… Syntax:

COPY <src> <dest>
  • <src>: Source file or directory path (relative to the build context).
  • <dest>: Destination path inside the image.

๐Ÿ“‚ Example:

COPY index.html /usr/share/nginx/html/

๐Ÿ” This command copies index.html from your local folder to the /usr/share/nginx/html/ directory inside the Docker image.


๐Ÿ“Œ What is the ADD Command in DockerFile?

The ADD instruction does everything COPY does and more. It also allows:

  • Extracting local .tar archives.
  • Downloading files from remote URLs.

โœ… Syntax:

ADD <src> <dest>

๐Ÿ“‚ Example โ€“ Copying a File:

ADD index.html /usr/share/nginx/html/

This behaves the same as COPY in this context.

๐Ÿ“ฆ Example โ€“ Extracting a .tar Archive:

ADD assets.tar.gz /app/

๐ŸŽฏ This will automatically extract the contents of assets.tar.gz into the /app/ directory inside the image.

๐ŸŒ Example โ€“ Downloading from a URL:

ADD https://example.com/image.png /images/

๐ŸŒ The file is downloaded and placed into /images/.


๐Ÿง  Difference Between COPY and ADD

FeatureCOPYADD
Basic file copyingโœ… Yesโœ… Yes
Accepts multiple sourcesโœ… Yesโœ… Yes
Extracts .tar, .tar.gz, etc.โŒ Noโœ… Yes
Supports remote URLs (HTTP/HTTPS)โŒ Noโœ… Yes
More predictable & secureโœ… YesโŒ No (can do too much)

โœ… Best Practice: Use COPY unless you explicitly need the features of ADD.


๐Ÿ”’ Best Practices for Using COPY and ADD

  1. โœ… Prefer COPY over ADD: It’s clearer and more predictable.
  2. ๐Ÿ›ก๏ธ Avoid downloading from external URLs in ADD โ€” use curl or wget instead in a RUN command.
  3. ๐Ÿ“ฆ Only use ADD for archive extraction, if needed.
  4. ๐Ÿ“‚ Structure your build context so only necessary files are included (use .dockerignore).

๐Ÿ› ๏ธ Practical Example โ€“ Using Both

FROM python:3.11

WORKDIR /app

# Copy source code
COPY . .

# Extract archive (only ADD can do this)
ADD data-files.tar.gz /data/

# Install dependencies
RUN pip install -r requirements.txt

CMD ["python", "main.py"]

๐Ÿงพ Summary Table

InstructionPurposeExtra CapabilitiesRecommended Use
COPYCopy files/directoriesโŒโœ… Yes (preferred)
ADDCopy + extract + downloadโœ… Tar, URL supportโŒ Only when required


๐Ÿ”ง DockerFile COPY vs ADD โ€“ Functional Table with Code Syntax

๐Ÿงฉ Feature / Capability๐Ÿ—‚๏ธ COPY Command๐Ÿ“ฆ ADD Command๐Ÿ’ก Recommended Use
Basic file/directory copyingโœ… Yes
COPY <source> <destination>
โœ… Yes
ADD <source> <destination>
Use either
ExampleCOPY app.py /app/ADD app.py /app/Use either
Supports multiple source pathsโœ… Yes
COPY file1.txt file2.txt /app/
โœ… Yes
ADD file1.txt file2.txt /app/
Use either
ExampleCOPY . /app/ADD . /app/Use either
Copies from local build contextโœ… Yesโœ… YesUse either
ExampleCOPY ./src /app/ADD ./src /app/Use either
Supports remote URLs (http/https)โŒ Noโœ… Yes
ADD https://example.com/file.tar /app/
Use ADD only if necessary
ExampleโŒ Not allowedADD https://example.com/file.tar /app/Use ADD only if necessary
Auto-extracts compressed archivesโŒ Noโœ… Yes
ADD file.tar.gz /app/
Use ADD if extraction needed
ExampleโŒ Not applicableADD archive.tar.gz /app/Use ADD if extraction needed
More predictable behaviorโœ… YesโŒ No (can behave unexpectedly with URLs/tars)Prefer COPY
ExampleCOPY app.py /app/ADD app.py /app/Prefer COPY
Security (avoids unwanted downloads)โœ… HighโŒ Lower (downloads from URLs can be insecure)Prefer COPY
ExampleCOPY config.json /app/ADD https://example.com/file.txt /app/Prefer COPY
Common for codebase and config filesโœ… Yesโœ… YesUse COPY
ExampleCOPY . /app/ADD . /app/Use COPY
Recommended for production useโœ… Strongly RecommendedโŒ Only if requiredPrefer COPY
ExampleCOPY ./app /app/ADD ./app /app/Prefer COPY
Best use caseCopy source code, config filesExtract local archives or fetch remote filesUse based on scenario
ExampleCOPY index.html /usr/share/nginx/html/ADD assets.tar.gz /app/Use ADD for archive extraction

๐Ÿงฉ Final Thoughts

When creating a DockerFile, keep it clean, clear, and secure. Stick to COPY for straightforward operations, and only use ADD when its extra features are truly needed.

๐Ÿ’ก Tip: Always test your Docker image locally before pushing it to production.


โ“ FAQ โ€“ Frequently Asked Questions

๐Ÿ”น Q1: Can COPY copy from remote URLs?

No, COPY can only copy files from your local context. If you need to download files, use curl, wget, or ADD.


๐Ÿ”น Q2: Should I always use COPY instead of ADD?

Yes, unless you specifically need to extract an archive or download a file. Using COPY improves transparency and security.


๐Ÿ”น Q3: What if the .tar file is corrupted or not extracted by ADD?

Docker will throw an error. Ensure your .tar archive is valid and in a supported format like .tar, .tar.gz, .tar.bz2.


๐Ÿ”น Q4: How can I exclude files from being copied?

Use a .dockerignore file to prevent specific files or folders from being added to the image:

node_modules/
*.log
.env

๐Ÿ”น Q5: Can COPY and ADD copy hidden files?

Yes, as long as the files are not excluded in .dockerignore and the path is correct.


๐Ÿ”น Q6: Can I copy multiple files in a single instruction?

Yes. Use multiple source paths:

COPY app.py requirements.txt /app/

๐Ÿ”น Q7: Is there a performance difference between COPY and ADD?

Very minor โ€” but using COPY is generally faster and cleaner unless ADD‘s extra features are required.


Leave a Reply

Your email address will not be published. Required fields are marked *

Share this Doc

COPY/ADD

Or copy link

CONTENTS
Scroll to Top