Skip to content

Docker difference between copy and add

In Docker, both COPY and ADD are used to move files and directories from the host machine into the Docker container during the build process, but they have some differences

  1. Usage:
    • COPY: It is used to copy files and directories from the host machine to the container.
    • ADD: It has functionality similar to COPY, but it can also fetch remote URLs and extract TAR files.
  2. Complexity:
    • COPY: Simple and straightforward. It copies files or directories from the host machine to the specified location in the container.
    • ADD: A bit more complex. In addition to copying files, it can also handle URLs and automatically extract compressed files (like TAR, gzip, etc.) into the container.
  3. Cache Utilization:
    • COPY: This instruction has better caching during builds. If the contents being copied haven’t changed, Docker can use the cached version without re-executing this step during subsequent builds.
    • ADD: This instruction is less efficient in terms of caching because of its additional functionality. If the source file or URL changes, Docker will rebuild the step, even if the other parts of the Dockerfile remain the same.
  4. Best Practices:
    • COPY is recommended when you only need to copy local files or directories into the container.
    • ADD should be used when additional functionality (like fetching remote resources or extracting compressed files) is required.

In most cases, COPY is preferred due to its simplicity and better caching behavior. However, if you specifically need the additional features of ADD, such as handling URLs or extracting archives, then it’s suitable for those scenarios.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments