Copy Files

Copy files or folders between a container and the local filesystem.

container
files

Command

docker cp

Explanation

docker cp copies files both ways without requiring the container to be running. It behaves like the Linux cp command but across environments. File ownership and permissions are preserved by default.

Common Use Cases

  • Extract logs or data files from containers
  • Copy configuration files into containers
  • Backup or restore application data

Best Practices

  • Use absolute paths for reliability
  • Validate target directory exists before copying

Common Mistakes to Avoid

  • Using incorrect path syntax (must include container name and colon)
  • Expecting copy to work if container doesn’t exist

Troubleshooting

Problem: Permission denied on copied files

Solution: Adjust host directory permissions or use sudo when needed.

Examples

Copy from container to host

docker cp my_container:/file.txt .

Copy from host to container

docker cp file.txt my_container:/path/