# Docker Cp Failed: How to Copy Files Between Host and Containers
You tried to copy files between your host and a container, but it failed:
Error: No such container: mycontainerOr:
Error response from daemon: Could not find the file /app/data.txt in container mycontainerOr:
lstat /path/to/file: permission denieddocker cp lets you transfer files bidirectionally. Let me show you how to diagnose and fix copy failures.
Introduction
This article covers troubleshooting steps and solutions for Docker Cp Failed: How to Copy Files Between Host and Containers. The error typically occurs in production environments and can cause service disruptions if not addressed promptly.
Symptoms
Common error messages include:
Error: No such container: mycontainerError response from daemon: Could not find the file /app/data.txt in container mycontainerlstat /path/to/file: permission deniedCommon Causes
- Configuration misconfiguration
- Missing or incorrect credentials
- Network connectivity issues
- Version compatibility problems
- Resource exhaustion or limits
- Permission or access denied
Step-by-Step Fix
Understanding Docker Cp
docker cp works like standard cp:
- Copy from host to container: docker cp host_path container:path
- Copy from container to host: docker cp container:path host_path
Key requirements: - Container must exist (running or stopped) - Paths must be valid - Permissions must allow access
Step 1: Verify Container Exists
Check the container is available:
```bash # List all containers including stopped docker ps -a
# Check specific container docker ps -a --filter name=mycontainer
# Container must exist for cp to work # Create container if missing docker run -d --name mycontainer nginx ```
Note: docker cp works on stopped containers too.
Step 2: Check Path Validity
Paths must exist in both source and destination:
```bash # Copy from container to host docker cp mycontainer:/app/file.txt ./local-file.txt
# Check if file exists in container docker exec mycontainer ls -la /app/file.txt
# Or for stopped containers docker commit mycontainer temp-image docker run --rm temp-image ls -la /app/file.txt ```
Common path mistakes:
```bash # Wrong: relative path in container docker cp mycontainer:file.txt ./ # Should be absolute path in container
# Correct: absolute path docker cp mycontainer:/app/file.txt ./
# Wrong: non-existent host path docker cp mycontainer:/app/file.txt /nonexistent/ # Host directory must exist
# Correct: existing host path mkdir -p ./output docker cp mycontainer:/app/file.txt ./output/ ```
Step 3: Handle Directory Copying
Copy entire directories:
```bash # Copy directory from container docker cp mycontainer:/app/data ./local-data
# Copy directory to container docker cp ./local-data mycontainer:/app/
# The trailing slash matters: # destination/ -> contents copied into directory # destination -> directory itself copied ```
For container to stopped container:
# Can't cp between containers directly
# Use intermediate host copy
docker cp source-container:/app/file.txt ./temp-file
docker cp ./temp-file dest-container:/app/file.txtStep 4: Fix Permission Issues
Permission denied during copy:
```bash # Check container file permissions docker exec mycontainer ls -la /app/
# Check container user docker exec mycontainer whoami
# If file owned by root in container docker exec -u root mycontainer chmod 644 /app/file.txt
# Then copy docker cp mycontainer:/app/file.txt ./ ```
Host permission issues:
```bash # Check host directory permissions ls -la ./output
# Fix if needed chmod 755 ./output
# Or use sudo sudo docker cp mycontainer:/app/file.txt ./output/ ```
Step 5: Copy Between Containers
No direct container-to-container copy. Use intermediate:
```bash # Create a temporary volume docker volume create temp-volume
# Mount to source, copy files docker run --rm -v temp-volume:/data -v mycontainer-data:/src alpine cp -r /src/. /data/
# Then mount to destination docker run --rm -v temp-volume:/data -v dest-data:/dest alpine cp -r /data/. /dest/
# Clean up docker volume rm temp-volume ```
Or use host as intermediate:
docker cp source:/app/file.txt ./temp/
docker cp ./temp/file.txt dest:/app/
rm ./temp/file.txtStep 6: Handle Symlinks
Symlinks have special behavior:
```bash # Symlinks in container docker exec mycontainer ls -la /app/link
# Cp copies symlink target by default docker cp mycontainer:/app/link ./
# To preserve symlink, use archive mode (not available) # Instead, create symlink manually on host ```
Circular symlinks cause errors:
Error: symlink loop detectedAvoid copying directories with circular symlinks.
Step 7: Copy Large Files
Large files may have issues:
```bash # Check file size docker exec mycontainer du -h /app/large-file
# For very large files, stream through tar docker exec mycontainer tar -cf - /app/large-file | tar -xf -
# Or use volume for efficiency docker run --rm -v mycontainer-data:/data -v $(pwd):/output alpine \ cp /data/large-file /output/ ```
Step 8: Copy During Build (Not Runtime)
Use COPY in Dockerfile for build-time copying:
```dockerfile # Dockerfile COPY COPY ./local-file.txt /app/file.txt
# This happens during build, not runtime ```
For runtime copying, use docker cp or volume mounts.
Step 9: Windows Path Issues
On Windows, paths have different format:
```bash # Windows path syntax docker cp mycontainer:/app/file.txt C:/Users/name/file.txt
# Or using Git Bash style docker cp mycontainer:/app/file.txt /c/Users/name/file.txt
# PowerShell docker cp mycontainer:/app/file.txt . ```
Drive mounting on Windows:
# Must share drive in Docker Desktop first
# Settings > Resources > File Sharing > Add driveStep 10: Alternative Methods
If docker cp keeps failing:
```bash # Use volume mounts instead docker run -v $(pwd):/output mycontainer cp /app/file.txt /output/
# Or export entire filesystem docker export mycontainer | tar -x -C ./output/
# Or use rsync through exec docker exec mycontainer rsync -av /app/data/ /output/ ```
Quick Cp Reference
```bash # From container to host docker cp CONTAINER:/container/path ./host/path
# From host to container docker cp ./host/path CONTAINER:/container/path
# Copy directory docker cp CONTAINER:/app ./local-app
# To specific location docker cp CONTAINER:/app/file.txt ./output/new-name.txt ```
Common Cp Error Patterns
| Error | Cause | Fix |
|---|---|---|
No such container | Container doesn't exist | Create container first |
Could not find file | Path invalid in container | Verify path exists |
permission denied | File permissions | Fix permissions or use root |
no such file or directory | Host path missing | Create host directory |
symlink loop | Circular symlinks | Avoid copying circular links |
Prevention
- 1.Verify paths exist before copying
- 2.Use absolute paths in container
- 3.Create host directories before destination copy
- 4.Check permissions for both sides
- 5.Use volume mounts for frequent transfers
- 6.Test with small files first
Verification
| Task | Command |
|---|---|
| Check container exists | docker ps -a --filter name=NAME |
| Check container path | docker exec NAME ls -la /path |
| Copy file to host | docker cp NAME:/path/file ./ |
| Copy file to container | docker cp ./file NAME:/path/ |
| Copy directory | docker cp NAME:/path ./ |
| Check permissions | docker exec NAME ls -la /path |
| Fix permissions | docker exec -u root NAME chmod 644 /path/file |
Cp failures are usually due to invalid paths, missing containers, or permissions. Verify the container exists, check paths are absolute, and ensure permissions allow access.
Related Articles
- [Fix docker build cache invalidated unnecessary layers Issue in Docker-Errors](docker-build-cache-invalidated-unnecessary-layers)
- [Fix Docker Build Cache Invalidation Optimization Issue in Docker](docker-build-cache-invalidation-optimization)
- [Fix docker build context slow large files Issue in Docker-Errors](docker-build-context-slow-large-files)
- [Fix docker build multi stage copy from not found Issue in Docker-Errors](docker-build-multi-stage-copy-from-not-found)
- [Fix docker buildkit export local tar layer missing Issue in Docker-Errors](docker-buildkit-export-local-tar-layer-missing)
<script type="application/ld+json"> { "@context": "https://schema.org", "@type": "TechArticle", "headline": "Docker Cp Failed: How to Copy Files Between Host and Containers", "description": "Troubleshoot Docker cp errors. Fix container status issues, resolve path problems, and handle permission denied errors for file copying.", "url": "https://www.fixwikihub.com/fix-docker-cp-failed", "publisher": { "@type": "Organization", "name": "FixWikiHub", "url": "https://www.fixwikihub.com" }, "author": { "@type": "Person", "name": "FixWikiHub Editorial Team" }, "datePublished": "2025-11-12T15:45:05.766Z", "dateModified": "2025-11-12T15:45:05.766Z" } </script>