Managing files between a Docker host and running containers is a fundamental operation in containerized environments. In production or test environments, you frequently need to copy configuration files, certificates, logs, or encrypted data packages into or out of a running container.
Rather than installing SSH daemons, using network protocols (scp, rsync), or relying on temporary volume mounts, Docker provides a native, secure, and efficient command-line tool: docker cp.
docker cp WorksThe docker cp command copies files or directories between a container and the local host filesystem. It interacts directly with the Docker Daemon, which packages the source files as a tar archive stream and extracts them into the target destination.
sequenceDiagram
participant HostFS as Host Filesystem (/tmp/)
participant Docker as Docker Daemon
participant ContainerFS as Container Filesystem (/usr/src/)
HostFS->>Docker: Read `/tmp/nautilus.txt.gpg`
Docker->>ContainerFS: Stream & write file to `ubuntu_latest:/usr/src/`
Note over Docker,ContainerFS: Preserves file integrity, timestamps, and permissions
docker cp:stapp02) (can vary in labs, e.g., stapp01, stapp02, stapp03)steve (associated with stapp02; tony for stapp01, banner for stapp03)ubuntu_latest/tmp/nautilus.txt.gpg/usr/src/Establish an SSH connection from the Jump Host to the designated App Server (in this example, App Server 2):
ssh steve@stapp02
Provide the server password when prompted.
Verify if the target container (ubuntu_latest) is currently active and running on the server:
# List running containers
docker ps
Expected Output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8e7e1f40a1b2 ubuntu "tail -f /dev/null" 10 minutes ago Up 10 minutes ubuntu_latest
Confirm that the source file /tmp/nautilus.txt.gpg exists on the host filesystem:
ls -l /tmp/nautilus.txt.gpg
Use docker cp to copy the encrypted file from the host’s /tmp directory to the /usr/src/ directory inside the ubuntu_latest container:
docker cp /tmp/nautilus.txt.gpg ubuntu_latest:/usr/src/
Note: If your user is not in the docker group, you may need to prepend sudo to the command:
sudo docker cp /tmp/nautilus.txt.gpg ubuntu_latest:/usr/src/
Execute a command inside the container to list the destination directory and check if the file exists:
docker exec ubuntu_latest ls -l /usr/src/nautilus.txt.gpg
Expected Output:
-rw-r--r-- 1 root root 352 Jul 11 22:45 /usr/src/nautilus.txt.gpg
To ensure the file was not modified or corrupted during transfer, compare the MD5 checksum of the file on the host with the checksum of the file inside the container:
On the Docker Host:
md5sum /tmp/nautilus.txt.gpg
Inside the Container:
docker exec ubuntu_latest md5sum /usr/src/nautilus.txt.gpg
The output hashes from both commands must match exactly.
Log out of the Application Server:
exit