What is the purpose of the version key in a docker-compose.yml file?
The version key specifies the Compose file format version. It tells Docker Compose which syntax rules to follow when reading the file.
Click to reveal answer
beginner
What does the services section define in a docker-compose.yml file?
The services section lists all the containers (services) that Docker Compose will create and manage. Each service has its own configuration like image, ports, and environment variables.
Click to reveal answer
intermediate
In docker-compose.yml, what is the role of the volumes key under a service?
The volumes key connects storage from the host machine or named volumes to the container, allowing data to persist or be shared between containers.
Click to reveal answer
beginner
How do you specify which ports to expose from a container in docker-compose.yml?
Use the ports key under a service. It maps host ports to container ports in the format host_port:container_port.
Click to reveal answer
beginner
What is the purpose of the environment section in a docker-compose.yml service?
The environment section sets environment variables inside the container. These variables can configure the app or service running inside.
Click to reveal answer
Which key in docker-compose.yml defines the containers to run?
Avolumes
Bnetworks
Cservices
Dimages
✗ Incorrect
The services key lists all containers Docker Compose will create.
How do you map port 8080 on your computer to port 80 in the container?
Aports: - "8080:80"
Bports: - "80"
Cports: - "8080"
Dports: - "80:8080"
✗ Incorrect
The format is host_port:container_port, so 8080:80 maps host port 8080 to container port 80.
What does the volumes key do in a service definition?
ASets environment variables
BConnects storage between host and container
CDefines network settings
DSpecifies container image
✗ Incorrect
Volumes connect storage from the host or named volumes to containers for data persistence.
Where do you specify environment variables for a container in docker-compose.yml?
Aunder <code>environment</code> key
Bunder <code>ports</code> key
Cunder <code>volumes</code> key
Dunder <code>networks</code> key
✗ Incorrect
The environment key sets environment variables inside the container.
What is the correct file format for docker-compose.yml?
AJSON
BXML
CINI
DYAML
✗ Incorrect
docker-compose.yml uses YAML format, which is easy to read and write.
Describe the main sections of a docker-compose.yml file and their purposes.
Think about what each part controls in running containers.
You got /5 concepts.
Explain how you would expose a web app running on port 80 inside a container to port 3000 on your computer using docker-compose.yml.
Remember the format is host_port:container_port.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of the services section in a docker-compose.yml file?
easy
A. To define the containers that will run and their settings
B. To specify the Docker version to use
C. To list the volumes to be created
D. To set environment variables globally
Solution
Step 1: Understand the role of services
The services section lists all containers to run and their configurations.
Step 2: Compare with other sections
version sets compose file version, volumes defines shared storage, environment variables are per service or global but not the main purpose of services.
Final Answer:
To define the containers that will run and their settings -> Option A
Quick Check:
services = container definitions [OK]
Hint: Services section lists containers and their configs [OK]
Common Mistakes:
Confusing version with services
Thinking volumes are defined inside services only
Assuming environment variables are global by default
2. Which of the following is the correct way to specify a port mapping in docker-compose.yml under a service named web?
easy
A. port:
- "80:80"
B. ports:
- "80:80"
C. ports:
- 80:80
D. mapping:
- "80:80"
Solution
Step 1: Check correct key for port mapping
The correct key is ports (plural), not port or mapping.
Step 2: Validate syntax for port mapping
Port mappings must be strings with quotes like "80:80" to avoid YAML parsing issues.
Final Answer:
ports:
- "80:80" -> Option B
Quick Check:
Use ports with quoted strings [OK]
Hint: Use 'ports' key with quoted strings for mappings [OK]
A. Port mapping should be quoted strings like "5432:5432"
B. Environment variables must be listed as a sequence, not mapping
C. The version '3' is invalid and should be '3.8'
D. The image name 'postgres' must include a tag
Solution
Step 1: Check port mapping syntax
Port mappings should be quoted strings like "5432:5432" to avoid YAML parsing errors.
Step 2: Validate environment variable format
Environment variables can be mappings like POSTGRES_PASSWORD: example, which is correct here.
Final Answer:
Port mapping should be quoted strings like "5432:5432" -> Option A
Quick Check:
Port mappings need quotes to parse correctly [OK]
Hint: Always quote port mappings to avoid YAML errors [OK]
Common Mistakes:
Not quoting port mappings
Confusing environment variable formats
Thinking version '3' is invalid
5. You want to run two services, web and db, where web depends on db. Which docker-compose.yml snippet correctly expresses this dependency and shares a volume named data between them?
hard
A. version: '3'
services:
web:
image: webapp
depends_on:
- db
db:
image: postgres
volumes:
data:
external: true