DEV Community

Janak Shrestha
Janak Shrestha

Posted on

Copy File to Docker Container

The Nautilus DevOps team possesses confidential data on App Server 1 in the Stratos Datacenter. A container named ubuntu_latest is running on the same server.

Copy an encrypted file /tmp/nautilus.txt.gpg from the docker host to the ubuntu_latest container located at /usr/src/. Ensure the file is not modified during this operation.


Step-by-Step Instructions

1. SSH into Application Server 1

ssh steve@stapp01
Enter fullscreen mode Exit fullscreen mode

(Enter the password when prompted)


2. Switch to root or use sudo

sudo su -
Enter fullscreen mode Exit fullscreen mode

or prefix all commands with sudo


3. Verify the container is running

sudo docker ps | grep ubuntu_latest
Enter fullscreen mode Exit fullscreen mode

If the container isn't running, start it:

sudo docker start ubuntu_latest
Enter fullscreen mode Exit fullscreen mode

4. Verify the source file exists on the host

ls -la /tmp/nautilus.txt.gpg
Enter fullscreen mode Exit fullscreen mode

5. Copy the file to the container

Use the docker cp command to copy the file from the host to the container:

sudo docker cp /tmp/nautilus.txt.gpg ubuntu_latest:/usr/src/
Enter fullscreen mode Exit fullscreen mode

Syntax: docker cp <source_path> <container_name>:<destination_path>


6. Verify the file was copied successfully

Check that the file exists inside the container:

sudo docker exec ubuntu_latest ls -la /usr/src/nautilus.txt.gpg
Enter fullscreen mode Exit fullscreen mode

7. Verify the file was not modified

Compare the file checksums on the host and inside the container:

On the host:

md5sum /tmp/nautilus.txt.gpg
Enter fullscreen mode Exit fullscreen mode

Inside the container:

sudo docker exec ubuntu_latest md5sum /usr/src/nautilus.txt.gpg
Enter fullscreen mode Exit fullscreen mode

The checksums should match, confirming the file was not modified.


Complete Execution Summary

# SSH to App Server 1
ssh steve@stapp01

# Verify container is running
sudo docker ps | grep ubuntu_latest

# Copy the file
sudo docker cp /tmp/nautilus.txt.gpg ubuntu_latest:/usr/src/

# Verify file exists
sudo docker exec ubuntu_latest ls -la /usr/src/nautilus.txt.gpg

# Verify file integrity (checksums should match)
md5sum /tmp/nautilus.txt.gpg
sudo docker exec ubuntu_latest md5sum /usr/src/nautilus.txt.gpg
Enter fullscreen mode Exit fullscreen mode

Key Points

  • docker cp preserves the file exactly as-is (no modifications)
  • The destination path /usr/src/ must exist inside the container
  • If the destination directory doesn't exist, you'll get an error
  • You can also specify a different filename in the destination if needed

Top comments (0)