How to Copy Files from a Docker Container to the Host

Working with Docker containers often involves manipulating files—whether it’s exporting logs, downloading generated assets, or retrieving data for debugging. Fortunately, Docker provides a simple way to copy files from inside a container to your local host system.

In this guide, you’ll learn how to copy files or folders from a Docker container to the host, with clear examples and best practices.


✅ Method 1: Using docker cp Command

The easiest and most common way to copy files from a running (or even stopped) container to the host is with the docker cp command.

Syntax:

docker cp <container-id>:<container-path> <host-path>

📁 Example: Copy a File from Container to Host

Suppose you have a container named my_container, and you want to copy a file located at /app/output.txt to your local machine:

docker cp my_container:/app/output.txt ./output.txt

This command will copy the file to your current working directory on the host.


📁 Example: Copy a Folder

To copy an entire directory from the container to the host:

docker cp my_container:/var/logs ./logs

This copies the /var/logs folder inside the container to a folder named logs on your host.


🔍 How to Find the Container Name or ID

To list your running containers:

docker ps

To list all containers (including stopped ones):

docker ps -a

Use the value from the CONTAINER ID or NAMES column in the docker cp command.


✅ Method 2: Using docker exec + cat (for Quick File Reads)

If you just want to view or stream a file’s content rather than saving it, you can use docker exec and cat:

docker exec my_container cat /app/output.txt

Or to save it:

docker exec my_container cat /app/output.txt > output.txt

This method is handy for small text files or logs.


✅ Method 3: Archive & Transfer (Advanced)

To compress multiple files or folders inside the container and transfer them as a single archive:

docker exec my_container tar czf - /app/data | tar xzf - -C .

This command:

  • Creates a .tar.gz archive inside the container
  • Streams it to the host
  • Extracts it into the current host directory

Great for moving large or nested directory structures.


🧠 Best Practices

  • Ensure the container path exists before copying; otherwise, Docker will throw an error.
  • The docker cp command works even for stopped containers.
  • Be careful when copying files into host system directories—avoid overwriting important files.
  • For frequent file transfer, consider using volumes instead of copy commands.

✅ Summary

TaskCommand Example
Copy a file from container to hostdocker cp my_container:/app/file.txt ./file.txt
Copy a folderdocker cp my_container:/app/folder ./folder
Read a file without savingdocker exec my_container cat /path/to/file
Copy multiple files as archive`docker exec my_container tar czf – /path

By mastering Docker’s file copy commands, you’ll make your workflows more efficient—especially when extracting logs, backups, and configuration files.

Sharing Is Caring:

Leave a Comment