0
0
DockerHow-ToBeginner · 3 min read

How to Copy File from Docker Container Easily

Use the docker cp command to copy files from a container to your local machine. The syntax is docker cp <containerId>:<path_in_container> <path_on_host>.
📐

Syntax

The docker cp command copies files or folders between a container and the local filesystem.

  • containerId: The ID or name of the running container.
  • path_in_container: The full path of the file or folder inside the container.
  • path_on_host: The destination path on your local machine.
bash
docker cp <containerId>:<path_in_container> <path_on_host>
💻

Example

This example copies a file named log.txt from the container mycontainer located at /var/log/log.txt to the current directory on the host.

bash
docker cp mycontainer:/var/log/log.txt ./log.txt
⚠️

Common Pitfalls

  • Using the wrong container ID or name will cause an error.
  • Specifying a wrong path inside the container will fail to find the file.
  • Not having permission to write to the destination folder on the host will cause a permission error.
  • Remember to include the colon : after the container ID to separate it from the file path.
bash
docker cp mycontainer/var/log/log.txt ./log.txt  # Wrong: missing colon

docker cp mycontainer:/var/log/log.txt ./log.txt  # Correct
📊

Quick Reference

CommandDescription
docker cp : Copy file/folder from container to host
docker cp :Copy file/folder from host to container
docker psList running containers to get container ID or name

Key Takeaways

Use docker cp with container ID and file path to copy files from container to host.
Always include a colon : after the container ID to separate it from the file path.
Check container ID and file paths carefully to avoid errors.
Ensure you have write permission on the destination folder on your host machine.
You can also copy files from host to container by reversing the source and destination.