Docker – COPY Instruction
Last Updated :
19 Apr, 2025
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 –


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 .

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

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

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.

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.
Similar Reads
Docker - ADD Instruction
If you want to extract a TAR file inside a Docker Container or copy files from a URL or local directory, you can specify ADD Instructions inside your Dockerfile. This is different from COPY instruction because COPY instruction only allows you to copy files and directories from the local machine. In
2 min read
Docker - ARG Instruction
You can use the ARG command inside a Dockerfile for defining the name of a parameter and its default value. This default value can also be overridden using a simple option with the Docker build command. The difference between ENV and ARG is that after you set an environment variable using ARG, you w
5 min read
Docker - USER Instruction
By default, a Docker Container runs as a Root user. This poses a great security threat if you deploy your applications on a large scale inside Docker Containers. You can change or switch to a different user inside a Docker Container using the USER Instruction. For this, you first need to create a us
2 min read
Docker - LABEL Instruction
Labels are used in Dockerfile to help organize your Docker Images. Labels are key-value pairs and simply adds custom metadata to your Docker Images. Some key points associated with the LABEL instructions are as follows: To include spaces inside a label, you can use quotes.For multi line labels, you
2 min read
Docker - EXPOSE Instruction
The EXPOSE instruction exposes a particular port with a specified protocol inside a Docker Container. Â In the simplest terms, the EXPOSE instruction tells Docker to get all the information required during the runtime from a specified port. These ports can be either TCP or UDP, but it's TCP by defaul
5 min read
Docker - WORKDIR Instruction
In Docker, organizing and managing file paths within a container is key to building efficient and easy-to-maintain applications. One way to streamline this is by using the WORKDIR instruction in your Dockerfile. Setting the working directory helps control where commands run, making your Dockerfile m
6 min read
Installation of Docker Compose
Docker Compose is a powerful tool that helps in simplifying the orchestration of multi-container docker applications. In this we have to configure the application services inside the Yaml file and docker-compose will read the configuration data from that Yaml script. Before Moving into the installat
4 min read
Docker - Continuous Integration
Continuous Integration ( CI ) with Docker improves the productivity of software development. Docker make the applications portable and independent of the system making its environment uniform. Development of the pipelines can be improved with CI technology tools like Jenkins which automates building
8 min read
What Is Docker Compose Up?
Docker Compose is a powerful tool utilized for defining and running multi-container Docker applications. It improves on the most common way of managing complex applications composed of multiple interconnected services by allowing you to characterize their configuration in a single YAML file. With Do
15+ min read
Docker Compose Restart Policy
The capability of delivering application availability and resilience in the dynamic world of software development is enormous. Docker Compose is among the powerful tools in the Docker ecosystem that make multi-container application management very easy by letting developers describe services, networ
8 min read