0
0
Dockerdevops~10 mins

Volumes in Compose in Docker - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Volumes in Compose
Define volume in docker-compose.yml
Start containers with volumes
Docker creates volume if missing
Container mounts volume at path
Data persists outside container lifecycle
Stop/remove container, volume remains
Reuse volume in new container
Data intact
This flow shows how volumes are defined in Compose, created by Docker, mounted inside containers, and persist data beyond container life.
Execution Sample
Docker
version: '3.8'
services:
  app:
    image: alpine
    volumes:
      - mydata:/data
volumes:
  mydata:
A Compose file defining a volume 'mydata' mounted inside the 'app' container at /data.
Process Table
StepActionDocker State ChangeVolume StateContainer State
1Parse docker-compose.ymlRecognizes volume 'mydata'Volume 'mydata' not yet createdNo containers running
2docker-compose up starts 'app'Creates volume 'mydata' if missingVolume 'mydata' created and emptyContainer 'app' running, mounts /data to 'mydata'
3Write file inside container at /data/file.txtVolume 'mydata' stores file dataVolume 'mydata' contains file.txtFile visible inside container at /data/file.txt
4Stop and remove container 'app'Container removedVolume 'mydata' persists with file.txtNo containers running
5Start new container mounting 'mydata'Container mounts existing volumeVolume 'mydata' still has file.txtFile visible inside new container at /data/file.txt
💡 Volume persists after container removal, data remains accessible in new containers.
Status Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5
Volume 'mydata'Not createdCreated emptyContains file.txtContains file.txtContains file.txt
Container 'app'Not runningRunning with volume mountedRunning with file inside volumeStopped and removedNew container running with volume mounted
Key Moments - 3 Insights
Why does the data inside the volume still exist after the container is removed?
Because volumes are stored outside the container's writable layer, they persist independently. As shown in step 4 of the execution_table, the container is removed but the volume still holds the data.
What happens if the volume is not defined under 'volumes:' in the Compose file?
Docker Compose will create an anonymous volume automatically, but it won't have a reusable name. Defining it explicitly under 'volumes:' allows reuse and better management, as seen in step 1.
How does mounting a volume affect the container's filesystem?
Mounting a volume replaces the container's directory at the mount point with the volume's data. So files written inside the container at that path are stored in the volume, as shown in step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state of volume 'mydata' after step 2?
ACreated and empty
BNot created
CContains file.txt
DDeleted
💡 Hint
Check the 'Volume State' column at step 2 in the execution_table.
At which step does the container get removed but the volume still keeps data?
AStep 3
BStep 5
CStep 4
DStep 1
💡 Hint
Look for 'Container removed' and 'Volume persists' in the execution_table.
If you remove the 'volumes:' section from the Compose file, what changes in the execution?
AContainer won't start
BVolume 'mydata' won't be created explicitly
CData won't persist after container removal
DVolume will be shared between containers automatically
💡 Hint
Refer to the key_moments about volume definition and creation.
Concept Snapshot
Volumes in Compose:
- Define volumes under 'volumes:' in docker-compose.yml
- Mount volumes in services with 'volumes:' key
- Docker creates volumes if missing
- Volumes persist data beyond container life
- Reuse volumes by naming them explicitly
Full Transcript
This visual execution shows how Docker Compose handles volumes. First, the Compose file defines a named volume 'mydata'. When 'docker-compose up' runs, Docker creates this volume if it doesn't exist. The container mounts this volume at /data. Writing files inside the container at /data stores them in the volume. When the container stops and is removed, the volume remains with the data intact. Starting a new container mounting the same volume shows the data is still there. This demonstrates volumes keep data independent of container lifecycle, enabling persistent storage in Compose setups.