📖 Scenario: You are setting up a simple web application using Docker Compose. This app has a web server and a database. You will create a docker-compose.yml file step-by-step to define these services.
🎯 Goal: Build a docker-compose.yml file that defines two services: web and db. The web service uses the nginx:latest image and the db service uses the mysql:5.7 image.
📋 What You'll Learn
Create a version key with value '3.8' in the YAML file
Define a services section with two services: web and db
Set the image for web to nginx:latest
Set the image for db to mysql:5.7
Add environment variables MYSQL_ROOT_PASSWORD: example to the db service
💡 Why This Matters
🌍 Real World
Docker Compose is used to run multi-container Docker applications easily on your local machine or servers.
💼 Career
Understanding <code>docker-compose.yml</code> files is essential for DevOps roles to manage containerized applications efficiently.
Progress0 / 4 steps
1
Create the YAML version key
Create a docker-compose.yml file and add the version key with the value '3.8' at the top.
Docker
Hint
The version key tells Docker Compose which file format version you are using.
2
Add the services section with web and db
Add a services section below version. Inside services, define two services: web and db.
Docker
Hint
The services section groups all your app's containers.
3
Set images for web and db services
Under the web service, add image: nginx:latest. Under the db service, add image: mysql:5.7.
Docker
Hint
The image key tells Docker which container image to use for each service.
4
Add environment variable to db service
Add an environment section under the db service with MYSQL_ROOT_PASSWORD: example.
Docker
Hint
Environment variables configure the container. Here, it sets the MySQL root password.
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