Service Dependencies
100%

Docker Compose · Lesson 6

Service Dependencies

Learn to distinguish dependency startup order from a service that is ready to answer requests.

A helper that needs a web server can start in the right order and still make its first request too early. A health check is a small test run by Docker to observe whether a service can do something useful, such as answer an HTTP request.

Choosing a Readiness Check

In this separate example the check runs inside the web container. It requests the local web page. A successful request gives Docker evidence that the server is answering from that location.

services:
  web:
    image: nginx:alpine
    healthcheck:
      test: ["CMD", "wget", "-q", "-O", "/dev/null", "http://127.0.0.1/"]
      interval: 3s
      timeout: 2s
      retries: 5
  toolbox:
    image: busybox:1.36
    command: ["sleep", "3600"]
    depends_on:
      web:
        condition: service_healthy

Save this as compose.health.yaml in the same practice folder. The check runs at intervals and has a time limit per attempt. It checks a local response inside the web container.

Where does wget run for this health check?

Waiting for a Healthy Dependency

The service_healthy condition makes Compose wait for a successful health check before starting the dependent toolbox. Simple startup ordering can start a dependent as soon as the dependency is running. That can be earlier than its first successful request.

$ docker compose -p journey-health -f compose.health.yaml up -d
$ docker compose -p journey-health -f compose.health.yaml ps

Look for a healthy state on the web row and a running toolbox.

The web process started but its local request still fails. Which condition should delay starting the toolbox?

Compose status showing a healthy web dependency and the running toolbox

Handling a Later Failure

A Compose startup condition makes an initial ordering decision. A previously healthy service can still stop answering after startup. A client should handle a failed request and reconnect when the dependency becomes available. Use the service state and a direct request to check the present behavior.

A request fails an hour after both services started. Which evidence helps now?

Checking Two Points of View

A successful local health check shows what the web container can reach from inside itself. The toolbox still needs the project network and a usable service address. Check the health result and the request from the toolbox separately.

Health is green but one request from the toolbox fails. Which comparison helps locate the problem?

Lesson complete

You finished Service Dependencies

You can now use a check to reason about whether a dependency is ready.

  • Identify what a health check actually observes.

  • Choose a healthy dependency condition when startup order is insufficient.

  • Verify current behavior if a service fails later.

Keep your learning progress

Create a free account to save this lesson and continue learning on any device.

Create a free account
Back to Docker Compose