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:
-
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.
-
Usage Recommendations:
- Since
COPYfocuses solely on basic copy operations, its behavior is more direct and predictable, so Docker documentation recommends usingCOPYwhen only copying files is required. ADDshould be used for special scenarios whereCOPYcannot satisfy requirements, such as when downloading network resources or automatically extracting compressed files.
- Since
-
Examples:
- Using
COPYinstruction:COPY ./localfile.txt /path/in/container/localfile.txt - Using
ADDinstruction: `ADD http://example.com/examplefile.tar /path/in/container/
- Using
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.