Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Docker cp command

Docker cp command

Docker Command ReferenceDocker Command Reference


docker cp The command copies files or directories between a Docker container and the host.

docker cp The command supports copying files from container to host, or from host to container.

Syntax

docker cp [OPTIONS] SRC_PATH CONTAINER:DEST_PATH

docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH

Copy files from a container to the host



docker cp my_container:/path/in/container /path/on/host

Copies the file or directory /path/in/container inside container my_container to /path/on/host on the host.

Copy files from the host to a container



docker cp /path/on/host my_container:/path/in/container

Copies the file or directory /path/on/host on the host to /path/in/container inside container my_container.

Copy a directory from a container to the host



docker cp my_container:/path/in/container /path/on/host

Copies the directory /path/in/container inside the container and its contents to /path/on/host on the host.

Copy a directory from the host to a container



docker cp /path/on/host my_container:/path/in/container

Copies the directory /path/on/host on the host and its contents to /path/in/container inside container my_container.

Example

Container copies a file to the host

Start a container and create a file:



docker run -d --name my_container ubuntu bash -c "echo 'Hello, Docker!' > /hello.txt"

Copy a file from the container to the host:



docker cp my_container:/hello.txt ./hello.txt

Copies the /hello.txt file from the container to the current host directory.

View the copied file's contents:



cat hello.txt

Output:

Hello, Docker!

Copy files from the host to a container

Copy a file to the container:



docker cp ./hello.txt my_container:/hello.txt

Copies the hello.txt file from the host to /hello.txt inside the container.

Enter the container to view the file contents:



docker exec -it my_container cat /hello.txt

Output:



Hello, Docker!

Notes

docker cp The command is a useful tool provided by Docker for copying files or directories between a container and the host. It is suitable for operations such as backup, configuration updates, and log collection, helping users conveniently manage and manipulate files in containers.


Docker Command ReferenceDocker Command Reference