0
0
Dockerdevops~20 mins

Volumes in Compose in Docker - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Volume Mastery in Compose
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Volume Mount Behavior in Docker Compose
Given the following docker-compose.yml snippet, what will be the output of docker volume ls after running docker-compose up -d?
Docker
version: '3.8'
services:
  app:
    image: busybox
    volumes:
      - data-volume:/data
volumes:
  data-volume:
A
DRIVER    VOLUME NAME
local     data-volume
B
DRIVER    VOLUME NAME
local     project_data-volume
C
DRIVER    VOLUME NAME
local     app_data-volume
DNo volumes listed
Attempts:
2 left
💡 Hint
Docker Compose prefixes volume names with the project directory name by default.
Configuration
intermediate
2:00remaining
Correct Volume Syntax in Compose
Which option correctly defines a named volume and mounts it to /var/lib/data inside the container in a docker-compose.yml file?
A
volumes:
  - mydata:/var/lib/data
volumes:
  mydata: {}
B
volumes:
  - /var/lib/data:mydata
volumes:
  mydata:
C
volumes:
  - /var/lib/data:/mydata
volumes:
  mydata:
D
volumes:
  - mydata:/var/lib/data
volumes:
  mydata:
Attempts:
2 left
💡 Hint
Named volumes are declared under top-level volumes and referenced by name in services.
Troubleshoot
advanced
2:00remaining
Volume Mount Not Persisting Data
You have this docker-compose.yml: version: '3.8' services: db: image: postgres volumes: - ./dbdata:/var/lib/postgresql/data After restarting the container, your data is lost. What is the most likely cause?
AThe host directory './dbdata' is empty and overwrites the container data directory.
BThe volume is not declared under top-level volumes section.
CPostgres requires a named volume, bind mounts are not supported.
DThe container image does not support volumes.
Attempts:
2 left
💡 Hint
Bind mounts use host directories which can overwrite container data if empty.
🔀 Workflow
advanced
2:00remaining
Volume Sharing Between Services
You want two services in your Compose file to share the same persistent data. Which volume configuration achieves this?
Docker
version: '3.8'
services:
  service1:
    image: alpine
    volumes:
      - shared-data:/data
  service2:
    image: alpine
    volumes:
      - shared-data:/data
volumes:
  shared-data:
AUse bind mounts with different host paths for each service.
BEach service uses a different named volume with the same path '/data'.
CBoth services mount the same named volume 'shared-data' to '/data'.
DDeclare volumes only under services, not top-level volumes.
Attempts:
2 left
💡 Hint
Named volumes can be shared by multiple services by referencing the same volume name.
Best Practice
expert
2:00remaining
Best Practice for Volume Declaration in Compose
Which option follows the best practice for declaring and using volumes in a Docker Compose file for production environments?
ADeclare named volumes under top-level volumes and mount them in services; avoid bind mounts for persistent data.
BUse bind mounts for all data to easily access files on the host system.
CDeclare volumes only inside service definitions without top-level volumes section.
DUse anonymous volumes without names to keep configuration simple.
Attempts:
2 left
💡 Hint
Named volumes managed by Docker provide better portability and data safety than bind mounts.