How do I copy a directory into a docker container?

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 seamless 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.

 Copying files from Docker Container to Local Machine

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

How do I copy a directory into a docker container?

Creating a Container

Step 2: Create a File inside Container

echo "geeksforgeeks" > geeksforgeeks.txt

How do I copy a directory into a docker container?

Creating File

Step 3: Get the Container ID

sudo docker start my-container sudo docker container ls

How do I copy a directory into a docker container?

Copying Container ID

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 do I copy a directory into a docker container?

Output

Copying files from Local System to Docker 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

How do I copy a directory into a docker container?

File to copy

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 of Docker Container (Destination).

sudo docker cp ~/Desktop/to-be-copied.txt 135950565ad8:/to-be-copied.txt

How do I copy a directory into a docker container?

Copy Command

Step 3: Verify the copied file

To start the Container, use the following command.

sudo docker exec -it my-container /bin/bash

How do I copy a directory into a docker container?

Verifying the Output

How do I copy a directory to a Docker container?

Copying Files with the docker cp Command.
Open a terminal on your local machine..
Create a file named myfile. ... .
Execute the docker run command. ... .
Verify if the Docker container is successfully created using the docker ps command. ... .
Next, execute the docker cp command..

How do I copy a folder from local to container?

Copying files from Docker Container to Local Machine.
Step 1: Create a Docker Container. ... .
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..

How do I move a directory in Docker?

Procedure.
Stop the Docker services: ... .
Create the necessary directory structure into which to move Docker root by running the following command. ... .
Move Docker root to the new directory structure: ... .
Edit the file /etc/docker/daemon.json. ... .
After the /etc/docker/daemon.json file is saved and closed, restart the Docker services:.

How do I copy a file into a container?

Solution.
To copy a file from the local file system to a container, run the command for Docker container or Kubernetes pod, respectively: docker cp : ... .
To copy a file from the container to the local file system, use: docker cp : .