📖 Scenario: You are working on a small web application that uses two services: a web server and a database. You want to manage these services easily using Docker Compose.
🎯 Goal: Learn how to write a simple docker-compose.yml file to define two services, then use docker compose up to start them and docker compose down to stop and remove them.
📋 What You'll Learn
Create a docker-compose.yml file with two services: web and db
The web service uses the nginx:latest image
The db service uses the mysql:5.7 image
Use docker compose up to start the services
Use docker compose down to stop and remove the services
💡 Why This Matters
🌍 Real World
Docker Compose is widely used to manage multi-container applications easily on a developer's machine or in testing environments.
💼 Career
Knowing how to use Docker Compose is essential for DevOps roles to automate and manage containerized applications efficiently.
Progress0 / 4 steps
1
Create the docker-compose.yml file with two services
Create a file named docker-compose.yml with two services: web using the image nginx:latest and db using the image mysql:5.7. Use version '3.8' at the top.
Docker
Hint
Start with version: '3.8', then add services with web and db keys and their images.
2
Add environment variable for MySQL root password
Add an environment variable MYSQL_ROOT_PASSWORD with value example under the db service in docker-compose.yml.
Docker
Hint
Under db, add environment: and then MYSQL_ROOT_PASSWORD: example.
3
Start the services using docker compose up
Run the command docker compose up -d in the terminal to start the web and db services in detached mode.
Docker
Hint
Use docker compose up -d to start services in the background.
4
Stop and remove the services using docker compose down
Run the command docker compose down in the terminal to stop and remove the web and db services.
Docker
Hint
Use docker compose down to stop and clean up the containers.
Practice
(1/5)
1. What does the command docker compose up do in a multi-container application?
easy
A. Deletes the docker-compose file
B. Stops and removes all containers
C. Starts all the containers defined in the docker-compose file
D. Builds images but does not start containers
Solution
Step 1: Understand the purpose of docker compose up
This command reads the docker-compose.yml file and starts all the containers defined in it.
Step 2: Compare with other commands
docker compose down stops and removes containers, so it is not the same as up.
Final Answer:
Starts all the containers defined in the docker-compose file -> Option C
Quick Check:
docker compose up = start containers [OK]
Hint: Remember: 'up' means start containers [OK]
Common Mistakes:
Confusing 'up' with 'down'
Thinking 'up' deletes files
Assuming 'up' only builds images
2. Which of the following is the correct command to stop and remove all containers started by docker compose?
easy
A. docker compose stop
B. docker compose down
C. docker compose remove
D. docker compose kill
Solution
Step 1: Identify the command to stop and remove containers
docker compose down stops and removes containers, networks, and default volumes created by up.
Step 2: Differentiate from other commands
docker compose stop only stops containers but does not remove them. remove is not a valid command here.
Final Answer:
docker compose down -> Option B
Quick Check:
down = stop and remove [OK]
Hint: Use 'down' to stop and clean containers [OK]
Common Mistakes:
Using 'stop' which doesn't remove containers
Trying non-existent commands like 'remove'
Confusing 'kill' with 'down'
3. Given this sequence of commands: docker compose up -d docker compose down What will be the state of the containers after these commands?
medium
A. Containers are removed and not running
B. Containers are stopped but still exist
C. Containers are running in detached mode
D. Containers are paused
Solution
Step 1: Analyze docker compose up -d
This starts containers in detached mode (running in background).
Step 2: Analyze docker compose down
This stops and removes all containers started by up.
Final Answer:
Containers are removed and not running -> Option A
Quick Check:
up -d starts, down removes [OK]
Hint: 'down' removes containers even if started detached [OK]
Common Mistakes:
Thinking containers remain stopped but not removed
Confusing paused with stopped
Assuming detached means containers stay after down
4. You ran docker compose down but your containers are still running. What is the most likely reason?
medium
A. You ran docker compose down in a different directory than the compose file
B. You forgot to add -d flag
C. The containers were started with docker run instead
D. Docker daemon is not running
Solution
Step 1: Understand how docker compose down works
This command affects containers defined in the compose file in the current directory.
Step 2: Identify why containers remain running
If run in a different directory, it won't find or stop those containers.
Final Answer:
You ran docker compose down in a different directory than the compose file -> Option A
Quick Check:
Correct directory needed for down [OK]
Hint: Run commands where your compose file is located [OK]
Common Mistakes:
Thinking -d flag affects stopping containers
Assuming down stops containers from docker run
Not checking Docker daemon status
5. You want to restart your multi-container app with fresh containers, removing old volumes and networks. Which command should you run?
hard
A. docker compose up --build
B. docker compose stop && docker compose start
C. docker compose restart
D. docker compose down -v && docker compose up -d
Solution
Step 1: Understand the need to remove old volumes and networks
Removing volumes requires the -v flag with docker compose down.
Step 2: Combine commands to remove and start fresh
docker compose down -v removes containers, networks, and volumes; docker compose up -d starts fresh containers in detached mode.
Final Answer:
docker compose down -v && docker compose up -d -> Option D
Quick Check:
Use down -v to remove volumes before up [OK]
Hint: Use 'down -v' to remove volumes before 'up' [OK]
Common Mistakes:
Using 'restart' which doesn't remove volumes
Using 'stop' and 'start' which keep old volumes
Assuming '--build' removes volumes
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.