While working on a Docker project, you might require copying files to and from Docker Containers and your Local Machine. Once you have built the Docker Image with a particular Docker build context, building it again and again just to add small files or folders inside the Container might be expensive because usually, Docker Images are of very large sizes.
Docker provides us with very helpful copy commands which allow us to seamlessly copy files to and from Docker Containers and Local Systems. In this article, we will discuss how to use the Docker cp commands using practical examples.
How To Docker Copy Files Container To Host?
Method 1: Using the Docker cp Command
Follow the below steps to copy a file from a docker container to a local machine:
Step 1: Create a Docker Container.
sudo docker run -it --name my-container ubuntu
Step 2: Create a File inside Container
echo "geeksforgeeks" > geeksforgeeks.txt
Step 3: Get the Container ID
sudo docker start my-container
sudo docker container ls
Step 4: Copy the file to your Local System
You can use the docker cp command to copy the file.
sudo docker cp 135950565ad8:/geeksforgeeks.txt ~/Desktop/geeksforgeeks.txt
The first path (Source) is the path in the Docker Container and the second one is the path inside your Local System (Destination).
How To Docker Copy Files Host To Container ?
Follow the below steps to copy files from a local machine to the Docker container:
Step 1: Create a file on your local machine
Step 2: Copy the File to the Container
You can use cp instruction. The path on the left should be on the local machine (Source) and on the right should be on Docker Container (Destination).
sudo docker cp ~/Desktop/to-be-copied.txt 135950565ad8:/to-be-copied.txt
Step 3: Verify the copied file
To start the Container, use the following command.
sudo docker exec -it my-container /bin/bash
Method 2: Mounting a Host Directory
You can copy the files to the docker container from the host machine by mounting a directory of the host machine to the docker container. By using the below command you can perform that.
docker run -d -v <Host path to mount> <name of the container and image name>
Method 3: Using The Cat Command
If you want to copy the files from the host machine to the docker container, You can do that by using the “cat” command. Along with the “docker exec” command as shown in the figure below.
docker exec -i <container name or container ID> sh -c ‘cat > <Container path>’ < Host path>
Method 4:Using The tar Command
First, create a tar file by using the tar command of the file or directory which you want to copy. By using the below command.
tar -cvf <file name>
After creating a tar file know copy the tar file into the container from the host. By using the below command.
docker cp file.tar.gz <Container name:path of container>
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses
are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!