Docker cp: copy files to and from a container
Move a copy across the boundary
docker cp transfers file content between the Docker client’s local filesystem and a container. It also works for many files in stopped containers. A path written as NAME:/path identifies the container side; a regular local path identifies the client side. This is a one-time copy, not a shared folder.
Lab prerequisites
Use an approved disposable Linux-container lab only. Verify the active context (docker context show), CLI/daemon versions and that the instructor has reviewed and preloaded the BusyBox 1.37 fixture for the target platform; record its digest before use. The tag is inherited from earlier chapters, not a verified immutable identity. --pull=never avoids automatic downloads; --network none isolates normal network access for these local-only tasks. Verify each bm-v04 name is unused; if not, choose a fresh name consistently. Run host commands one at a time in Bash, stop on unexpected errors, and clean up only the container you created. If a fixture times out, inspect its state before retrying; remove only your stopped fixture. All snippets are static, not executed by ByteMind. Before any engine request, confirm the effective approved daemon endpoint, including DOCKER_HOST, DOCKER_CONTEXT and CLI overrides; do this privately in each terminal. Confirm the local tag still resolves to the approved image identity and platform. If any check fails, use the paper exercise instead. The network flag limits container networking, not the client-to-daemon connection. Use a lab with readable, local-only logging and an owner-set execution time limit; confirm your fixture identity before every cleanup.
Round trip with a new folder
The host creates a new temporary directory, writes a synthetic note, copies it into /tmp in a waiting container, and reads it back under a different filename. cmp succeeds with no output when the files are identical. Stop on any error; the final cleanup affects only the two lab files and empty temporary directory. The && operator proceeds only when the previous command succeeds. Keep this variable in the same shell and proceed only after the temporary directory and file have been created successfully. The second comparison happens after container removal but before file cleanup.
lab_dir=$(mktemp -d) && printf 'sample note\n' > "$lab_dir/note.txt"docker run -d --pull=never --network none --name bm-v04-copy busybox:1.37 sleep 300docker cp "$lab_dir/note.txt" bm-v04-copy:/tmp/note.txtdocker cp bm-v04-copy:/tmp/note.txt "$lab_dir/returned.txt"cmp "$lab_dir/note.txt" "$lab_dir/returned.txt"docker stop bm-v04-copydocker rm bm-v04-copy# Compare again after the container is gone, before removing local files.cmp "$lab_dir/note.txt" "$lab_dir/returned.txt"rm "$lab_dir/note.txt" "$lab_dir/returned.txt" && rmdir "$lab_dir"Paths and ownership
A relative local path is interpreted relative to the client command’s working directory. Container paths are rooted in the container filesystem. Ensure parent directories exist first. Copying can overwrite files and destination ownership can differ from the source. Do not copy secrets, live database directories or untrusted archives as a beginner exercise.
Manual practice
Predict which argument in each copy command is local. Repeat the lab with a different synthetic message and new container name, verify the round trip, and check that the local returned file stays present after removal of the container. Then clean up only those lab files.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Docker cp: copy files to and from a container — Knowledge check
Apply the concepts from this lesson.
Which argument denotes a container file?
Does copying a file create continuous synchronization?
What is a good check for two synthetic files expected to be identical?