0
0
Dockerdevops~10 mins

Sharing volumes between containers in Docker - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Sharing volumes between containers
Create Volume
Start Container A with Volume
Container A writes data
Start Container B with same Volume
Container B reads data
Data shared via Volume
This flow shows how a volume is created and shared between two containers, allowing data written by one to be accessed by the other.
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 volume, run containerA writing a file to it, then run containerB to read the file from the same volume.
Process Table
StepCommandActionResultOutput
1docker volume create shared_dataCreate a named volumeVolume 'shared_data' created
2docker run -d --name containerA -v shared_data:/data busybox sh -c "echo hello > /data/greeting.txt && sleep 3600"Start containerA with volume mountedContainerA running, file written to /data/greeting.txt
3docker run --rm -v shared_data:/data busybox cat /data/greeting.txtRun containerB to read file from volumeContainerB runs and reads filehello
4ContainerA stopped or removedVolume persists independentlyData remains in volume
💡 ContainerB finishes reading and exits; volume data persists beyond container lifecycle.
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
Volume 'shared_data'Does not existCreatedContains greeting.txt with 'hello'Accessible with 'hello' contentRemains with data after containers exit
ContainerANot runningNot runningRunning, wrote fileRunningStopped or removed
ContainerBNot runningNot runningNot runningRuns, reads fileExited
Key Moments - 3 Insights
Why does containerB see the file written by containerA?
Because both containers mount the same named volume 'shared_data', so data written by containerA is stored in the volume and accessible to containerB, as shown in execution_table step 3.
Does the volume get deleted when containerA stops?
No, the volume persists independently of containers. Even after containerA stops or is removed (step 4), the data remains in the volume for other containers to use.
What happens if containerB tries to read before containerA writes the file?
ContainerB would see no file or empty content because the volume is empty initially. The file appears only after containerA writes it, as shown in step 2 and 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what output does containerB produce?
A"hello"
B"greeting.txt not found"
C"" (empty output)
DError: volume not found
💡 Hint
Check the 'Output' column in execution_table row for step 3.
At which step does the volume 'shared_data' get created?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the 'Action' column in execution_table for volume creation.
If containerA did not write the file, what would containerB see at step 3?
AError: volume missing
B"hello"
C"greeting.txt not found" or empty output
DContainerB would not start
💡 Hint
Refer to key_moments explanation about file presence before writing.
Concept Snapshot
Sharing volumes between containers:
- Create a named volume with 'docker volume create'
- Mount it in containers with '-v volume_name:/path'
- Data written by one container is accessible to others
- Volumes persist beyond container lifecycle
- Useful for sharing files or data between containers
Full Transcript
This visual execution shows how Docker volumes enable sharing data between containers. First, a volume named 'shared_data' is created. Then containerA starts with this volume mounted and writes a file 'greeting.txt' inside it. Next, containerB runs with the same volume mounted and reads the file, outputting 'hello'. The volume persists even after containers stop, allowing data sharing. Key points include volume creation, mounting, data persistence, and container independence from volume lifecycle.