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.
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.
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.
- Docker - USER Instruction
- Docker - HEALTHCHECK Instruction
- Docker - WORKDIR Instruction
- Docker - ARG Instruction
- Docker - EXPOSE Instruction
- Docker - LABEL Instruction
- Docker - ADD Instruction
- How to Install and Configure Docker in Ubuntu?
- Setup Web Server Over Docker Container in Linux
- Create your own secure Home Network using Pi-hole and Docker
- How to create your own Docker Customised Image ?
- Docker compose tool to run multi container applications
- Introduction to Docker
- How to Build a Web Server Docker File?
- How to Map Ports in Docker?
- Running GUI Applications on Docker in Linux
- Difference between RUN vs CMD vs ENTRYPOINT Docker Commands
- Creating a Network in Docker and Connecting a Container to That Network
- Backing up a Docker Container
- How to Run a Python Script using Docker?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.