Jun 15, 2026 · 2 min read

Mastering docker cp: Transfer Files In and Out of Running Containers

Introduction

Ever needed to edit a file inside a running container without rebuilding the image? In this quick tip we’ll master docker cp to move files instantly, letting you patch containers on the fly and keep your images immutable.

How docker cp Works

docker cp copies files or directories between the Docker host filesystem and a container’s filesystem. The basic syntax is:

docker cp <source_path> <container_name>:<destination_path>

You can also reverse the direction by swapping the source and destination, and the command works with both files and whole directories.


Copying Files from Host → Container

Simple file copy

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

Configuration file example (from the video)

docker cp ./app/config.yaml my_container:/app/config.yaml

Copying a directory with metadata preservation

docker cp -a ./data my_container:/data

The -a flag preserves permissions, timestamps, and ownership, making the copy behave like a traditional cp -a.

Copying Files from Container → Host

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

This pulls a log file out of the container for local analysis.


Advanced Options

Option Description
-a Archive mode – preserves permissions, timestamps, and ownership.
-L Follow symbolic links in the source path.

You can combine them as needed, e.g. docker cp -aL ./mydir my_container:/app/mydir.


Pitfalls to Avoid

  1. Ephemeral changes – Files copied into a running container disappear when the container is recreated from its image. For persistent data use a volume mount.

  2. Large files – Copying big assets at runtime can delay container start‑up. Consider baking them into the image or mounting them.

  3. Path confusion – Remember the colon (:) separates the container name from the internal path. Omitting it will make Docker look for a local path that matches the container name.


Best Practices

  • Use volumes for regular data sharing: docker run -v $(pwd)/data:/data my_image keeps data outside the container lifecycle.

  • Reserve docker cp for one‑off migrations, debugging, or hot‑patches where rebuilding the image would be overkill.

  • Preserve metadata with -a when copying executables or configuration directories that rely on specific permissions.

  • Validate copy results: After copying, exec into the container (docker exec -it my_container sh) and list the files to ensure the operation succeeded.


Conclusion

docker cp is a lightweight, powerful tool for instant file transfers between your host and a running container. By understanding its syntax, options, and limitations, you can streamline debugging, perform quick patches, and avoid unnecessary image rebuilds. Use it wisely alongside volumes for a balanced, maintainable Docker workflow.

Related Posts

Useful Linux Commands for Logs and File Search

A production-style walkthrough of find, tail, journalctl, grep, and awk for server troubleshooting.

Jul 06, 2026

Demystifying Kubernetes Service Discovery

Unlock the full potential of Kubernetes with our comprehensive guide to service discovery, enabling seamless communication between services within your cluster.

Jul 05, 2026

Harnessing the Power of Kubernetes: Pods and Deployments Demystified

Discover how Kubernetes Pods and Deployments provide powerful tools for managing and scaling applications efficiently within containerized environments.

Jul 04, 2026

Effortlessly Set Up Your First Kubernetes Cluster on AWS EKS

Learn how to effortlessly set up your first Kubernetes cluster on AWS EKS with this step-by-step guide, perfect for developers aiming to leverage cloud scalability.