0
0
Dockerdevops~15 mins

Named volumes for data sharing in Docker - Mini Project: Build & Apply

Choose your learning style9 modes available
Named volumes for data sharing
📖 Scenario: You are setting up two Docker containers that need to share data. To do this safely and easily, you will use a named volume. This volume will store files that both containers can access.
🎯 Goal: Create a Docker named volume called shared_data. Then, configure two containers to use this volume so they can share files. Finally, verify the shared data by listing the files inside the volume from one container.
📋 What You'll Learn
Create a named volume called shared_data
Run a container named writer that uses the shared_data volume and writes a file inside it
Run a container named reader that uses the shared_data volume and lists the files inside it
Print the output of the reader container showing the shared file
💡 Why This Matters
🌍 Real World
Named volumes are used in real projects to share data between containers, like sharing database files or configuration.
💼 Career
Understanding named volumes is important for DevOps roles to manage persistent data and container communication.
Progress0 / 4 steps
1
Create a named volume called shared_data
Write the Docker command to create a named volume called shared_data.
Docker
Need a hint?

Use docker volume create followed by the volume name.

2
Run a container named writer that uses the shared_data volume and writes a file
Write the Docker command to run a container named writer using the shared_data volume mounted at /data. Inside the container, create a file named hello.txt with the content Hello from writer. Use the alpine image and run the command sh -c "echo 'Hello from writer' > /data/hello.txt".
Docker
Need a hint?

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

3
Run a container named reader that uses the shared_data volume and lists files
Write the Docker command to run a container named reader using the shared_data volume mounted at /data. Use the alpine image and run the command ls /data to list files inside the volume.
Docker
Need a hint?

Use docker run --rm --name reader -v shared_data:/data alpine ls /data.

4
Print the output of the reader container showing the shared file
Run the command to list files inside the shared_data volume from the reader container and print the output. The output should show hello.txt.
Docker
Need a hint?

Run the reader container with ls /data and check the output.