Difference Between the “COPY” and “ADD” Commands in a Dockerfile

Dockerfile is utilized to create an image in the Docker environment. It is an instructions file that includes commands to make a Docker image. However, Docker images can be created from the terminal by executing each instruction or command, which can be hectic. Dockerfile provides different commands, such as “FROM” is utilized to define the base image, “WORKDIR” specify the working directory, “RUN” is used to execute the specified command or statement in the top layer of Docker, and “COPY” and “ADD” commands add the source file to container path.

This blog will distinguish between the Dockerfile “COPY” and “ADD” commands.

Difference Between the Dockerfile’s “COPY” and “ADD” Commands

Both commands work similarly and are used for the same purpose. These commands copy or add the source file to the destination path within a container. The major difference between these two commands is the “COPY” command only copies the file locally to the destination path container. However, the “ADD” command can add files from local and remote sources through URLs.

Moreover, the “ADD” command also supports the “tar” extraction and is widely used for local tar file extraction into containers or images.

How to Use Dockerfile “COPY” and “ADD” Commands?

To utilize the “COPY” and “ADD” commands in Dockerfile, go through the provided examples:

Example 1: “COPY” Command in Dockerfile

The “COPY” command copies the source file locally to the container path.

The syntax for the “COPY” command is as follows:

COPY <src> <dest>

Let’s make a Dockerfile that will contain instructions to deploy a simple Golang application:

FROM golang:alpine

WORKDIR /go/src/app

COPY main.go .

RUN go build -o webserver .

CMD ["./webserver"]

In the above-provided snippet:

  • FROM” is utilized to define the base image.
  • WORKDIR” specifies the container or image working directory.
  • COPY” copies the “main.go” file locally and past it to the container path.
  • RUN” is utilized to execute the specified command on the top-most layer of the Docker container.
  • CMD” sets the default path of execution or entrypoint for a container:

Example 2: “ADD” Command to Dockerfile

The “ADD” statement is used to add or copy the file from the source URL and paste it to the destination address in the container.

The syntax used to specify the “ADD” command in the Dockerfile is given below:

ADD <src URL> <dest>

In the below code block, we have specified the URL to copy the “main.go” file from GitHub and paste it to the container path:

How to Build an Image and Deploy it Using Dockerfile?

To build an image to containerize and deploy the application from Dockerfile, go through the given instruction instructions.

Step 1: Create an Image

Build the new Docker image to containerize the application through the given command:

> docker build -t new-go-img .

Step 2: Run the Image as Container

Run the image to deploy the application through the “docker run” command. This command will automatically generate the container and expose it on port “8080”. Here, the “-d” option is used to execute the container in detached mode, and “-p” specifies the exposing port of the container:

> docker run -d -p 8080:8080 new-go-img

For the confirmation, navigate to the localhost specified port “8080”. Here, we have successfully executed the “main.go” program:

We have distinguished the “COPY” and “ADD” in Dockerfile.


The “COPY” and “ADD” commands are used for a similar purpose. These are used to copy the files from a source location to a container path. However, the “COPY” command copies the file locally, and the “ADD” command adds the file from the src URL to the container. The “ADD” command is also used for local tar file extraction into containers or images. This write-up has distinguished the “COPY” and “ADD” commands in Dockerfile.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.