📖 Scenario: You want to run a simple web app that needs two parts: a web server and a database. Managing each part separately is hard and slow.Docker Compose helps you start both parts together with one command.
🎯 Goal: Build a Docker Compose setup that runs a web server and a database together easily.
📋 What You'll Learn
Create a docker-compose.yml file with two services: web and db
Set the web service to use the nginx:latest image
Set the db service to use the mysql:5.7 image with environment variables
Use Docker Compose commands to start and show running containers
💡 Why This Matters
🌍 Real World
Many apps need multiple parts like a web server and database. Docker Compose helps run them together easily.
💼 Career
DevOps engineers use Docker Compose to manage multi-container apps during development and testing.
Progress0 / 4 steps
1
Create the initial Docker Compose file
Create a file named docker-compose.yml with a version set to '3.8' and an empty services section.
Docker
Hint
The version key defines the Compose file format version. The services section will hold your app parts.
2
Add the web and db services
Add two services under services: web using image nginx:latest and db using image mysql:5.7.
Docker
Hint
Indent the services properly under services. Use image to specify the container image.
3
Configure environment variables for the db service
Under the db service, add an environment section with MYSQL_ROOT_PASSWORD set to example.
Docker
Hint
Environment variables are set as key-value pairs under environment.
4
Start the app and check running containers
Run docker-compose up -d to start the services in the background, then run docker-compose ps to list running containers.
Docker
Hint
Use docker-compose up -d to start containers quietly in the background. Use docker-compose ps to see their status.
Practice
(1/5)
1. What is the main benefit of using Docker Compose for multi-container applications?
easy
A. It allows managing multiple containers with a single configuration file and command.
B. It replaces Docker Engine for running containers.
C. It automatically updates container images without user input.
D. It only works with single-container applications.
Solution
Step 1: Understand Docker Compose purpose
Docker Compose uses one file to define and run multiple containers together easily.
Step 2: Compare options
Options B, C, and D describe incorrect or unrelated features. Only It allows managing multiple containers with a single configuration file and command. correctly states the main benefit.
Final Answer:
It allows managing multiple containers with a single configuration file and command. -> Option A
Quick Check:
Single file + command = easier multi-container management [OK]
Hint: Think: one file controls many containers [OK]
Common Mistakes:
Confusing Docker Compose with Docker Engine
Believing Compose updates images automatically
Thinking Compose works only for one container
2. Which of the following is the correct command to start all services defined in a docker-compose.yml file?
easy
A. docker-compose run
B. docker-compose up
C. docker-compose build
D. docker-compose start-all
Solution
Step 1: Identify the command to launch services
The docker-compose up command starts all containers defined in the compose file.
Step 2: Review other commands
run starts a one-off container, build builds images, and start-all is invalid.
Final Answer:
docker-compose up -> Option B
Quick Check:
Start all services = docker-compose up [OK]
Hint: Use 'up' to launch all services together [OK]
Common Mistakes:
Using 'run' instead of 'up' to start all containers
Confusing 'build' with starting containers
Using a non-existent command like 'start-all'
3. Given this docker-compose.yml snippet:
version: '3'
services:
web:
image: nginx
ports:
- "8080:80"
db:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: example
What happens when you run docker-compose up?
medium
A. Only the web service starts, exposing port 80 on 8080.
B. The db service starts but fails due to missing password.
C. Both web and db services start; web is accessible on port 8080.
D. No containers start because the file version is incorrect.
Solution
Step 1: Analyze the compose file
Two services: 'web' with nginx exposing port 8080:80, and 'db' with mysql and a root password set.
Step 2: Understand the command effect
docker-compose up starts all services defined, so both 'web' and 'db' start successfully.
Final Answer:
Both web and db services start; web is accessible on port 8080. -> Option C
Quick Check:
All services start with 'up' if config is valid [OK]
Hint: All services in file start with 'docker-compose up' [OK]
Common Mistakes:
Thinking only one service starts by default
Assuming missing environment variables cause failure here
Believing file version '3' is invalid
4. You wrote a docker-compose.yml but running docker-compose up gives an error: ERROR: yaml.scanner.ScannerError: mapping values are not allowed here. What is the most likely cause?
medium
A. Incorrect indentation or syntax in the YAML file.
B. Docker Compose is not installed on the system.
C. The Docker daemon is not running.
D. The image names are missing in the file.
Solution
Step 1: Understand the error message
The error indicates a YAML syntax problem, usually caused by wrong indentation or misplaced characters.
Step 2: Evaluate other options
Options B and C cause different errors, and missing image names cause different warnings, not YAML syntax errors.
Final Answer:
Incorrect indentation or syntax in the YAML file. -> Option A
Quick Check:
YAML errors = check indentation and syntax [OK]
Hint: YAML errors usually mean bad indentation [OK]
Common Mistakes:
Assuming Docker Compose is missing when error is YAML
Thinking Docker daemon status causes YAML errors
Ignoring indentation rules in YAML files
5. You want to share your multi-container app setup with a teammate. Which Docker Compose feature best helps you do this efficiently?
hard
A. Using Docker Swarm instead of Compose for local development.
B. Running each container manually with separate docker run commands.
C. Building images locally and sending them as tar files.
D. Using a single docker-compose.yml file to define all services and their settings.
Solution
Step 1: Identify sharing method for multi-container apps
A single docker-compose.yml file contains all service definitions and configurations, making sharing easy.
Step 2: Compare other options
Manual docker run commands are error-prone and complex; sending images as tar files is cumbersome; Docker Swarm is for orchestration, not simple sharing.
Final Answer:
Using a single docker-compose.yml file to define all services and their settings. -> Option D
Quick Check:
One file shares whole app setup easily [OK]
Hint: Share one compose file, not many commands [OK]
Common Mistakes:
Thinking manual commands are easier to share
Believing image tar files are best for sharing setup
Confusing Docker Swarm with Compose for local sharing
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.