Sobes.tech
Middle

How do the directives ADD and COPY differ in a Dockerfile?

sobes.tech AI

Answer from AI

In Dockerfile, the directives ADD and COPY are used to copy files into the image, but they have differences:

  • COPY — a simple and predictable command that copies files and directories from the build context into the image. It does not perform any additional actions.

  • ADD — more powerful, and besides copying files, it can:

    • Automatically unpack local archives (e.g., tar.gz) during copying.
    • Copy files from a URL (download from the internet).

It is recommended to use COPY if you just need to copy files, and ADD if automatic unpacking or URL downloading is needed.

Example:

# Copy a local file
COPY ./app /app

# Add and unpack an archive
ADD ./archive.tar.gz /app

# Download a file from URL
ADD https://example.com/file.txt /app/