Docker Error Guide: 'no such file or directory' docker cp Copy Failure
Fix 'docker cp' errors like 'no such file or directory' and 'Could not find the file <path> in container': diagnose wrong source paths, stopped containers, and bad copy direction.
- #docker
- #troubleshooting
- #errors
- #cp
Stuck on this Docker with AI error? Get the free incident triage checklist
A one-page PDF — the exact steps to isolate, fix, and verify a production error like this one. No spam, unsubscribe anytime.
Exact Error Message
$ docker cp web:/app/config.yaml ./config.yaml
Error response from daemon: Could not find the file /app/config.yaml in container web
Depending on the direction of the copy and the Docker version, you may instead see:
$ docker cp ./config.yaml web:/app/config.yaml
Error: No such container:path: web:/app
lstat /home/user/config.yaml: no such file or directory
All three describe the same root problem: one side of the copy, the container path or the host path, does not exist as docker cp expects.
What It Means
docker cp copies files and directories between a container’s filesystem and the host. It resolves both endpoints strictly: the source must exist and be readable, and the destination’s parent directory must exist. When Docker reports Could not find the file ... in container, the container path you named does not resolve inside that container’s filesystem. When it reports no such file or directory or lstat ..., the host-side path is wrong.
A key detail: docker cp reads the container’s own filesystem layer, so paths are relative to the container root, not your shell’s working directory. A stopped or restarting container can still be copied from in modern Docker, but a container that never created the file, or created it in a different path, will fail.
Common Causes
- The path inside the container is wrong (typo, wrong working directory, or the file lives elsewhere than expected).
- The file is on a mounted volume that resolves differently than the assumed in-container path.
- The copy direction is reversed, so a host path is being treated as a container path or vice versa.
- The destination parent directory does not exist on the host or in the container.
- The file is generated at runtime and does not exist yet, or the process wrote it somewhere else.
- The container name or ID is wrong, producing
No such container.
Diagnostic Commands
Confirm the container exists and note its exact name/ID:
docker ps -a --format 'table {{.Names}}\t{{.Status}}\t{{.ID}}'
List the file inside the container to verify the real path:
docker exec web ls -l /app/
For a stopped container that cannot exec, inspect its mounts and working directory:
docker inspect web --format '{{.Config.WorkingDir}} {{json .Mounts}}'
Confirm the host-side path exists before copying into the container:
ls -l ./config.yaml
Step-by-Step Resolution
-
Verify the container name. Use
docker ps -aand copy the exact name;No such containeralmost always means a typo or a container that was removed. -
Locate the file inside the container. Do not assume the path; list it:
docker exec web sh -c 'ls -l /app; pwd'
- Copy using the confirmed path.
docker cpworks even when the container is stopped, so you do not need to start it just to retrieve a file:
docker cp web:/app/config.yaml ./config.yaml
- If the file lives on a volume, copy from the volume mount on the host instead, which avoids path confusion entirely:
docker volume inspect app_data --format '{{.Mountpoint}}'
sudo cp /var/lib/docker/volumes/app_data/_data/config.yaml ./config.yaml
- When copying into a container, make sure the destination directory exists first, because
docker cpwill not create missing parent directories:
docker exec web mkdir -p /app
docker cp ./config.yaml web:/app/config.yaml
- If the file is generated at runtime and is genuinely absent, exec in and confirm the application actually wrote it before retrying:
docker exec web find / -name 'config.yaml' 2>/dev/null
- Re-run the copy and confirm the file arrived:
docker cp web:/app/config.yaml ./config.yaml && ls -l ./config.yaml
-rw-r--r-- 1 user user 812 Jul 16 10:04 ./config.yaml
Prevention
- Verify container-side paths with
docker exec ... lsbefore scripting adocker cp, rather than assuming the working directory. - Remember that
docker cpcontainer paths are absolute from the container root; leading/matters and relative paths resolve against the container’sWorkingDir. - When copying into a container, create the destination directory first;
docker cpcopies into an existing directory but will not build the tree for you. - For files on named volumes, read them from the volume mountpoint on the host instead of through the container, which sidesteps path ambiguity.
- Use full container IDs or unique names in automation to avoid
No such containerfrom ambiguous or reused names. - Guard scripts that copy runtime-generated files with a check that the file exists inside the container first.
Related Errors
No such container:path— the container reference or its path prefix is invalid.Error: No such container— the container name/ID does not exist at all.lstat <path>: no such file or directory— the host-side source or destination path is missing.permission denied— the path resolves but the user lacks rights to read or write it.
Frequently Asked Questions
Can I run docker cp on a stopped container? Yes. docker cp reads and writes the container’s filesystem layer directly and does not require the container to be running, which makes it useful for recovering files from a crashed container.
Why does the file exist when I exec in but docker cp still fails? Check the exact path and copy direction. docker exec ... ls and docker cp must reference the identical absolute path; a different WorkingDir or a trailing slash can change what each resolves to.
Does docker cp create missing destination directories? No. When copying into a container or the host, the destination’s parent directory must already exist. Create it first with mkdir -p (or docker exec ... mkdir -p).
How do I copy a file that lives on a mounted volume? Read it from the volume’s host mountpoint via docker volume inspect, or copy from the container path where the volume is mounted. Both work, but the mountpoint avoids in-container path guesswork.
Where can I find more container file-handling tips? Generate copy-and-debug commands for your exact setup with the DevOps AI prompt library, and browse more Docker guides.
Fixed it? Get 500 Docker with AI & DevOps AI prompts — free
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.
Did this fix your issue?
Get 500 Battle-Tested DevOps AI Prompts — Free
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.