Sobes.tech
Junior — Middle

What is the difference between COPY and ADD commands when creating Docker images?

sobes.tech AI

Answer from AI

The COPY and ADD commands in Dockerfile are used to copy files and directories from the build context into the image, but they have differences:

  • COPY is a simple command that copies files and directories from the local build context to a specified path inside the image. It does not perform any additional actions.

  • ADD is a more powerful command that, besides copying local files, can:

    • Extract archives (e.g., .tar, .tar.gz) directly into the image.
    • Copy files from URLs (download remote files).

It is recommended to use COPY if you just need to copy files, as it is more predictable and safe. Use ADD if you need archive extraction or URL download functionality.

Example:

# Copy a local file
COPY ./app /app

# Extract an archive into the image
ADD archive.tar.gz /app/