Why ADD is preferred than COPY in Dockerfile ?

Syed Jafer KSyed Jafer K
1 min read

Doubt on Best Practices:

While i was reading on the best practices of writing a Dockerfile, its mentioned that COPY is preferred than ADD. Eventhough it performs same operations as COPY.

Solution:

The COPY and ADD commands in Docker are both used to copy files or directories from the host machine to the Docker image. However, there are key differences between the two:

COPY ./app /app
ADD http://github.com/syedjafer/file.txt /app/
ADD archive.tar.gz /app/

Use of Local Files

  • COPY: Primarily used for copying local files and directories from the host machine to the image. It is straightforward and is the recommended choice for simple file copying tasks.

  • ADD: Similar to COPY, but it also has additional features, such as the ability to copy remote URLs and extract compressed files. For eg; ADD archive.tar.gz /app/ will extract the tar file and copy the same to /app folder.

URLs and Compression

  • COPY: Only handles local files and directories.

  • ADD: Can fetch files from URLs and automatically extract compressed files (e.g., tar and zip) during the copy process. While this can be convenient, it also introduces complexity and potential security risks.

In order to avoid confusion of the working, COPY is preferred than ADD.

0
Subscribe to my newsletter

Read articles from Syed Jafer K directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Syed Jafer K
Syed Jafer K