docker cp Explained

What docker cp does, with an example.

Published November 9, 2026

docker cp copies files and folders between a container and the host filesystem, in either direction.

How it works

It works on running or stopped containers and does not need a volume. Use it for quick extraction or injection of files.

For repeatable workflows, prefer volumes or build-time COPY.

Try it

docker cp web:/etc/nginx/nginx.conf ./nginx.conf
docker cp ./index.html web:/usr/share/nginx/html/

Practical examples

Container to host

docker cp web:/var/log/app.log ./app.log

Host to container

docker cp ./config.json web:/app/config.json

Copy a folder

docker cp web:/app/uploads ./uploads

From a stopped container

docker cp dead_container:/data/dump.sql .

Common mistakes

  • Expecting copied files to persist after the container is removed.
  • Overwriting files inside a running app unsafely.
  • Permission/ownership surprises.

Key takeaways

  • docker cp copies files and folders between a container and the host filesystem, in either direction.
  • Run docker <command> --help for all options.
  • Clean up unused resources regularly.

FAQ

cp vs volumes?

cp is a one-off copy; volumes share data continuously.

Where can I see all options?

Run the command with --help.

More General articles