Copying Files

Difficulty: Beginner

Question

How do you copy files between the host and a container, and what are the limitations of docker cp?

Answer

Use docker cp <host_path> <container>:<container_path> to copy into a container, and docker cp <container>:<container_path> <host_path> to copy out. It works on running and stopped containers, copies directories recursively, and does not need any tools inside the image. It is meant for debugging and one-off tasks: anything you copy into a container is lost when that container is recreated.

Basic usage and path rules

  • Copy in: docker cp ./config.yml web:/etc/app/config.yml
  • Copy out: docker cp web:/var/log/app/error.log ./error.log
  • Directories are copied recursively. The trailing /. matters: docker cp ./site/. web:/usr/share/nginx/html copies the contents of site, while ./site copies the folder itself into the target.
  • The parent directory of the destination must exist inside the container.
  • Use - as the source or destination to stream a tar archive through stdin or stdout, for example to pipe logs into tar on the host.

Behind the scenes Docker builds a tar stream of the source and extracts it at the destination, which is why no cp or tar binary is needed in the image, and why it even works on distroless images.

Ownership and permissions

  • Files copied into a container are owned by root (UID 0) by default. If the app runs as a non-root user, it may not be able to modify them; fix this with docker exec -u 0 web chown ....
  • Files copied out are created owned by the user who ran docker cp.
  • -a / --archive preserves the original UID and GID from the source.

What docker cp cannot do

  • It cannot copy special filesystems like /proc, /sys and /dev, or tmpfs mounts.
  • Changes are not persistent: they live in the container's writable layer and disappear when the container is removed, as with every docker compose up that recreates it.
  • It is not a deployment mechanism. Copying a hotfix into production containers by hand creates drift between what is running and what is in your registry, which is how "the bug came back after the next deploy" happens.

When to use it, and alternatives

Good uses: pulling a heap dump, core file or log out of a crashed container; dropping a test config into a dev container; extracting a build artefact from a container (though BuildKit's docker build --output type=local,dest=./out . is cleaner for build outputs).

For files that should change while the container runs, such as source code during development, use a bind mount (or Compose's develop.watch). For configuration in production, bake it into the image or provide it through volumes, config files mounted read-only, or environment variables.

Code examples

Copying in and out

# Into a running container
docker cp ./nginx.conf web:/etc/nginx/nginx.conf
docker exec web nginx -s reload

# Contents of a folder (note the trailing /.)
docker cp ./site/. web:/usr/share/nginx/html

# Out of a stopped or crashed container
docker cp payments-api:/tmp/heapdump.hprof ./heapdump.hprof

# Preserve original UID/GID
docker cp -a ./data/. web:/srv/data

Direction is set by which side has the container: prefix; /. copies a folder's contents rather than the folder.

Fixing ownership after copying in

docker cp ./uploads/. app:/home/node/uploads
# Files arrive owned by root; give them to the app user
docker exec -u 0 app chown -R node:node /home/node/uploads

Copied files are root-owned by default, so a non-root app user needs a chown before it can write to them.

Key points

  • docker cp copies both ways and works on running and stopped containers.
  • No cp or tar is needed in the image; Docker streams a tar archive.
  • Copied-in files are root-owned unless you use -a to keep the source UID/GID.
  • Changes live in the writable layer and vanish when the container is recreated.
  • Use bind mounts for live editing and images for anything that must persist.

Common follow-up questions

  • Why should you not use docker cp to deploy a hotfix to production?
  • How do you copy a file out of a container that has already crashed?
  • What is the difference between docker cp and a bind mount?
  • How can you extract build artefacts from a Docker build without running a container?

Interview tips

  • Give both directions and the stopped-container capability in the first sentence.
  • Say clearly that it is a debugging tool, not a deployment tool.
  • Mentioning root ownership of copied files shows hands-on experience.

Concepts covered

docker cp, Container filesystem, File ownership, Tar streaming

More Docker & Containers questions