Docker Course
Docker
/
Intermediate

COPY vs ADD

Definition

Instructions used to copy files from the host machine (your laptop) into the Docker image. `COPY` is the standard tool. `ADD` is an older tool that does the same thing but has hidden extra features.

Explain Like I'm New

Taking the source code folder sitting on your Macbook desktop and permanently dragging-and-dropping it inside the Docker Image.

Real World Example

`COPY ./src /app/src`. This copies the 'src' folder from your laptop and pastes it into the '/app/src' directory inside the container.

Common Use Cases

  • •Importing source code
  • •Injecting configuration files

Interview Questions

basic

  • Which command is considered best practice for normal file copying: `COPY` or `ADD`?

intermediate

  • What are the 'hidden features' of the `ADD` command?

Flash Cards

Question

Which is best practice?

Click to reveal answer
Answer

`COPY`. It is completely transparent and does exactly what it says. `ADD` should generally be avoided unless you specifically need its extra features.

Question

What hidden features?

Click to reveal answer
Answer

`ADD` can accept a URL to download a file from the internet directly into the image. Furthermore, if you `ADD` a compressed `.tar.gz` file, it will automatically unzip it inside the container.