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:
-
COPYis 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. -
ADDis 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).
- Extract archives (e.g.,
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/