Bird
Raised Fist0
Dockerdevops~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of sharing volumes between Docker containers?
easy
A. To automatically update container images
B. To increase the CPU performance of containers
C. To allow containers to access and modify the same files
D. To isolate containers completely from each other

Solution

  1. Step 1: Understand volume sharing concept

    Sharing volumes means multiple containers can read and write the same files stored in a volume.
  2. Step 2: Identify the purpose of sharing volumes

    This allows containers to share data easily, like sharing notes or files in a folder between friends.
  3. Final Answer:

    To allow containers to access and modify the same files -> Option C
  4. Quick Check:

    Sharing volumes = shared files access [OK]
Hint: Sharing volumes means sharing files between containers [OK]
Common Mistakes:
  • Thinking volumes improve CPU or performance
  • Confusing volume sharing with container isolation
  • Assuming volumes update container images automatically
2. Which Docker command syntax correctly shares a volume named shared_data between two containers app1 and app2?
easy
A. docker run --volumes shared_data:/data --name app1 busybox
B. docker run -v shared_data:/data --name app1 busybox
C. docker run -v /shared_data:/data --name app1 busybox
D. docker run -v shared_data --name app1 --link app2 busybox

Solution

  1. Step 1: Check volume syntax for sharing

    The correct syntax to share a named volume is -v volume_name:container_path. docker run -v shared_data:/data --name app1 busybox uses -v shared_data:/data which is correct.
  2. Step 2: Verify other options

    docker run --volumes shared_data:/data --name app1 busybox uses invalid --volumes flag (should be --volume or -v). docker run -v /shared_data:/data --name app1 busybox uses a host path, not a named volume. docker run -v shared_data --name app1 --link app2 busybox uses incomplete volume syntax (missing :/data) and --link which is unrelated to volume sharing.
  3. Final Answer:

    docker run -v shared_data:/data --name app1 busybox -> Option B
  4. Quick Check:

    Use -v volume_name:path to share volumes [OK]
Hint: Use -v volume_name:container_path to share volumes [OK]
Common Mistakes:
  • Using host paths instead of named volumes
  • Confusing --link with volume sharing
  • Using incorrect volume syntax
3. Given these commands, what will be the output of docker exec app2 cat /data/message.txt?
docker volume create shared_vol
docker run -d -v shared_vol:/data --name app1 busybox sh -c "echo Hello > /data/message.txt"
docker run -d -v shared_vol:/data --name app2 busybox sleep 1000
medium
A. Empty output
B. Error: file not found
C. Container app2 does not exist
D. Hello

Solution

  1. Step 1: Understand volume sharing effect

    Both containers use the same volume shared_vol mounted at /data. Writing in app1 creates the file in the shared volume.
  2. Step 2: Check file access from app2

    Since app2 mounts the same volume, it can read the file /data/message.txt created by app1. So the command outputs "Hello".
  3. Final Answer:

    Hello -> Option D
  4. Quick Check:

    Shared volume files visible in all containers [OK]
Hint: Shared volumes show same files in all containers [OK]
Common Mistakes:
  • Assuming files are isolated per container
  • Expecting file not found error
  • Confusing container names or existence
4. You tried to share a volume between two containers but the second container cannot see the files created by the first. Which of these is the most likely cause?
medium
A. The volume was not named the same in both containers
B. The containers are running different images
C. The containers are on different networks
D. The first container did not expose ports

Solution

  1. Step 1: Check volume naming consistency

    For containers to share data, they must use the exact same volume name. Different names create separate volumes.
  2. Step 2: Evaluate other options

    Different images or networks do not affect volume sharing. Port exposure is unrelated to file sharing.
  3. Final Answer:

    The volume was not named the same in both containers -> Option A
  4. Quick Check:

    Same volume name needed to share data [OK]
Hint: Use identical volume names to share data [OK]
Common Mistakes:
  • Thinking network or image affects volume sharing
  • Confusing port exposure with volumes
  • Using different volume names by mistake
5. You want two containers to share a volume but keep their own separate data initially, then merge changes later. Which Docker feature or approach helps achieve this?
hard
A. Use named volumes with different initial content and copy files manually
B. Use a bind mount to a host directory shared by both containers
C. Use Docker networks to share data between containers
D. Run containers with --rm to remove volumes after exit

Solution

  1. Step 1: Understand volume sharing with initial separate data

    Named volumes start empty by default. To have separate initial data, create volumes separately and copy files manually.
  2. Step 2: Consider merging changes later

    After initial separate data, you can copy or sync files between volumes or containers to merge changes.
  3. Step 3: Evaluate other options

    Bind mounts share host directories but do not isolate initial data. Networks do not share files. --rm removes containers and volumes, not helpful here.
  4. Final Answer:

    Use named volumes with different initial content and copy files manually -> Option A
  5. Quick Check:

    Named volumes + manual copy = separate then merge [OK]
Hint: Start with separate volumes, copy files to merge later [OK]
Common Mistakes:
  • Assuming networks share files
  • Using --rm which deletes volumes
  • Thinking bind mounts isolate initial data
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard