LinuxCommandLibrary

docker-cp

Copy files between container and host

TLDR

Copy a file or directory from the host to a container

$ docker cp [path/to/file_or_directory_on_host] [container_name]:[path/to/file_or_directory_in_container]
copy

Copy a file or directory from a container to the host
$ docker cp [container_name]:[path/to/file_or_directory_in_container] [path/to/file_or_directory_on_host]
copy

Copy a file or directory from the host to a container, following symlinks (copies the symlinked files directly, not the symlinks themselves)
$ docker cp --follow-link [path/to/symlink_on_host] [container_name]:[path/to/file_or_directory_in_container]
copy

SYNOPSIS

docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH
docker cp [OPTIONS] SRC_PATH CONTAINER:DEST_PATH

PARAMETERS

-a, --archive
    Archive mode (copy all uid/gid information).

-L, --follow-link
    Follow symlinks in SRC_PATH.
WARNING: this will resolve a local symlink on the host machine outside the context of the container to the host.

DESCRIPTION

The `docker cp` command facilitates transferring files and directories between a Docker container and the host machine. It allows users to copy data *into* a running or stopped container from the host, or conversely, *extract* files and directories from a container to the host's filesystem. This command is essential for tasks like transferring configuration files, retrieving logs, or deploying updated code into a container. It's a relatively simple yet powerful tool for managing data flow in Docker environments.

Note that directory metadata is not preserved when copying. This means file permissions, ownership, and timestamps might not be identical after the copy operation.

CAVEATS

File permissions/ownership are not preserved by default unless the `-a` (archive) option is used.
The destination path must exist. If it doesn't, the copy will fail.

LIMITATIONS WITH VOLUMES

`docker cp` is *not* recommended for use with persistent volumes or bind mounts. If the data you want to copy exists within a volume, it's preferable to use other methods such as creating an image or using `docker exec` in conjunction with `tar` for more robust and portable data extraction.

SRC_PATH AND DEST_PATH INTERPRETATION

When copying *from* a container (CONTAINER:SRC_PATH DEST_PATH), the `SRC_PATH` is interpreted relative to the container's filesystem.
When copying *to* a container (SRC_PATH CONTAINER:DEST_PATH), the `DEST_PATH` is interpreted relative to the container's filesystem.

HISTORY

The `docker cp` command has been a core part of Docker since its early releases. It provides a basic mechanism for interacting with the container's filesystem, predating more advanced features like volumes and bind mounts. It evolved as a necessary tool for quick data transfers without the overhead of building new images every time.

SEE ALSO

docker commit(1), docker run(1), tar(1)

Copied to clipboard