But enough praises about docker. In this article, we will discuss how to copy files to and from a container to the host system and vice versa.”
Let’s get started
Docker cp is a command-line utility that allows you to copy files and directories between systems. The command takes on a very simple syntax that enables easy workflow.
The command syntax:
The cp utility will copy the files specified from the SRC_PATH to the DEST_PATH. This allows you to copy from the container into the host system and vice versa.
The container can be running or stopped state, and the command will perform the copy operation successfully.
If a directory is specified, the command will copy the files recursively into the specified destination.
To copy files from the host system to a container, you can use the syntax:
The same command syntax applies when you want to copy files from the container to the host system:
Let us see how we can accomplish this with practical examples.
Get Running Containers
The first step is to get the names and ids of the running containers. We can do this with the docker ps command.
Once you get the container and name or ID, we can proceed. You can get the id in the CONTAINER ID and NAMES columns.
Copy Files From Host System to Container
Suppose we have a file called backups.tar on the Desktop of the host system and want to copy it to the debian11 container.
We can run the command:
The command above will copy the backups.tar file into the home directory of the specified container.
We can verify that the file exists by running the command:
The command above should allow you to login into the container and spawn a bash shell.
You can now list the files and directories in the container as:
drwxr-xr-x 1 root root 4096 Jun 7 02:09 .
drwxr-xr-x 1 root root 4096 Jun 7 02:09 ..
-rw-r--r-- 1 root root 7348816 Jun 7 02:08 backups.tar
We can see that the file has been successfully copied into the container.
If you wish to use the container id, you can run the command:
NOTE: If a file with a similar name exists as the specified destination path, the command will overwrite it without prompt.
Docker Copy File From Container to Host
We can also use the command to copy files from the container into the host system. For example, suppose we want to copy the logs from the container to the logs directory in the host’s Desktop folder.
We can run the command:
In the command above, we tell the docker cp command to copy the files from the /var/log directory in the debian11 to the logs directory in the host system.
We can verify this by running the command:
0 drwxr-xr-x 3 root staff 96 May 27 03:00 apt
0 -rw-rw---- 1 root staff 0 May 27 03:00 btmp
8 -rw-r--r-- 1 root staff 3232 May 27 03:00 faillog
64 -rw-rw-r-- 1 root staff 29896 May 27 03:00 lastlog
0 -rw-rw-r-- 1 root staff 0 May 27 03:00 wtmp
Docker cp Preserve Attributes
You will notice that once we copy a file to and from the host system, docker does not preserve the attributes such as permissions, UIDs, etc.
We can resolve this by appending the -a flag in the docker cp command.
$ docker cp -a container:path host_path
An example is as shown:
We can see the permissions are similar to one of the copied files in the container.
In this tutorial, you learned how to use the docker cp command to copy files to and from a docker container to the host system and vice versa.
Thanks for reading!!