A volume is a storage area outside the container's writable layer. It keeps data safe even if the container is deleted or recreated.
Click to reveal answer
beginner
How do you define a named volume in a docker-compose.yml file?
You define it under the 'volumes:' key at the bottom of the file, like this:
volumes:
mydata:
Click to reveal answer
intermediate
What is the difference between a bind mount and a volume in Docker Compose?
A bind mount links a host folder directly to the container, while a volume is managed by Docker and stored in Docker's storage area.
Click to reveal answer
beginner
How do you attach a volume to a service in docker-compose.yml?
Under the service, use the 'volumes:' key and specify the volume name and container path, like:
volumes:
- mydata:/app/data
Click to reveal answer
beginner
Why use volumes in Docker Compose?
Volumes keep data persistent, share data between containers, and separate data from container lifecycle.
Click to reveal answer
Where are Docker volumes stored by default?
AInside the container's writable layer
BIn the container's memory
COn a remote cloud server
DIn Docker's managed storage area on the host
✗ Incorrect
Docker volumes are stored in a special area on the host managed by Docker, separate from the container's writable layer.
How do you declare a volume for a service in docker-compose.yml?
AIn the Dockerfile
BUnder the service's 'volumes:' key with volume_name:container_path
CUnder the 'services:' key only
DBy running a docker volume create command inside the container
✗ Incorrect
Volumes are declared under the service's 'volumes:' key in docker-compose.yml using the format volume_name:container_path.
What happens to data in a volume if the container is deleted?
AData is moved to a backup folder automatically
BData is deleted with the container
CData remains safe in the volume
DData is lost permanently
✗ Incorrect
Volumes keep data safe even if the container is deleted or recreated.
Which of these is NOT a benefit of using volumes?
AIncreasing container CPU speed
BSharing data between containers
CSeparating data from container lifecycle
DData persistence
✗ Incorrect
Volumes help with data persistence and sharing but do not affect container CPU speed.
What is a bind mount in Docker Compose?
AA direct link between a host folder and container folder
BA volume managed by Docker
CA network connection between containers
DA special type of container
✗ Incorrect
A bind mount links a host folder directly to a container folder.
Explain how to define and use a volume in a docker-compose.yml file.
Think about where volumes are declared and how services use them.
You got /3 concepts.
Describe the benefits of using volumes in Docker Compose.
Why do we want data to live outside containers?
You got /3 concepts.
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
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.
Step 2: Compare options
Options B, C, and D relate to performance or resource limits, not data persistence.
Final Answer:
To keep data safe outside containers so it persists -> Option D
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
Step 1: Identify volume declaration section
Named volumes are declared under the top-level 'volumes:' key with a name and optional driver.
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.
Final Answer:
volumes:
mydata:
driver: local -> Option C
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
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
Step 1: Understand volume declaration rules
Top-level 'volumes:' is for named volumes only, not for bind mounts with paths.
Step 2: Identify misuse of relative path
Using './data:' under 'volumes:' top-level is invalid syntax; relative paths belong only in service volume mounts.
Final Answer:
You cannot use relative paths under 'volumes:' top-level key -> Option A
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?
Step 1: Identify bind mount for local folder sharing
Bind mounts use local paths like './config:/etc/config' under service volumes.
Step 2: Identify named volume for database persistence
Named volumes like 'dbdata' are declared top-level and mounted as 'dbdata:/var/lib/postgresql/data'.
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.