In Docker, there are two ways to copy a file, namely, ADD and COPY. Though there is a slight difference between them in regard to 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. If you want to copy files and directories inside a Docker Container from your Local machine, you can use the COPY instruction inside your Dockerfile. The general form of a COPY instruction is:
Syntax: COPY <src-path> <destination-path>
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.
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.
Lost in the complex landscape of DevOps? It's time to find your way! Enroll in our DevOps Engineering Planning to Production
Live Course and set out on an exhilarating expedition to conquer DevOps methodologies with precision and timeliness.
What We Offer:
- Comprehensive DevOps Curriculum
- Expert Guidance for Streamlined Learning
- Hands-on Experience with Real-world Scenarios
- Proven Track Record with 100,000+ Successful DevOps Enthusiasts