Docker

Docker cp

“It is no doubt that docker has revolutionized the modern developer environment. The ability to package your applications in a single bundle and ship them to any platform is nothing short of revolutionary.

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

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:

docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-

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:

$ docker cp host_path container_id:path

The same command syntax applies when you want to copy files from the container to the host system:

$ docker cp container_id:src_path host_path

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.

$ docker ps

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:

docker cp ~/Desktop/backups.tar debian11:/home

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:

$ docker exec -ti debian11 /bin/bash

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:

[email protected]:/# ls -la /home
total 7188
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:

docker cp ~/Desktop/backups.tar f657ea827600:/home

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:

$ sudo docker cp debian11:/var/log/ ~/Desktop/logs

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:

$ ls -ls ~/Desktop/logs
total 72
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 host_path container:path
$ docker cp -a container:path host_path

An example is as shown:

sudo docker cp -a debian11:/var/log/ ~/Desktop/logs

We can see the permissions are similar to one of the copied files in the container.

Conclusion

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!!

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list