Bird
Raised Fist0
Dockerdevops~20 mins

Depends_on for service ordering in Docker - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Depends_on Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
What does depends_on control in Docker Compose?

In a docker-compose.yml file, what is the main purpose of the depends_on key under a service?

AIt ensures the dependent service is fully ready before starting the current service.
BIt controls the order in which services start but does not wait for them to be ready.
CIt automatically restarts the dependent service if it crashes.
DIt links the network ports of the dependent service to the current service.
Attempts:
2 left
💡 Hint

Think about whether depends_on waits for the service to be fully ready or just starts it first.

💻 Command Output
intermediate
1:30remaining
Output of docker-compose with depends_on

Given this docker-compose.yml snippet:

version: '3.8'
services:
  db:
    image: postgres
  web:
    image: nginx
    depends_on:
      - db

What will happen when you run docker-compose up?

ABoth services start simultaneously without any order.
BThe <code>web</code> service waits until <code>db</code> is fully ready before starting.
CThe <code>db</code> service starts first, then <code>web</code> starts immediately after, without waiting for <code>db</code> readiness.
DThe <code>web</code> service will fail to start because <code>depends_on</code> is invalid syntax.
Attempts:
2 left
💡 Hint

Remember what depends_on controls in Docker Compose.

❓ Configuration
advanced
2:00remaining
Correct depends_on syntax for multiple dependencies

Which depends_on syntax correctly specifies that a service depends on db and cache services?

Adepends_on: [db, cache]
B
depends_on:
  db: true
  cache: true
C
depends_on:
  db
  cache
D
depends_on:
  - db
  - cache
Attempts:
2 left
💡 Hint

Check the correct YAML list syntax for depends_on.

❓ Troubleshoot
advanced
2:00remaining
Why does a service start before its dependency is ready?

You have a web service that depends_on a db service. However, web tries to connect to db before it is ready, causing errors. Why does this happen?

A<code>depends_on</code> only controls start order, not readiness or health checks.
B<code>depends_on</code> is deprecated and ignored by Docker Compose.
C<code>db</code> service is not exposed on the network, so <code>web</code> cannot connect.
D<code>web</code> service must be restarted manually after <code>db</code> is ready.
Attempts:
2 left
💡 Hint

Think about what depends_on does and does not guarantee.

✅ Best Practice
expert
2:30remaining
How to ensure service readiness with depends_on in Docker Compose v3.8?

Docker Compose's depends_on does not wait for service readiness. What is the best way to ensure a service starts only after its dependency is fully ready?

AUse healthchecks in the dependency service and configure <code>depends_on</code> with condition: service_healthy.
BAdd a <code>restart: always</code> policy to the dependent service.
CManually add <code>sleep</code> commands in the dependent service's startup script.
DUse <code>links</code> instead of <code>depends_on</code> to enforce readiness.
Attempts:
2 left
💡 Hint

Check how Docker Compose can use healthchecks with depends_on conditions.

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

  1. Step 1: Understand the role of depends_on

    depends_on is used to specify which services must start before others in Docker Compose.
  2. Step 2: Differentiate from other settings

    It does not set environment variables, network settings, or image versions; it only controls start order.
  3. Final Answer:

    To control the order in which services start -> Option D
  4. 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

  1. Step 1: Recall correct depends_on syntax

    The correct syntax uses depends_on as a key with a list of service names under it.
  2. 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.
  3. Final Answer:

    web: depends_on: - db -> Option C
  4. 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

  1. Step 1: Understand depends_on behavior

    depends_on ensures the db service starts before web, but does not wait for db to be ready.
  2. Step 2: Analyze the startup sequence

    db container starts first, then web starts immediately after, even if db is not fully ready.
  3. Final Answer:

    The db service starts before the web service, but web may start before db is ready -> Option B
  4. 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

  1. Step 1: Check depends_on syntax

    depends_on expects a list of services, but here it is given as a single string.
  2. Step 2: Identify correct format

    It should be:
    depends_on:
      - db
    not depends_on: db.
  3. Final Answer:

    depends_on should be a list, not a single string -> Option A
  4. 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

  1. Step 1: Define cache dependency on db

    cache must start after db, so cache depends_on db.
  2. 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.
  3. Final Answer:

    web: depends_on: - db - cache cache: depends_on: - db -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard