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

What is the difference between the ' COPY ' and ' ADD ' commands in a Dockerfile?

1个答案

1

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:

  1. 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 of COPY, ADD supports 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.
  2. Use Cases:

    • COPY is the recommended command when you need to simply copy local files into the image; prefer using COPY in such cases.
    • ADD can be used in specific scenarios, such as when you need to automatically decompress files or download files from the internet into the image.
  3. Examples:

    • Using the COPY command:

      dockerfile
      COPY ./app /usr/src/app

      This command copies the local app directory into the /usr/src/app path in the image.

    • Using the ADD command:

      dockerfile
      ADD https://example.com/example.tar.gz /var/www

      This command not only downloads the example.tar.gz file but also automatically decompresses it into the /var/www directory.

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 回复

你的答案