# 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:

bash
Error: No such container: mycontainer

Or:

bash
Error response from daemon: Could not find the file /app/data.txt in container mycontainer

Or:

bash
lstat /path/to/file: permission denied

docker cp lets you transfer files bidirectionally. Let me show you how to diagnose and fix copy failures.

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:

bash
# 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.txt

Step 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:

bash
docker cp source:/app/file.txt ./temp/
docker cp ./temp/file.txt dest:/app/
rm ./temp/file.txt

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:

bash
Error: symlink loop detected

Avoid 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:

bash
# Must share drive in Docker Desktop first
# Settings > Resources > File Sharing > Add drive

Step 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

ErrorCauseFix
No such containerContainer doesn't existCreate container first
Could not find filePath invalid in containerVerify path exists
permission deniedFile permissionsFix permissions or use root
no such file or directoryHost path missingCreate host directory
symlink loopCircular symlinksAvoid copying circular links

Prevention Best Practices

  1. 1.Verify paths exist before copying
  2. 2.Use absolute paths in container
  3. 3.Create host directories before destination copy
  4. 4.Check permissions for both sides
  5. 5.Use volume mounts for frequent transfers
  6. 6.Test with small files first

Quick Reference

TaskCommand
Check container existsdocker ps -a --filter name=NAME
Check container pathdocker exec NAME ls -la /path
Copy file to hostdocker cp NAME:/path/file ./
Copy file to containerdocker cp ./file NAME:/path/
Copy directorydocker cp NAME:/path ./
Check permissionsdocker exec NAME ls -la /path
Fix permissionsdocker 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.