Bird
Raised Fist0
Dockerdevops~10 mins

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

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

Practice

(1/5)
1. What is the main purpose of using volumes in a Docker Compose file?
easy
A. To limit container CPU usage
B. To speed up container startup time
C. To automatically update container images
D. To keep data safe outside containers so it persists

Solution

  1. Step 1: Understand volume role in Docker

    Volumes store data outside the container's writable layer, so data is not lost when containers stop or are removed.
  2. Step 2: Compare options

    Options B, C, and D relate to performance or resource limits, not data persistence.
  3. Final Answer:

    To keep data safe outside containers so it persists -> Option D
  4. Quick Check:

    Volumes = Data persistence [OK]
Hint: Volumes = persistent data storage outside containers [OK]
Common Mistakes:
  • Thinking volumes speed up container startup
  • Confusing volumes with resource limits
  • Assuming volumes update images automatically
2. Which of the following is the correct syntax to define a named volume in a Docker Compose file?
easy
A. services: volumes: - mydata:/app/data
B. volumes: - mydata:/app/data
C. volumes: mydata: driver: local
D. volumes: mydata: /app/data

Solution

  1. Step 1: Identify volume declaration section

    Named volumes are declared under the top-level 'volumes:' key with a name and optional driver.
  2. Step 2: Check syntax correctness

    volumes: mydata: driver: local correctly defines a named volume 'mydata' with driver 'local'. Options A, B, and D mix service and volume syntax incorrectly or have invalid mapping syntax.
  3. Final Answer:

    volumes: mydata: driver: local -> Option C
  4. Quick Check:

    Named volume syntax = volumes: mydata: driver: local [OK]
Hint: Define named volumes under top-level 'volumes:' with name and driver [OK]
Common Mistakes:
  • Defining volumes under services instead of top-level
  • Using list syntax for volume declaration
  • Incorrect mapping format for volumes
3. Given this snippet in a Docker Compose file:
services:
  web:
    image: nginx
    volumes:
      - mydata:/usr/share/nginx/html
volumes:
  mydata:
What happens to the data inside /usr/share/nginx/html when the container restarts?
medium
A. Data persists because 'mydata' is a named volume
B. Data is shared with the host folder automatically
C. Data is reset to the image default every restart
D. Data is lost because volumes do not persist

Solution

  1. Step 1: Understand named volume behavior

    Named volumes like 'mydata' store data outside the container and keep it between restarts.
  2. Step 2: Analyze the mount point

    The volume is mounted to '/usr/share/nginx/html', so data written there is saved in 'mydata' volume.
  3. Final Answer:

    Data persists because 'mydata' is a named volume -> Option A
  4. Quick Check:

    Named volume mount = data persistence [OK]
Hint: Named volumes keep data safe across container restarts [OK]
Common Mistakes:
  • Assuming data resets to image default
  • Confusing named volumes with bind mounts
  • Thinking volumes do not persist data
4. You wrote this in your Docker Compose file but get an error:
services:
  app:
    image: myapp
    volumes:
      - ./data:/app/data
volumes:
  ./data:
What is the problem?
medium
A. You cannot use relative paths under 'volumes:' top-level key
B. The image name 'myapp' is invalid
C. Bind mounts must be declared under 'volumes:' top-level key
D. The service name 'app' is reserved

Solution

  1. Step 1: Understand volume declaration rules

    Top-level 'volumes:' is for named volumes only, not for bind mounts with paths.
  2. Step 2: Identify misuse of relative path

    Using './data:' under 'volumes:' top-level is invalid syntax; relative paths belong only in service volume mounts.
  3. Final Answer:

    You cannot use relative paths under 'volumes:' top-level key -> Option A
  4. Quick Check:

    Relative paths only in service volumes [OK]
Hint: Declare bind mounts only under service volumes, not top-level [OK]
Common Mistakes:
  • Putting bind mount paths under top-level volumes
  • Assuming image name causes this error
  • Thinking service names are restricted
5. You want to share a local folder ./config with a container's /etc/config path and also keep a database's data persistent using a named volume dbdata. Which is the correct Docker Compose snippet?
hard
A. services: app: image: myapp volumes: - dbdata:/etc/config db: image: postgres volumes: - ./config:/var/lib/postgresql/data volumes: dbdata:
B. services: app: image: myapp volumes: - ./config:/etc/config db: image: postgres volumes: - dbdata:/var/lib/postgresql/data volumes: dbdata:
C. services: app: image: myapp volumes: - /etc/config:./config db: image: postgres volumes: - /var/lib/postgresql/data:dbdata volumes: dbdata:
D. services: app: image: myapp volumes: - ./config:/etc/config db: image: postgres volumes: - ./dbdata:/var/lib/postgresql/data volumes: dbdata:

Solution

  1. Step 1: Identify bind mount for local folder sharing

    Bind mounts use local paths like './config:/etc/config' under service volumes.
  2. Step 2: Identify named volume for database persistence

    Named volumes like 'dbdata' are declared top-level and mounted as 'dbdata:/var/lib/postgresql/data'.
  3. Step 3: Check options for correct syntax

    services: app: image: myapp volumes: - ./config:/etc/config db: image: postgres volumes: - dbdata:/var/lib/postgresql/data volumes: dbdata: correctly uses bind mount for app and named volume for db. Options B and C swap paths incorrectly. services: app: image: myapp volumes: - ./config:/etc/config db: image: postgres volumes: - ./dbdata:/var/lib/postgresql/data volumes: dbdata: uses a bind mount for dbdata instead of named volume.
  4. Final Answer:

    services: app: image: myapp volumes: - ./config:/etc/config db: image: postgres volumes: - dbdata:/var/lib/postgresql/data volumes: dbdata: -> Option B
  5. Quick Check:

    Bind mount + named volume = services: app: image: myapp volumes: - ./config:/etc/config db: image: postgres volumes: - dbdata:/var/lib/postgresql/data volumes: dbdata: [OK]
Hint: Use bind mounts for local folders, named volumes for persistent data [OK]
Common Mistakes:
  • Swapping source and target paths
  • Using bind mounts instead of named volumes for DB data
  • Declaring bind mounts under top-level volumes
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