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:
Let’s make a Dockerfile that will contain instructions to deploy a simple Golang application:
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:
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:
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:
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.
Conclusion
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.