0
0
Dockerdevops~10 mins

Named volumes for data sharing in Docker - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Named volumes for data sharing
Create Named Volume
Start Container A
Container A writes data
Start Container B
Container B reads data
Data shared via Named Volume
This flow shows how a named volume is created and used by two containers to share data persistently.
Execution Sample
Docker
docker volume create shared_data

docker run -d --name containerA -v shared_data:/data busybox sh -c "echo hello > /data/greeting.txt && sleep 3600"

docker run --rm -v shared_data:/data busybox cat /data/greeting.txt
Create a named volume, write data in one container, then read it from another container.
Process Table
StepActionCommandResult/Output
1Create named volumedocker volume create shared_dataVolume 'shared_data' created
2Start containerA with volumedocker run -d --name containerA -v shared_data:/data busybox sh -c "echo hello > /data/greeting.txt && sleep 3600"Container 'containerA' running, file /data/greeting.txt created with 'hello'
3Start containerB to read datadocker run --rm -v shared_data:/data busybox cat /data/greeting.txtOutput: hello
4Stop containerAdocker stop containerAContainer 'containerA' stopped
5Remove containerAdocker rm containerAContainer 'containerA' removed
💡 Data persists in named volume 'shared_data' even after containerA is removed
Status Tracker
VariableInitialAfter Step 1After Step 2After Step 3Final
shared_data volumenonecreatedcontains greeting.txt with 'hello'accessible with 'hello' contentpersists with data intact
Key Moments - 2 Insights
Why does containerB see the file created by containerA?
Because both containers use the same named volume 'shared_data' mounted at /data, so data written by containerA is accessible to containerB as shown in execution_table step 3.
What happens to the data if containerA is removed?
The data remains in the named volume 'shared_data' even after containerA is removed, as shown in execution_table step 5 and variable_tracker final state.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output when containerB reads the file?
Aempty output
Bhello
Cerror: file not found
DcontainerB fails to start
💡 Hint
Check execution_table row 3 under Result/Output
At which step is the named volume created?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at execution_table row 1 Action and Command
If containerA did not write the file, what would containerB see when reading?
Aerror: file not found
Bhello
Cempty output
DcontainerB crashes
💡 Hint
Think about what happens if the file /data/greeting.txt does not exist in the volume
Concept Snapshot
Named volumes in Docker allow data sharing between containers.
Create a volume with 'docker volume create NAME'.
Mount it with '-v NAME:/path' in containers.
Data persists beyond container life.
Containers share files via the volume path.
Full Transcript
This visual execution shows how Docker named volumes enable data sharing between containers. First, a named volume 'shared_data' is created. Then containerA starts with this volume mounted at /data and writes a file greeting.txt with content 'hello'. Next, containerB starts with the same volume mounted and reads the file, outputting 'hello'. Even after containerA stops and is removed, the data remains in the named volume. This demonstrates persistent and shared storage using Docker named volumes.