📖 Scenario: You are setting up a simple multi-service application using Docker Compose. One service depends on another to start first. For example, a web app needs the database to be ready before it starts.
🎯 Goal: You will create a docker-compose.yml file that defines two services: db and web. You will use depends_on to ensure the web service starts only after the db service.
📋 What You'll Learn
Create a docker-compose.yml file with two services: db and web.
Set the db service to use the official postgres image.
Set the web service to use the official nginx image.
Use depends_on in the web service to depend on db.
Print the content of the docker-compose.yml file to verify the setup.
💡 Why This Matters
🌍 Real World
Many applications use multiple services like databases and web servers. Docker Compose helps start them in the right order.
💼 Career
Understanding service dependencies is important for DevOps roles to ensure reliable application deployment and startup.
Progress0 / 4 steps
1
Create the basic docker-compose.yml structure
Create a file named docker-compose.yml and add the version '3.8' and an empty services section.
Docker
Hint
Start with the version line and then add the services key with no services yet.
2
Add the db service using the postgres image
Under services, add a service named db that uses the image postgres.
Docker
Hint
Indent the db service under services and specify the image as postgres.
3
Add the web service using nginx and set depends_on
Under services, add a service named web that uses the image nginx and add depends_on with db as its dependency.
Docker
Hint
Indent the web service under services. Use a list under depends_on with db as the item.
4
Print the docker-compose.yml content
Print the entire content of the docker-compose.yml file exactly as created.
Docker
Hint
Use a single print statement with the exact content including indentation and new lines.
Practice
(1/5)
1. What is the main purpose of depends_on in a Docker Compose file?
easy
A. To specify the image version for services
B. To set environment variables for services
C. To define network settings for services
D. To control the order in which services start
Solution
Step 1: Understand the role of depends_on
depends_on is used to specify which services must start before others in Docker Compose.
Step 2: Differentiate from other settings
It does not set environment variables, network settings, or image versions; it only controls start order.
Final Answer:
To control the order in which services start -> Option D
Quick Check:
Service start order = depends_on [OK]
Hint: Depends_on controls start order, not readiness or config [OK]
Common Mistakes:
Thinking depends_on waits for full service readiness
Confusing depends_on with environment or network settings
Assuming depends_on sets image versions
2. Which of the following is the correct syntax to make service web depend on service db in a Docker Compose file?
easy
A. web:
depends:
db
B. web:
depends:
- db
C. web:
depends_on:
- db
D. web:
depends_on: db
Solution
Step 1: Recall correct depends_on syntax
The correct syntax uses depends_on as a key with a list of service names under it.
Step 2: Check each option
web:
depends_on:
- db correctly uses depends_on: followed by a list with - db. Others have wrong keys or missing list format.
Final Answer:
web:
depends_on:
- db -> Option C
Quick Check:
depends_on needs a list under it [OK]
Hint: depends_on requires a list of services under it [OK]
Common Mistakes:
Using 'depends' instead of 'depends_on'
Not using a list for dependent services
Writing depends_on as a single string
3. Given this Docker Compose snippet:
services:
db:
image: postgres
web:
image: nginx
depends_on:
- db
What happens when you run docker-compose up?
medium
A. The web service starts only after the db service has fully initialized
B. The db service starts before the web service, but web may start before db is ready
C. Both services start simultaneously without any order
D. The web service starts first, then the db service
Solution
Step 1: Understand depends_on behavior
depends_on ensures the db service starts before web, but does not wait for db to be ready.
Step 2: Analyze the startup sequence
db container starts first, then web starts immediately after, even if db is not fully ready.
Final Answer:
The db service starts before the web service, but web may start before db is ready -> Option B
Quick Check:
depends_on controls start order, not readiness [OK]
Hint: depends_on orders start but doesn't wait for readiness [OK]
Common Mistakes:
Assuming depends_on waits for full service readiness
Thinking services start simultaneously
Believing web starts before db
4. You wrote this Docker Compose snippet:
services:
web:
image: nginx
depends_on: db
db:
image: postgres
But it fails to start with an error. What is the problem?
medium
A. depends_on should be a list, not a single string
B. The db service must be defined before web
C. Image names are incorrect
D. depends_on cannot be used with nginx
Solution
Step 1: Check depends_on syntax
depends_on expects a list of services, but here it is given as a single string.
Step 2: Identify correct format
It should be:
depends_on:
- db
not depends_on: db.
Final Answer:
depends_on should be a list, not a single string -> Option A
Quick Check:
depends_on requires a list [OK]
Hint: depends_on must list services with dash (-) [OK]
Common Mistakes:
Using a string instead of a list for depends_on
Assuming service order depends on definition order
Blaming image names for depends_on errors
5. You have three services: db, cache, and web. The web service needs both db and cache to start first. Also, cache must start after db. How should you write the depends_on section for web and cache to enforce this order?
hard
A. web:
depends_on:
- db
- cache
cache:
depends_on:
- db
B. web:
depends_on:
- cache
cache:
depends_on:
- web
- db
C. web:
depends_on:
- db
cache:
depends_on:
- web
D. web:
depends_on:
- db
cache:
depends_on:
- web
- db
Solution
Step 1: Define cache dependency on db
cache must start after db, so cache depends_on db.
Step 2: Define web dependency on both db and cache
web needs both db and cache started first, so web depends_on both db and cache.
Final Answer:
web:
depends_on:
- db
- cache
cache:
depends_on:
- db -> Option A
Quick Check:
Order: db -> cache -> web [OK]
Hint: Chain depends_on to enforce multi-step start order [OK]
Common Mistakes:
Making cache depend on web incorrectly
Not listing both dependencies for web
Assuming depends_on waits for readiness
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.