Copying Files from Docker Container to Host: A Quick Guide

Whether you’re debugging, backing up logs, or saving generated files, you might need to copy files from a Docker container to your host machine. Fortunately, Docker makes this easy with the docker cp command.

In this guide, we’ll walk you through how to safely and effectively copy files from a container to your local system.


🔧 Command: docker cp

The basic syntax is:

docker cp <container_id>:/path/in/container /path/on/host

This works similarly to the Unix cp command, but with Docker-specific targeting.


📥 Example: Copy a File from Container to Host

Let’s say you have a running container with the ID abc123 and you want to copy a log file from /app/logs/output.log inside the container to your desktop:

docker cp abc123:/app/logs/output.log ~/Desktop/output.log

✅ This will place output.log directly on your desktop.


📁 Example: Copy a Folder from Container to Host

To copy an entire directory (e.g., /app/logs) to your local machine:

docker cp abc123:/app/logs ~/Desktop/

This copies the logs directory and all its contents to your desktop.


🔍 Finding the Container ID or Name

To get the container’s ID or name:

docker ps

Example output:

CONTAINER ID   IMAGE         COMMAND                  NAMES
abc123         myapp:latest  "python app.py"          my-running-app

You can use either abc123 or my-running-app in the docker cp command.


🧱 Note: You Don’t Need the Container to Be Running

You can copy files from stopped containers too. The container just needs to exist.

docker cp <stopped_container>:/some/file.txt ./file.txt

⚠️ Gotchas to Avoid

  • Ensure the source path exists in the container — otherwise, you’ll get an error.
  • If copying directories, be careful with trailing slashes, as they affect the target folder structure (just like with rsync or cp).

🧠 Summary

TaskCommand
Copy file from container to hostdocker cp container:/path/in/container /host/path
Copy folder from containerdocker cp container:/container/folder /host/folder
Works with stopped containers?✅ Yes

🧼 Bonus: Copy Host to Container

The reverse is also possible:

docker cp /host/file.txt container:/path/in/container/

✅ Final Thoughts

Copying files from a Docker container to your host is a common task in development and production workflows. Whether you’re exporting logs, saving generated assets, or debugging, docker cp gets the job done quickly and cleanly.

Sharing Is Caring:

Leave a Comment