Docker containers are lightweight small executable packages containing all the essential components to run the project, such as source code, dependencies, libraries, and configuration settings. But sometimes, users want to share data, files, and configuration settings among the containers for the smooth execution of containerized applications and that process needs to copy the files from one container to another container.
This article will demonstrate the method of copying a file from one container to another Docker container.
How to Copy a File From One Container to Other Docker Container?
Usually, Docker users need to copy a file from one container to another to share data, settings, and code. But copying a file from one container to another cannot be directly done in Docker through the Docker “cp” command. When users directly try to copy the file to another container, they may get an error like “copying between containers is not supported” as shown below:
To copy a file from one container to another, go through the given procedure.
Step 1: Make a Dockerfile
Create the Dockerfile that will be used to make the first Docker container. In the given snippet, we have utilize the “nginx” image as a base image. Then, the “RUN” command is utilized to create the “index1.html” file on the “/usr/share/nginx/html/index1.html” container path:
RUN echo "<h1>Welcome to Linuxhint application</h1>" >
/usr/share/nginx/html/index1.html
VOLUME /usr/share/nginx/html
Step 2: Create Docker Image
Next, create the snapshot of the container from the Dockerfile through the “docker build” command. The container image or snapshot is basically a template of Docker that creates the container:
Step 3: Make and Run First Container
Execute the newly created image to create the first container using the “docker run” command:
In the above command, “–name” is utilized to specify the container name, and “-p” is providing the exposing port for the container:
For confirmation, navigate to the “localhost/index1.html” and check if the “container1” is executing or not:
The above output indicates that we have successfully executed the “container1”.
Step 4: Check Files Inside the Container
Next, check the files of the container by visiting the containers directory. For this purpose, simply use the “docker exec” command along with the container name or id:
Next, to check the files of any directory utilize the “ls” command along with the directory path:
The output shows that the “usr/share/nginx/html” directory contains the three files:
Let’s create the second container and copy the “index1.html” file of the first container into the second container.
Step 5: Create and Start Second Container
To create the second container, again use the “docker run <image-name>” command. For instance, we have directly used the “nginx:latest” image to create the second container:
Again execute the “docker exec” command with the second container name:
Then, utilize the “ls <directory-path>” command to check the files of the second container on the specified container path:
The output shows that container2 includes only 2 files:
Step 6: Copy “index1.html” File From First Container to Host
To copy the “index1.html” first from first to container to second, users are required to copy the file in the local directory first. Then, copy the container’s file from the local directory to the second container. For this purpose, utilize the “docker cp <first container-name>:<container-path to file> <local-directory path>” command:
For instance, we have copied the “index1.html” file from container1 to local directory bath “./Temporary/”:
Step 7: Copy “index1.html” File From Host to Second Container
Next, copy the file from local directory to container2 using “docker cp <local-directory-path to file> <second container-name>:<container-path>” command:
Here, we have copied the “index1.html” file from the “Temporary” local directory to the “usr/share/nginx/html” container path of “container2”:
For confirmation, again check the “usr/share/nginx/html” path of the second container and verify if the file is copied from one container to another or not:
The output indicates that we have successfully copied the “index1.html” file from “container1” to “container2”:
Note: If direct copying of files between containers is not supported, you are required to copy the file in the local directory first. Then, copy the file from the local directory to the second container path.
That’s all about copying files from one Docker container to another.
Conclusion
To copy the file from one container to another container, first, copy the file from container1 to the local directory or on the host machine using the “docker cp” command. Then, copy the file from the local directory to the second container. This article has demonstrated the solution to copy files from one container to another.