“Just exec in and cat it!” they said. Sure, if you enjoy copy-pasting multi-GB log files. Docker cp exists for a reason.
Copy from container to host#
1
2
3
4
5
| # Copy file
docker cp container-name:/path/to/file.txt /local/destination/
# Copy directory
docker cp container-name:/path/to/directory /local/destination/
|
Copy from host to container#
1
2
3
4
5
| # Copy file
docker cp /local/file.txt container-name:/path/in/container/
# Copy directory
docker cp /local/directory container-name:/path/in/container/
|
Examples#
1
2
3
4
5
6
7
8
9
10
11
| # Backup database dump from container
docker cp mysql-container:/var/lib/mysql/dump.sql ~/backups/
# Upload config file to nginx container
docker cp nginx.conf web-server:/etc/nginx/nginx.conf
# Extract logs from container
docker cp app-container:/var/log/app/ ./logs/
# Copy build artifacts from build container
docker cp build-container:/app/dist ./dist/
|
Notes#
- Works with running or stopped containers
- Preserves file permissions by default
- Use container ID or name
- Paths must be absolute inside container