0
0
Dockerdevops~30 mins

Sharing volumes between containers in Docker - Mini Project: Build & Apply

Choose your learning style9 modes available
Sharing volumes between containers
📖 Scenario: You are working on a project where two Docker containers need to share files. One container will create a file, and the other container will read it. This is common when containers need to exchange data without using a network.
🎯 Goal: Build two Docker containers that share a volume. The first container writes a file to the shared volume. The second container reads and displays the file content from the same volume.
📋 What You'll Learn
Create a Docker volume named shared_data
Run a container named writer that writes a file /data/message.txt with the text Hello from writer container!
Run a container named reader that reads and prints the content of /data/message.txt
Both containers must use the shared_data volume mounted at /data
💡 Why This Matters
🌍 Real World
Sharing volumes between containers is common when multiple services need access to the same files, such as logs, configuration, or data files.
💼 Career
Understanding Docker volumes is essential for DevOps roles to manage persistent data and enable communication between containers in real projects.
Progress0 / 4 steps
1
Create a Docker volume named shared_data
Write the Docker command to create a volume called shared_data.
Docker
Need a hint?

Use docker volume create followed by the volume name.

2
Run a container named writer that writes a file to the volume
Write the Docker command to run a container named writer that mounts the shared_data volume at /data and writes the text Hello from writer container! into the file /data/message.txt. Use the alpine image and the command sh -c "echo 'Hello from writer container!' > /data/message.txt".
Docker
Need a hint?

Use docker run --rm --name writer -v shared_data:/data alpine sh -c "echo 'Hello from writer container!' > /data/message.txt".

3
Run a container named reader that reads the file from the volume
Write the Docker command to run a container named reader that mounts the shared_data volume at /data and reads the content of /data/message.txt using cat. Use the alpine image and the command cat /data/message.txt.
Docker
Need a hint?

Use docker run --rm --name reader -v shared_data:/data alpine cat /data/message.txt.

4
Display the content of the shared file from the reader container
Run the full set of commands to create the volume, write the file, and then read and print the file content. The output should be exactly Hello from writer container!.
Docker
Need a hint?

Run all commands in order. The last command prints the message.