0
0
Dockerdevops~10 mins

Volumes for persistent data in Docker - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Volumes for persistent data
Create Volume
Start Container with Volume
Write Data inside Container
Stop Container
Start New Container with Same Volume
Read Data from Volume
Data Persists Across Containers
This flow shows how Docker volumes are created and used to keep data safe even when containers stop or are removed.
Execution Sample
Docker
docker volume create mydata

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

docker run --rm -v mydata:/data busybox cat /data/greeting.txt
Create a volume, run a container that writes data to it, then run another container to read the data back.
Process Table
StepCommandActionResultData State
1docker volume create mydataCreate volume named 'mydata'Volume 'mydata' createdEmpty volume created
2docker run -d --name app1 -v mydata:/data busybox sh -c "echo hello > /data/greeting.txt && sleep 3600"Start container 'app1' with volume 'mydata' mounted at /data; write 'hello' to greeting.txtContainer 'app1' running; file writtenVolume 'mydata' contains greeting.txt with 'hello'
3docker stop app1Stop container 'app1'Container 'app1' stoppedData in volume unchanged
4docker run --rm -v mydata:/data busybox cat /data/greeting.txtRun temporary container mounting 'mydata' to read greeting.txtOutput: helloData read successfully from volume
5docker volume rm mydataAttempt to remove volume 'mydata'Error: volume is in use or does not existVolume still exists because in use or not removed
6docker rm app1Remove container 'app1'Container 'app1' removedVolume still exists with data
7docker volume rm mydataRemove volume 'mydata'Volume 'mydata' removedVolume deleted, data lost
💡 Volume removed or container stopped; data persists until volume is deleted
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 6Final
Volume 'mydata'NoneCreated (empty)Contains greeting.txt with 'hello'Data unchangedData unchangedData unchangedDeleted
Container 'app1'NoneNoneRunningStoppedStoppedRemovedNone
Key Moments - 3 Insights
Why does the data still exist after stopping the container?
Because the data is stored in the Docker volume 'mydata', which is independent of the container's lifecycle, as shown in execution_table step 3 and 4.
What happens if you remove the container but not the volume?
The volume and its data remain intact, so you can attach it to another container later. This is shown in execution_table step 6 where the container is removed but the volume still holds data.
Why can't you remove a volume if it is still in use by a container?
Docker prevents removing volumes that are in use to avoid data loss, as seen in execution_table step 5 where removal fails because the container is still using the volume.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the content of the volume 'mydata' after step 2?
AContains greeting.txt with 'hello'
BVolume deleted
CEmpty volume
DContains no files
💡 Hint
Check the 'Data State' column at step 2 in the execution_table.
At which step is the container 'app1' removed?
AStep 4
BStep 6
CStep 3
DStep 7
💡 Hint
Look at the 'Command' and 'Result' columns for container removal in execution_table.
If you remove the volume before removing the container, what will happen?
AVolume is removed successfully
BContainer stops automatically
CError because volume is in use
DData is copied to container
💡 Hint
Refer to step 5 in execution_table where volume removal fails due to usage.
Concept Snapshot
Docker volumes store data outside containers.
Create with 'docker volume create'.
Mount with '-v volume_name:/path'.
Data persists after container stops.
Remove volume to delete data.
Volumes prevent data loss across container restarts.
Full Transcript
This lesson shows how Docker volumes help keep data safe even when containers stop or are removed. First, you create a volume with 'docker volume create'. Then you start a container and mount the volume inside it. The container writes data to the volume. When the container stops or is removed, the data remains in the volume. You can start another container and mount the same volume to read the data. The volume only disappears when you explicitly remove it. This way, volumes help keep your data persistent and safe across container lifecycles.