1. Home
  2. Linux
  3. How to transfer files to a docker container

How to transfer files to a Docker container

Sometimes you may need to transfer files from a Docker container to the host system for backup purposes, etc. This can be easily accomplished on Linux with the docker cp tool. Here’s how to use it.

xr:d:DAFds8YL9ik:2,j:43601822907,t:23032003

How to transfer files from a Docker container to the host using Docker CP

If you need to transfer files outside of your Docker container to the host system, the fastest and easiest way to do this is with the Docker CP tool. To start, log into your server over SSH, or via a physical monitor with a terminal interface.

Once you’ve logged in, access the Root account. You can do this in two ways. Either with the su command or the sudo -s command.

su

Or

sudo -s

After logging in, run the docker ps command. Running this command will show all running containers. Search for the ID of the container you plan to transfer files from. Then, use the following command to log into the container.

Note: replace $id with your container ID.

docker exec -it $id /bin/bash

Now that you’ve logged into your Docker container using the docker exec command above, you’ll need to navigate the container and compress the files you wish to transfer using Docker CP.

In this example, we’ll use the “test” folder in the /root/ directory. You’ll need to modify this command to suit your needs.

tar -czvf my-archive.tar.gz /root/test/

After compressing the files, exit the container. You can exit the terminal interface in your Docker container by using the following exit command.

exit

Finally, transfer your files from the container to the host system.

docker cp CONTAINER_ID:/path/to/archive.tar.gz /host/system/folder/

Transferring files without compression

While it’s not recommended to transfer files without compressing them first (for safety’s sake), you can. If you’d like to simply transfer a directory containing some files, here’s how to do it.

First, determine the path of the folder by logging into the container. You can log into the container with the commands below.

su
docker exec -it $id /bin/bash

After determining the path to the folder you wish to transfer out, exit the container. Then, run the following docker cp command.

docker cp CONTAINER_ID:/path/to/your/folder/ /host/system/folder/

How to transfer files from the host to a Docker container using Docker CP

In addition to transferring files via the Docker container to the host, it is possible to transfer files from the host back to the container. To it, find the location of the files you wish to transfer to your docker container.

When you’ve located the files you wish to transfer, use the following command to compress them. Compressing the folder makes it easier to transfer.

tar -czvf my-archive.tar.gz /path/to/your/folder/

After compressing your folder into a Tar.GZ archive, you’ll need to get the ID of the container you wish to transfer files to. First, log into root using su or sudo -s.

su

Or

sudo -s

After logging into the root account, run the docker ps command. This command will show information about all running containers. You can then take this information and use the docker cp command to transfer your compressed archive.

docker ps
docker cp  /path/to/my-archive.tar.gz CONTAINER_ID:/path/to/destination/

Transferring files without compression

If you wish to transfer to a container without compressing anything, here’s how to do it. First, log into root using su or sudo -s. After you’ve logged in as root, find the folder path you wish to transfer.

Once you’ve got the folder path, use the docker ps command to get the container ID of the container you wish to transfer files to. Then, use the following docker cp command to copy the files to your system.

docker cp /path/to/my/file/folder/ CONTAINER_ID:/path/to/destination/

How to transfer files from a Docker container from the host with Rsync

Using the docker cp command should be sufficient to transfer files to and from hosts. However, it is also possible to Rsync files from a container to the host. Keep in mind, you must have an SSH server enabled and running on the host machine.

To start, log in as root on your host Linux server via the terminal. You can log into the root account using the su or sudo -s. After logging into the root account, run the docker ps command.

su

Or

sudo -s
docker ps

Locate the Docker container ID, and log in using the command below. Then, install the Rsync tool using the container’s package manager. Be sure to replace “$id” with the container ID.

docker exec -it $id /bin/bash

After you’ve installed Rsync using the container’s package manager, use the Rsync command below to sync files from the container to the host.

rsync -avz -e ssh /path/to/local/files/ user@remote:/path/to/remote/files/