Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Copying files from Docker container to Host
We can use the Docker build command to build Docker images using a build context. The build context contains all the files which are required to create your containerized application environment. This includes the Dockerfile to build the Docker images, source code of the application, Dockerignore files, all the files, and directories that you want to be there beforehand in the container when you run it.
However, it often happens that you might want to copy some files from the container to the host machine. For example, if you are working on an application inside a Docker container and you have generated some log files or outputs inside the container. You might want to share the results with your team. In such a case, you will have to get access to those files from the container to the host machine.
There are two ways to do this. The first one is to mount directories in your host machine as volumes. However, for sharing simple files or directories, it might be unnecessary to create volumes. The second method is by using the Docker cp command to copy files from Docker containers to the host machine.
Docker cp Command
We can use the Docker cp command to copy files and directories from the source path to the target path. We can use this command to copy files from local machines to Docker containers and vice-versa. The container which is involved in this process can be both stopped or running. By default, the Docker cp command will assume that all the paths mentioned inside the container are relative to the container's root directory (/).
This means that even if we don't supply a forward slash, it is optional. Hence, the paths /user/app/folder1 and user/app/folder1 are identical. However, the paths that we specify for local machines can be both relative as well as absolute. The path in the host machine is relative to the current working directory.
Syntax
The syntax of the Docker cp command to copy files from Docker containers to host machines is −
$ docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
Where −
-
CONTAINER− Name or ID of the Docker container -
SRC_PATH− Source path inside the container -
DEST_PATH− Destination path on the host machine -
[OPTIONS]− Additional flags like-Lto follow symbolic links
Example − Copying Files from Container to Host
Let's check out an example to create a file inside a Docker container, include some content in it, and copy it into the host machine using the Docker cp command.
Step 1: Create and Access Ubuntu Container
First, let's create an Ubuntu container and access its bash −
$ docker run -it --name=myubuntu ubuntu bash
In the above command, we have used the Docker run command to pull and run an Ubuntu container. We have used the -i (interactive) and -t (pseudo-TTY) options along with the Docker run command to access the container in interactive mode.
Step 2: Create File Inside Container
Now that we have access to the container's bash, we can use the echo command to create a file with some content inside the container −
# echo "Welcome to TutorialsPoint" > TutorialsPoint.txt
You can verify the creation of the file by listing the files and using the cat command to list the contents −
# ls # cat TutorialsPoint.txt
TutorialsPoint.txt Welcome to TutorialsPoint
Step 3: Copy File to Host Machine
Now, open another terminal. You can either keep this container running or exit from it. In the new terminal, let's execute the Docker cp command to copy the TutorialsPoint.txt file from the ubuntu container to the host machine −
$ docker cp myubuntu:TutorialsPoint.txt /home/user/Desktop/
In the above Docker cp command, we have specified the name of the container and the path of the file inside the container separated by a colon. We have also specified the destination path inside the host machine. After executing this command, the file will be copied to the destination path.
Additional Examples
Copying Directories
To copy an entire directory from container to host −
$ docker cp myubuntu:/var/log/ ./container-logs/
Copying from Host to Container
To copy files from host to container (reverse direction) −
$ docker cp ./local-file.txt myubuntu:/home/
Limitations and Considerations
Please note that we are not allowed to copy some sensitive files such as system files. These include resources under /dev, /proc, /sys, tmpfs, and certain mounts in the container that are created by the user. A workaround to this is by manually executing tar commands inside Docker containers using the Docker exec command.
| Method | Use Case | Advantages | Disadvantages |
|---|---|---|---|
| Docker cp | One-time file transfers | Simple, works with stopped containers | Not real-time, manual process |
| Volume Mounts | Continuous file sharing | Real-time sync, automatic | Requires planning during container creation |
| Bind Mounts | Development workflows | Direct host filesystem access | Platform-dependent paths |
Conclusion
The Docker cp command provides a simple and effective way to copy files and directories between Docker containers and host machines. It works with both running and stopped containers, making it ideal for one-time file transfers, log extraction, and sharing generated outputs. For continuous file sharing needs, consider using volume mounts instead.
