📖 Scenario: You are setting up a simple web application using Docker. You want to define the services your app needs in a docker-compose.yml file.This will help you start all parts of your app with one command.
🎯 Goal: Create a docker-compose.yml file that defines two services: a web server and a database.The web server will use the nginx:latest image, and the database will use the mysql:5.7 image.
📋 What You'll Learn
Create a docker-compose.yml file
Define a service called web using the nginx:latest image
Define a service called db using the mysql:5.7 image
Add environment variables for the db service: MYSQL_ROOT_PASSWORD set to example
Print the final docker-compose.yml content
💡 Why This Matters
🌍 Real World
Docker Compose is used to run multi-container Docker applications easily by defining all services in one file.
💼 Career
Knowing how to define services in Docker Compose is essential for DevOps roles to manage application environments efficiently.
Progress0 / 4 steps
1
Create the base structure of docker-compose.yml
Create a docker-compose.yml file with the version set to "3.8" and an empty services section.
Docker
Hint
Start by writing version: "3.8" at the top, then add services: below it.
2
Define the web service using the nginx:latest image
Add a service called web under services that uses the image nginx:latest.
Docker
Hint
Indent the web service under services, then add image: nginx:latest under web.
3
Define the db service using the mysql:5.7 image with environment variables
Add a service called db under services that uses the image mysql:5.7 and has an environment variable MYSQL_ROOT_PASSWORD set to example.
Docker
Hint
Indent the db service under services. Add image: mysql:5.7 and then add an environment section with MYSQL_ROOT_PASSWORD: example.
4
Print the complete docker-compose.yml content
Print the entire content of the docker-compose.yml file exactly as defined in previous steps.
Docker
Hint
Use a multi-line string variable to hold the content and print it exactly.
Practice
(1/5)
1. What is the main purpose of defining services in a Docker Compose file?
easy
A. To install software inside containers manually
B. To write shell scripts for container startup
C. To group containers with their settings for easy management
D. To create virtual machines inside Docker
Solution
Step 1: Understand the role of services in Docker Compose
Services define how containers run, including images, ports, and environment variables.
Step 2: Recognize the benefit of grouping containers
Grouping containers with their settings allows easy start, stop, and management of multiple containers together.
Final Answer:
To group containers with their settings for easy management -> Option C
Quick Check:
Services group containers = D [OK]
Hint: Services group containers and settings in one file [OK]
Common Mistakes:
Confusing services with scripts
Thinking services create virtual machines
Believing services install software manually
2. Which of the following is the correct way to define a service named web using the image nginx in a Docker Compose YAML file?
easy
A. services:
web:
image: nginx
B. services:
web:
image nginx
C. service:
web:
image: nginx
D. services:
web:
img: nginx
Solution
Step 1: Check the correct YAML key for services
The top-level key must be services (plural), not service.
Step 2: Verify the syntax for image definition
The correct syntax is image: nginx with a colon and proper indentation.
Final Answer:
services:
web:
image: nginx -> Option A
Quick Check:
Correct YAML keys and syntax = B [OK]
Hint: Use 'services:' plural and 'image:' with colon [OK]
Common Mistakes:
Using 'service' instead of 'services'
Missing colon after 'image'
Using 'img' instead of 'image'
3. Given this Docker Compose snippet, what port will be accessible on the host machine?
services:
app:
image: myapp
ports:
- "8080:80"
medium
A. Port 80 on the host
B. Port 8080 on the host
C. Port 80 inside the container only
D. Port 8080 inside the container only
Solution
Step 1: Understand port mapping syntax
The format "host_port:container_port" means host port 8080 maps to container port 80.
Step 2: Identify which port is exposed on the host
Port 8080 on the host machine will forward traffic to port 80 inside the container.
Final Answer:
Port 8080 on the host -> Option B
Quick Check:
Host port is first in mapping = A [OK]
Hint: Host port is before colon in ports mapping [OK]
Common Mistakes:
Confusing host and container ports
Thinking container port is exposed on host
Ignoring quotes around ports
4. Identify the error in this Docker Compose service definition:
services:
db:
image: postgres
ports:
- "5432:"
environment:
POSTGRES_PASSWORD: example
medium
A. Missing container port in ports mapping
B. Environment variable name is incorrect
C. Image name should be capitalized
D. Service name cannot be 'db'
Solution
Step 1: Check ports syntax
Ports must be mapped as "host_port:container_port" or just container port. Here, "5432:" is invalid because missing container port after colon.
Step 2: Confirm environment and image correctness
Environment variable and image name are correct; service name 'db' is valid.
Final Answer:
Missing container port in ports mapping -> Option A
Quick Check:
Ports need host:container or container port only = A [OK]
Hint: Ports need host:container or container port only [OK]
Common Mistakes:
Using only one port number with quotes
Assuming environment variable names are wrong
Thinking service names have restrictions
5. You want to define two services, frontend and backend, where frontend depends on backend to start first. Which is the correct way to express this dependency in Docker Compose?