Open In App

Docker – COPY Instruction

Last Updated : 19 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Docker, there are two ways to copy a file: ADD and COPY. Though there is a slight difference between them regarding the scope of the functions, they more or less perform the same task. In this article, we will primarily focus on the COPY instruction of Docker. So, before discussing the COPY instruction in detail, first try to understand the Docker Copy Command.

What is Docker Copy Command?

The main reason for introducing the docker copy command is to address the issues faced by developers while using the Docker ADD Command. There was an issue of automatic extraction of compressed files during the build process, which caused confusion and led to broken Docker images. This situation comes when a developer does not fully understand how the ADD command behaves.

The Docker Copy command is only used to copy files and directories as it is as they are into the Docker image. When you try to copy a compressed file(like .zip or .tar.gz), it remains compressed and it won’t be unpacked automatically.

Also, Copy command only works on those files that are already present on your computer. You can’t use it to download files from the internet using a URL.

The general form of a COPY instruction is:

Syntax: COPY <src-path> <destination-path>
  • <src-path>: used to show the source file or directory on your local machine.
  • <destination-path>: used to show the target path inside the Docker image.

In this article, we will discuss how to use the COPY Instruction to copy files and directories inside a Docker Container. To do so, follow the below steps:

Step 1: Create a Directory to Copy

In this example, we will create a directory and a file, which we will copy using the COPY command. Create a folder and inside it create a file called “dockerfile“, which we will edit in the next step. Create another folder in the same directory where you have created the Dockerfile and a file inside it. We will copy this folder to our Docker Container. The final directory structure will be –

file to be copieddockerfile

Step 2: Edit the Dockerfile

After you have created the directory structure, edit the Dockerfile that we created in the previous step.

FROM ubuntu:latest
RUN apt-get -y update
COPY to-be-copied .

In the above Dockerfile, we have tried to pull the Ubuntu base image OS with the latest tag and run an update inside the Container. We have then included the COPY instruction to copy the directory created previously.

Step 3: Build the Docker Image

After creating the Dockerfile, we can now build the Docker Image using the Docker Build command.

sudo docker build -t sample-image .

docker build

Step 4: Verifying the Docker Image

After you have built the Docker Image, you can verify it by using the Docker Images command to list all the images in your system.

sudo docker images

docker images

Step 5: Running the Docker Container

After you have built the Docker Image with the COPY Instruction, you can now run the Docker container using the Docker RUN command.

sudo docker run -it sample-image bash

Copy command

Step 6: Verify the Copying of the Directory

You can now verify whether the directory has been copied or not by listing the directories inside the Container.

verifing the copy action

Why use COPY instead of ADD in the Dockerfile

With the help of following table you can easily compare and understand why use COPY instead of ADD in the dockerfile:

Feature/Aspect COPY Command ADD Command Why Prefer COPY?
Basic Function Copies files/directories as-it-is Copies files and also handles archives and URLs COPY is simpler and more predictable
Compressed File Handling Does not extract files Automatically extracts compressed files like .tar.gz Prevents unintentional file extraction
URL Support Not supported Can fetch files from remote URLs Avoids unexpected external dependencies
Clarity in Purpose Meant only for copying local files Dual purpose (copy + extract/fetch) Clearer intent and easier to understand
Security No risk of unintended downloads or extraction May pull files from unknown sources More secure and controlled
Best Practices Recommended for most use cases Use only when special features are needed Follows official Docker recommendations

Also read: Docker ADD vs COPY Command in Dockerfile

Conclusion

In this article we learnt about Docker Copy command from the basics,we also gives you a proper step-by-step demonstration how you can use this docker copy instruction with an example. We also discussed the difference between COPY and ADD commands. While both are used to add files to a Docker image, COPY is simpler, more secure, and easier to understand. It avoids the automatic extraction and URL download features that ADD supports, which can sometimes lead to confusion or unwanted behavior. By using COPY, your Docker builds will be cleaner, more predictable, and easier to maintain.



Next Article
Article Tags :

Similar Reads