Copy files out of a container

Low risk
What do risk levels mean?
Read-only
Inspects state without changing anything.
Low risk
Reversible with a routine follow-up command.
Medium risk
Changes state; undo path documented.
Destructive
Deletes or overwrites; confirmation required.

Copies a file or directory from a container's filesystem to the host, for example to retrieve generated output or a config file for inspection. The container is not modified.

dockercopyfiles

Parameters

Name or ID of the container to copy from; works for stopped containers too.

Absolute path inside the container, such as /app/output.log.

Destination path on the host, such as ./output.log.

Commands

Confirm the source path exists in the container and see its size

docker exec <container> ls -l <container_path>

Copy the file or directory from the container to the host

docker cp <container>:<container_path> <host_path>

Verification

docker exec <container> ls -l <container_path>

The source still exists in the container, and the destination file now exists on the host with the expected size.

Undo

Not undoable

Nothing in the container changed. The copy created a new file or directory on the host; delete it manually if it is not wanted.

Pitfalls

  • The inspection step needs a running container; docker cp itself also works on stopped containers, so skip the ls if the container has exited.
  • Copying a directory copies it recursively; a trailing /. on the source copies the directory's contents instead of the directory itself.
  • An existing destination file on the host is overwritten without warning.

Related