Skip to content
Go back

Copying Files Between Docker Containers and Host Machines

By SumGuy 5 min read
Copying Files Between Docker Containers and Host Machines

Docker is a powerful platform for developing, shipping, and running applications inside lightweight, portable containers. A common task when working with Docker is transferring files between the container and the host machine. This article will guide you through the process of copying files from a Docker container to a host and vice versa, providing detailed explanations and examples suitable for beginners.

Understanding Docker Containers and Hosts

Before diving into file copying, it’s important to understand the relationship between Docker containers and the host machine:

Prerequisites

Copying Files from Docker Container to Host

To copy files from a Docker container to your host machine, you use the docker cp command. The syntax is as follows:

docker cp <container_id>:<path_to_file_in_container> <path_to_destination_on_host>

Example:

Suppose you have a Docker container running with the ID abc123, and you want to copy a file located at /usr/src/app/data.txt from the container to your host machine’s desktop.

docker cp abc123:/usr/src/app/data.txt ~/Desktop/data.txt

This command will copy data.txt from the specified path inside the container to the Desktop of your host machine.

Copying Files from Host to Docker Container

To copy files from your host machine to a Docker container, you use the docker cp command in a slightly different way. The syntax is:

docker cp <path_to_file_on_host> <container_id>:<path_to_destination_in_container>

Example:

If you want to copy a file from your host machine’s desktop called config.json to the /etc/config directory inside a Docker container with the ID abc123, you would do the following:

docker cp ~/Desktop/config.json abc123:/etc/config/config.json

This command will copy config.json from your Desktop to the /etc/config directory inside the specified container.

Detailed Explanation

Tips for Beginners

Conclusion

Copying files between Docker containers and host machines is a straightforward process once you understand the basic commands and syntax. This functionality is crucial for tasks such as configuration, backups, and when working with data generated by applications running inside containers. By mastering these commands, you can enhance your Docker workflow and manage your containers more effectively.

What Actually Breaks docker cp

The workflow above is solid until it isn’t. Here’s where people hit walls:

The container doesn’t have to be running. docker cp works on stopped containers too — which is actually a lifesaver when something crashed and you need to pull logs out. Use the container ID from docker ps -a (the -a shows stopped ones).

You can’t copy between two containers directly. There’s no docker cp container1:/file container2:/dest shortcut. You copy to the host first, then from the host to the second container. Two commands, no magic.

Symlinks get weird. If the path you’re copying from inside the container is a symlink, docker cp follows it and copies the target, not the link itself. Usually fine, occasionally confusing when you’re debugging why the file landed somewhere unexpected.

Trailing slashes change the behavior. This one bites people constantly:

Terminal window
# Copies the directory itself INTO dest/ → dest/app/file.txt
docker cp mycontainer:/usr/src/app ./dest
# Copies the *contents* of app/ into dest/ → dest/file.txt
docker cp mycontainer:/usr/src/app/. ./dest

That trailing /. is the POSIX directory-contents syntax. It’s not a typo — it’s intentional.

Pulling Logs Out of a Dead Container

This is probably the most practical use case that never gets mentioned in tutorials. Container crashed at 3 AM, docker logs only has so much buffered, but the app wrote to /var/log/app/error.log inside the container. No volume mounted because past-you was optimistic.

Terminal window
# Get the container ID — it's stopped, so use -a
docker ps -a --filter "name=myapp" --format "{{.ID}} {{.Status}}"
# Pull the log out before you rm -f it in frustration
docker cp <container_id>:/var/log/app/error.log ./error.log
# Now you can read it, grep it, share it, cry about it
grep -i "error\|fatal\|panic" ./error.log | tail -50

Your future self will thank you for knowing this exists before you need it.

When docker cp Is the Wrong Tool

If you’re copying the same files repeatedly — configs on every deploy, data files that need to live somewhere persistent — stop and mount a volume instead. docker cp is for one-off operations, not a substitute for proper volume management. Using it in a deploy script is a code smell. Using it to rescue a file at 2 AM is wisdom.


Share this post on:

Send a Webmention

Written about this post on your own site? Send a webmention and it'll show up above once verified.


Previous Post
Understanding CMD and ENTRYPOINT in Dockerfiles
Next Post
Understanding the regreSSHion Vulnerability in OpenSSH

Discussion

Powered by Garrul . Sign in with GitHub or Google, or post anonymously.

Related Posts