Copying Files Between Host & Container (docker cp)
Move files in and out of containers for debugging, backups, and one-off transfers.
In plain words
Copy a specific diagnostic artifact or one-off seed file without adding a general-purpose mount or publishing a port.
Think of it like…
It is carrying one paper file between offices; a bind mount is a shared filing room, and a backup is the audited off-site copy.
Where you’ve seen it
In practice, docker cp ./evidence/config.json api:/tmp/config.json && docker cp api:/var/log/app/incident.log ./artifacts/incident.log is the concrete move for this job: move files in and out of containers for debugging, backups, and one-off transfers. The release is not accepted until docker exec api stat -c %U:%G:%a /tmp/config.json and sha256sum both copies confirms the result.
Words you’ll hear
- docker cp
- The implementation control in
docker cp ./evidence/config.json api:/tmp/config.json && docker cp api:/var/log/app/incident.log ./artifacts/incident.logfor move files in and out of containers for debugging, backups, and one-off transfers. - Host Path
- The result to observe with
docker exec api stat -c %U:%G:%a /tmp/config.json and sha256sum both copies, rather than infer from a successful command alone. - Container Path
- The production boundary exposed when adding a trailing directory slash incorrectly, following a symlink into an unexpected host path, or copying a live database data directory mid-write.
Visual diagram
Theory & deep dive
docker cp control path
The concrete mechanism is docker cp ./evidence/config.json api:/tmp/config.json && docker cp api:/var/log/app/incident.log ./artifacts/incident.log. The observable result is docker exec api stat -c %U:%G:%a /tmp/config.json and sha256sum both copies; if those disagree, inspect the runtime evidence before changing unrelated settings.
Move files in and out of containers for debugging, backups, and one-off transfers. Keep docker cp, Host Path and Container Path in the same deployment record so the intent and the observed state cannot drift apart.
docker cp ./evidence/config.json api:/tmp/config.json && docker cp api:/var/log/app/incident.log ./artifacts/incident.log
docker exec api stat -c %U:%G:%a /tmp/config.json and sha256sum both copies, not with command success alone.Container Path failure boundary
docker cp bypasses the application’s normal API and can silently overwrite paths with different ownership. It does not follow volume changes on another host and is never a substitute for a database-consistent backup. The narrow failure to design for is adding a trailing directory slash incorrectly, following a symlink into an unexpected host path, or copying a live database data directory mid-write.
Accept this compromise explicitly: docker cp is excellent for bounded incident evidence; API exports, volumes and backup tools are safer for repeatable data movement. The controlled check-docker exec api stat -c %U:%G:%a /tmp/config.json and sha256sum both copies-also identifies the point where a different design is safer.
Exercise this failure safely: adding a trailing directory slash incorrectly, following a symlink into an unexpected host path, or copying a live database data directory mid-write.
How it works, step by step
Step 1 of 41. State the boundary
Write down why the workload needs this: docker cp bypasses the application’s normal API and can silently overwrite paths with different ownership. It does not follow volume changes on another host and is never a substitute for a database-consistent backup.
- Intent
- docker cp
When to use it & trade-offs
What it is
docker cp uses the same archive-style copy semantics as a tar stream between a path on the Docker host and a running or stopped container filesystem. The trailing /. and directory forms determine whether the source directory itself or only its contents are copied.
Why it matters: docker cp bypasses the application’s normal API and can silently overwrite paths with different ownership. It does not follow volume changes on another host and is never a substitute for a database-consistent backup.
When to reach for it
- The fit is strongest for this job: move files in and out of containers for debugging, backups, and one-off transfers. Pair it with
docker exec api stat -c %U:%G:%a /tmp/config.json and sha256sum both copiesin the runbook. - Production docker cp changes should make Host Path observable through
docker cp ./evidence/config.json api:/tmp/config.json && docker cp api:/var/log/app/incident.log ./artifacts/incident.log, rather than trusting the runtime default. - Keep a rollback example for the case where adding a trailing directory slash incorrectly, following a symlink into an unexpected host path, or copying a live database data directory mid-write.
When to avoid or be careful
- Do not accept command success while the system can still fail by adding a trailing directory slash incorrectly, following a symlink into an unexpected host path, or copying a live database data directory mid-write.
- Avoid erasing Container Path protection merely to dodge this cost: “docker cp is excellent for bounded incident evidence; API exports, volumes and backup tools are safer for repeatable data movement”.
- Do not replace the evidence path-
docker exec api stat -c %U:%G:%a /tmp/config.json and sha256sum both copies-with a screenshot or a mutable tag.
docker cp ./evidence/config.json api:/tmp/config.json && docker cp api:/var/log/app/incident.log ./artifacts/incident.log: practical choices
Strengths: docker cp uses the same archive-style copy semantics as a tar stream between a path on the Docker host and a running or stopped container filesystem. The trailing /. and directory forms determine whether the source directory itself or only its contents are copied.
Watch out: docker cp is excellent for bounded incident evidence; API exports, volumes and backup tools are safer for repeatable data movement.
Strengths: It can begin with docker cp ./evidence/config.json api:/tmp/config.json && docker cp api:/var/log/app/incident.log ./artifacts/incident.log and fewer surrounding controls.
Watch out: It silently accepts adding a trailing directory slash incorrectly, following a symlink into an unexpected host path, or copying a live database data directory mid-write, which makes the result hard to reproduce after promotion.
Strengths: docker cp bypasses the application’s normal API and can silently overwrite paths with different ownership. It does not follow volume changes on another host and is never a substitute for a database-consistent backup.
Watch out: The platform adds an ownership and debugging layer; retain docker exec api stat -c %U:%G:%a /tmp/config.json and sha256sum both copies as the direct check.
Interview signals & numbers to know
When this topic comes up
- docker cp ./evidence/config.json api:/tmp/config.json && docker cp api:/var/log/app/incident.log ./artifacts/incident.log
- docker exec api stat -c %U:%G:%a /tmp/config.json and sha256sum both copies
- Failure signature: adding a trailing directory slash incorrectly, following a symlink into an unexpected host path, or copying a live database data directory mid-write
- Trade-off: docker cp is excellent for bounded incident evidence; API exports, volumes and backup tools are safer for repeatable data movement.
Benchmarks & rules of thumb
- 2 endpoints
- source and destination may each be host or container paths
- 1 UID boundary
- host and container numeric ownership can disagree
- tar stream
- underlies the copy semantics and directory behavior
Common mistakes & follow-ups
Common interview mistakes
- Treating a successful
docker cp ./evidence/config.json api:/tmp/config.json && docker cp api:/var/log/app/incident.log ./artifacts/incident.logas proof and skippingdocker exec api stat -c %U:%G:%a /tmp/config.json and sha256sum both copies. - Adding a trailing directory slash incorrectly, following a symlink into an unexpected host path, or copying a live database data directory mid-write
- Optimizing docker cp without accepting that docker cp is excellent for bounded incident evidence; API exports, volumes and backup tools are safer for repeatable data movement.
Likely interviewer follow-ups
- Why does
docker cp ./evidence/config.json api:/tmp/config.json && docker cp api:/var/log/app/incident.log ./artifacts/incident.logaddress docker cp here rather than through application code? - How would you distinguish healthy Host Path from the failure “adding a trailing directory slash incorrectly, following a symlink into an unexpected host path, or copying a live database data directory mid-write” using
docker exec api stat -c %U:%G:%a /tmp/config.json and sha256sum both copies? - When would this trade-off-“docker cp is excellent for bounded incident evidence; API exports, volumes and backup tools are safer for repeatable data movement”-justify a different Container Path design?
docker cp ./evidence/config.json api:/tmp/config.json && docker cp api:/var/log/app/incident.log ./artifacts/incident.log-and one proof-docker exec api stat -c %U:%G:%a /tmp/config.json and sha256sum both copies. Then name the boundary: adding a trailing directory slash incorrectly, following a symlink into an unexpected host path, or copying a live database data directory mid-write.Knowledge check
Put it into practice
Practice related interview Q&A
Apply Copying Files Between Host & Container (docker cp) with generative AI interview Q&A and hands-on practice.