乐闻世界logo
搜索文章和话题

What is the Difference Between COPY and ADD Instructions in Dockerfile?

2月7日 13:58

Both COPY and ADD are instructions in a Dockerfile used to copy files from the build context to a Docker image. However, there are some key differences between them:

  1. Basic Functionality:

    • COPY: Simply copies local files or directories to a specified path in the target Docker image.
    • ADD: Can also copy local files or directories to the image, but it supports two additional features: first, it can handle URL source files, downloading the file specified by the URL to the image; second, it automatically handles compressed archives (such as tar archives), extracting them to the target path.
  2. Usage Recommendations:

    • Since COPY focuses solely on basic copy operations, its behavior is more direct and predictable, so Docker documentation recommends using COPY when only copying files is required.
    • ADD should be used for special scenarios where COPY cannot satisfy requirements, such as when downloading network resources or automatically extracting compressed files.
  3. Examples:

In summary, although ADD provides more features, it is generally recommended to use COPY in most cases to maintain the simplicity and maintainability of the Dockerfile.

标签:Docker