SoFunction
Updated on 2025-04-14

The difference between copy and add in Dockerfile

In Dockerfile, what is the difference between copy and add?

In the Dockerfile,COPYandADDCommands are all used to copy files from the build context into a container, but there are some differences between them.

COPY Command

  • usage:COPY <source path> <destination path>
  • Used only to copy local files or directories into containers.
  • If the source path is a directory, copy the directory and its contents to the target path in the container.
  • Automatic decompression of files is not supported. If the source file is an archive file (such as .tar or .zip), it will be copied to a normal file.
  • Suitable for most common file copying needs.

ADD Command

  • usage:ADD <source path> <destination path>
  • In addition to copying local files or directories, there are also some other advanced features.
  • If the source path is an archive file (such as .tar or .zip), it will automatically decompress before copying to the container.
  • Supports the use of URLs as source paths, and files can be downloaded from the network and copied into containers.
  • The file added in the build context can be a URL that will be automatically downloaded and copied into the container.
  • Compared withCOPYADDThe command is more functional, but in general, for simplicity and clarity, it is recommended to use itCOPY

To sum up:

  • For most common file copy requirements, useCOPYJust command.
  • Use only if you need to automatically decompress files or download files from URLs and copy them into containersADDOrder.

In the Dockerfile,COPYandADDCommands are case-insensitive, that is, they can be written in uppercase or lowercase.

For example:

  • You can useCOPYorcopyADDoradd, they are all valid commands.
  • Dockerfile is not case sensitive to commands.

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.