Copy File to Docker Container
The Nautilus DevOps team is conducting application deployment tests on selected application servers. They require completing a task on app server 1.
On Application Server 1 a container named ubuntu_latest is running, you have to copy the encrypted file /tmp/nautilus.tx.gpg from docker hosts to container folder /usr/src.
Steps
Login into App Server and run the following command:
docker cp /tmp/nautilus.txt.gpg ubuntu_latest:/usr/src/
Good to Know?
Docker Copy Operations
- Purpose: Transfer files between host and containers
- Bidirectional: Copy from host to container or vice versa
- Running Containers: Works with both running and stopped containers
- Preserve Permissions: Maintains file ownership and permissions
Copy Command Syntax
- Host to Container:
docker cp /host/path container:/container/path
- Container to Host:
docker cp container:/container/path /host/path
- Directory Copy: Use
-a flag to preserve attributes
- Recursive: Automatically copies directories recursively
Use Cases
- Configuration: Deploy config files to containers
- Logs: Extract log files for analysis
- Debugging: Copy files for troubleshooting
- Backup: Extract data from containers
Best Practices
- Volume Mounts: Prefer volumes for persistent data
- Build Time: Use COPY in Dockerfile for build-time files
- Security: Be cautious with file permissions
- Temporary: Use docker cp for temporary file transfers