In Dockerfile, the COPY and ADD commands are both used to copy files from the build context into a Docker image. Although their functionalities are similar, there are some key differences:
-
Basic Functionality:
COPY: Copies files or directories from the build context to a specified path in the image. It is limited to basic copy operations.ADD: In addition to all the functionalities ofCOPY,ADDsupports two additional features: automatically decompressing compressed files (such as tar files) into the image, and using a URL as the source path to directly download files into the image.
-
Use Cases:
COPYis the recommended command when you need to simply copy local files into the image; prefer usingCOPYin such cases.ADDcan be used in specific scenarios, such as when you need to automatically decompress files or download files from the internet into the image.
-
Examples:
-
Using the
COPYcommand:dockerfileCOPY ./app /usr/src/appThis command copies the local
appdirectory into the/usr/src/apppath in the image. -
Using the
ADDcommand:dockerfileADD https://example.com/example.tar.gz /var/wwwThis command not only downloads the
example.tar.gzfile but also automatically decompresses it into the/var/wwwdirectory.
-
Summary: Although ADD provides some additional features, Docker's official documentation recommends using COPY as it is more direct and transparent. Only consider using ADD in specific cases where its additional features are needed.
2024年8月10日 00:21 回复