DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Chaos Engineering and Kafka In Action: Topic Level Idempotence and Retry Check
  • Deploying Small Language Models on Sovereign Cloud
  • How to Keep AI Agents From Going Rogue Using Docker Sandbox
  • When One Docker Swarm Replica Became Unreachable Over an Overlay Network: A Production Case With Traefik

Trending

  • AI-Assisted Product Discovery
  • Treating Web Scrapers Like Production Services
  • Member Spotlight: Srinivas Chippagiri
  • Every Acquisition Promises One Company. Most Deliver Two.

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Why You Should Use COPY Instead of ADD When Building Docker Images

Why You Should Use COPY Instead of ADD When Building Docker Images

A quick look into why COPY adds more security to your Docker images and containers than ADD.

By 
Liran Tal
·
Updated May. 01, 19 · Analysis
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
19.9K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

A report about Docker security has recently been published with actionable advice on how to further bridge the gap on docker images security. In this article, I'd like to visit one of these concerns and discuss the differences of COPY and ADD directives in a Dockerfile.

Docker provides two commands for copying files from the host to the Docker image when building it: COPY and ADD. The instructions are similar in nature, but differ in their functionality:

  • COPY — copies local files recursively, given explicit source and destination files or directories. With COPY, you must declare the locations.
  • ADD — copies local files recursively, implicitly creates the destination directory when it doesn’t exist, and accepts archives as local or remote URLs as its source, which it expands or downloads respectively into the destination directory.

While subtle, the differences between ADD and COPY are important. Be aware of these differences to avoid potential security issues:

  • When remote URLs are used to download data directly into a source location, they could result in man-in-the-middle attacks that modify the content of the file being downloaded. Moreover, the origin and authenticity of remote URLs need to be further validated. When using COPY the source for the files to be downloaded from remote URLs should be declared over a secure TLS connection and their origins need to be validated as well.
  • Space and image layer considerations: using COPY allows separating the addition of an archive from remote locations and unpacking it as different layers, which optimizes the image cache. If remote files are needed, combining all of them into one RUN command that downloads, extracts, and cleans-up afterwards optimizes a single layer operation over several layers that would be required if ADD were used.
  • When local archives are used, ADD automatically extracts them to the destination directory. While this may be acceptable, it adds the risk of zip bombs and Zip Slip vulnerabilities that could then be triggered automatically.

This tip is part of a complete 10 Docker image security best practices you should adopt. Thanks for reading and to Omer Levi Hevroni who worked with me on it.

The original blog post includes a high-resolution printable PDF like the snippet you see below. Check it out

Docker (software)

Opinions expressed by DZone contributors are their own.

Related

  • Chaos Engineering and Kafka In Action: Topic Level Idempotence and Retry Check
  • Deploying Small Language Models on Sovereign Cloud
  • How to Keep AI Agents From Going Rogue Using Docker Sandbox
  • When One Docker Swarm Replica Became Unreachable Over an Overlay Network: A Production Case With Traefik

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook