Copying Files from a Docker Container to the Host System: A Step-by-Step Guide

In Docker, copying files between a container and the host system is quite a regular task. Especially, when we need to extract configuration files from our running container to use somewhere else. We can even copy logs and other data items as per our requirements. But if you are a beginner or don’t know how to do that, this article is for you.

Learn the simple steps to know how to copy files from a Docker container to the host system in a few commands.

The command that can be used to copy container files to the host is “docker cp“, let’s see the ways and options available to use it.

1. Identify the Container ID or Name:

Well, to copy files from your running container or any existing one we must know either its name or ID. If you already know that then move to the next step, whereas users who don’t can run the given command to list all running docker containers.

docker ps

Note the Container ID or Name associated with the container from which you want to copy some files.

command to list running and stopped docker containers

2. Copy a File from the Container to the Host:

Once you have noted down the ID or name of the container in the previous step, next we use the docker cp command to copy a file or directory from a container to the host system:

Here is the syntax:

docker cp <container_id_or_name>:<container_path> <host_path>

In the above-given command, you to replace <container_id_or_name> with the actual ID or name of the container, whereas <container_path> with the path to the file or directory inside the container, and <host_path> with the destination path on the host system.

For example, we have a container named – webserver, and inside that there is a file called demo.txt under the /opt directory. Now, we want to copy demo.txt to our host’s /tmp directory, so to do all this, we use the above command like this:

docker cp webserver:/downloads/demo.txt /tmp/
Copy a File from the Container to the Host

3. Copy Multiple Files or Directories:

A task doesn’t end with just a single file copying you may need to transfer multiple files from a container to a host, in that case, you can specify the path and name all the files that need to be copied in the same command.

However, we should note that the “docker cp” command doesn’t support copying multiple files from a container to the host in a single command. But we can still do that by using Tar and docker exec commands.

With their help, first, we archive all the files we want to copy and then extract them in our host directory, all this will be done using a single command given below:

docker exec my_container tar -czf - -C /container-directory file1 file2 | tar -xzf - -C /host-directory/
  • my_container is the ID or name of the Docker container.
  • /container-directory is the source directory inside the container.
  • file1 and file2 are the specific files you want to copy from the container.
  • /host-directory/ is the destination directory on the host.

For example:

We have a container named – webserver and from its /opt directory we want to copy three files (demo.txt test.txt example.txt) at once to our host system directory /tmp.

docker exec webserver tar -czf - -C /opt demo.txt test.txt example.txt| tar -xzf - -C /tmp/

The above command was for those who want to copy some specific multiple files from docker to host but if you want to copy all files from a Container’s directory to host then you don’t need to specify them one by one. Instead, use the given syntax.

docker cp my_container:/container/source/directory/. /host/destination/directory/


docker cp webserver:/opt/. /tmp

A dot(.) after that container directory from where you want to copy files to the host will copy all files (including subdirectories and their contents).

4. Copy from a Running Container:

Well, it doesn’t matter if your docker container is in a running or stopped state, the “docker cp” command will work for both.

So the syntax and examples we have discussed in the second point of the article are also applicable to stopped docker containers.

docker cp <container_id_or_name>:<container_path> <host_path>

5. Verify the Copied Files:

After successfully copying the file you would like to check and verify the files have been copied. For that, on your host system, use the ls” command along with the path of the directory where you have transferred the files from the container.


ls /host/path/directory


ls /tmp

This command lists the contents of the /tmp directory on the host, including the copied file.


The “docker cp” command is quite handy when we want to transfer some data from our existing old or new container to the host. Whether it is about inspecting some logs files, extracting configurations, or any other data the above-given methods of this tutorial will surely help you.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.