LinuxCommandLibrary

docker-container-cp

Copy files between container and host system

TLDR

Copy a file or directory from the host to a container

$ docker [[cp|container 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 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|container cp]] [[-L|--follow-link]] [path/to/symlink_on_host] [container_name]:[path/to/file_or_directory_in_container]
copy

SYNOPSIS

docker container cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
docker container cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH

PARAMETERS

--archive, -a
    Archive mode: copy all uid/gid/permissions information

--follow-link, -L
    Always follow symbol links in SRC_PATH

--quiet, -q
    Suppress progress output (default false)

DESCRIPTION

The docker container cp command copies files or directories between a running or stopped Docker container and the host machine's filesystem. It supports two primary directions: from container to host (CONTAINER:SRC_PATH to DEST_PATH) or from host to container (SRC_PATH to CONTAINER:DEST_PATH). Paths inside the container are relative to its root filesystem and can use : to specify the container name or ID.

This utility is essential for data transfer without entering the container interactively, such as debugging, backups, or injecting configuration files. It preserves file attributes when using archive mode but does not support direct container-to-container copies—use the host as an intermediary. Symbol links are handled optionally, and progress output can be suppressed for scripting.

Common use cases include extracting logs (docker container cp myapp:/var/log/app.log ./), copying scripts into a container for execution, or archiving directories. Note that the container must exist, and permissions on the host may affect access.

CAVEATS

Cannot copy directly between containers; use host as intermediate. Container paths are relative to rootfs. Requires container ID/name to exist. Host permissions apply to destinations.

EXAMPLES

docker container cp mycontainer:/app/config.json . (container to host)
docker container cp README.md mycontainer:/app/ (host to container)

EXIT CODES

0: success
1: failure (invalid args, no such container/file, permission denied)

HISTORY

Introduced as docker cp in Docker 1.3 (2014). docker container cp subcommand added in Docker 17.06 (2017) for consistency with container management commands.

SEE ALSO

docker(1), docker-container-ls(1), docker cp(1), tar(1), scp(1)

Copied to clipboard