0
0
Dockerdevops~30 mins

Copying files to and from containers in Docker - Mini Project: Build & Apply

Choose your learning style9 modes available
Copying files to and from containers
📖 Scenario: You are working with Docker containers and need to move files between your local machine and a running container. This is a common task when you want to add configuration files or retrieve logs from containers.
🎯 Goal: Learn how to copy files into a running Docker container and copy files out from it using Docker commands.
📋 What You'll Learn
Create a simple Docker container running in the background
Copy a file from the local machine into the container
Copy a file from the container back to the local machine
Verify the copied files exist in the correct locations
💡 Why This Matters
🌍 Real World
Copying files to and from containers is essential when you need to add configuration files, scripts, or retrieve logs and data from running containers.
💼 Career
DevOps engineers and developers often need to manage files inside containers for debugging, configuration, and deployment tasks.
Progress0 / 4 steps
1
Start a running container
Run a Docker container named mycontainer in detached mode using the alpine image and keep it running with the command sleep 300.
Docker
Need a hint?

Use docker run -d --name mycontainer alpine sleep 300 to start the container.

2
Copy a file into the container
Create a file named hello.txt locally with the content Hello from local! and then copy this file into the container mycontainer at path /root/hello.txt using the Docker copy command.
Docker
Need a hint?

Use echo to create the file and docker cp to copy it into the container.

3
Copy a file from the container to local
Inside the container mycontainer, create a file /root/containerfile.txt with content Hello from container!. Then copy this file from the container to your local machine as containerfile.txt using the Docker copy command.
Docker
Need a hint?

Use docker exec to create the file inside the container and docker cp to copy it out.

4
Verify copied files
Use cat commands to display the contents of the local files hello.txt and containerfile.txt to verify the copy operations worked correctly.
Docker
Need a hint?

Use cat hello.txt and cat containerfile.txt to see the file contents.