How to use the command 'docker cp' (with examples)
This article will illustrate various use cases of the ‘docker cp’ command, which is used to copy files or directories between the host and container filesystems.
Use case 1: Copy a file or directory from the host to a container
Code:
docker cp path/to/file_or_directory_on_host container_name:path/to/file_or_directory_in_container
Motivation: Suppose you have a file or directory on your host machine that needs to be copied into a running container. This use case allows you to easily copy the file or directory into the container.
Explanation:
path/to/file_or_directory_on_host
: This is the path to the file or directory on the host machine that you want to copy into the container.container_name
: This is the name or ID of the container where you want to copy the file or directory.path/to/file_or_directory_in_container
: This is the path where you want to copy the file or directory inside the container.
Example output:
docker cp /home/user/file.txt my_container:/app/file.txt
In this example, the file file.txt
located at /home/user
on the host machine is copied into the container named my_container
at the path /app/file.txt
.
Use case 2: Copy a file or directory from a container to the host
Code:
docker cp container_name:path/to/file_or_directory_in_container path/to/file_or_directory_on_host
Motivation: Sometimes you may need to retrieve a file or directory from a running container to your host machine. This use case allows you to easily copy the desired file or directory to your host machine.
Explanation:
container_name
: This is the name or ID of the container from which you want to copy the file or directory.path/to/file_or_directory_in_container
: This is the path to the file or directory inside the container that you want to copy.path/to/file_or_directory_on_host
: This is the path on the host machine where you want to copy the file or directory.
Example output:
docker cp my_container:/app/file.txt /home/user/file.txt
In this example, the file file.txt
located at /app
inside the container named my_container
is copied to the path /home/user/file.txt
on the host machine.
Use case 3: Copy a file or directory from the host to a container, following symlinks
Code:
docker cp --follow-link path/to/symlink_on_host container_name:path/to/file_or_directory_in_container
Motivation: If you have symbolic links (symlinks) in your file or directory on the host machine, and you want to copy the actual files and directories pointed to by the symlinks into the container, this use case allows you to achieve that.
Explanation:
--follow-link
: This flag is used to make thedocker cp
command follow symbolic links on the host machine and copy the target files and directories instead.path/to/symlink_on_host
: This is the path to the symlink on the host machine that you want to copy into the container.container_name
: This is the name or ID of the container where you want to copy the symlink and its target files or directories.path/to/file_or_directory_in_container
: This is the path where you want to copy the symlink and its target files or directories inside the container.
Example output:
docker cp --follow-link /home/user/symlink my_container:/app/
In this example, the symlink /home/user/symlink
on the host machine is followed, and the target files or directories are copied into the container named my_container
at the path /app/
.