Wait for a healthy Compose dependency.

Docker / Compose: The service_healthy dependency condition waits for the dependency’s health command to succeed.

Shell compose.yaml
services:
  probe:
    image: ubuntu:20.04
    command: ["sh", "-c", "touch /tmp/ready; sleep 300"]
    healthcheck:
      test: ["CMD", "test", "-f", "/tmp/ready"]
      interval: 1s
      timeout: 1s
      retries: 3
  app:
    image: ubuntu:20.04
    depends_on:
      probe:
        condition: service_healthy
    command: ["printf", "dependency ready\n"]

Expected output EXAMPLE

dependency ready

Details

Requires Docker Engine and Docker Compose (the docker compose command). Run in a disposable development environment with ubuntu:20.04 available locally. See the input setup for any starting containers or files. Save as compose.yaml and run docker compose run --rm app. Compose starts probe and waits for its real health check before running app. Run docker compose down afterward to stop this example project.

✓ Verified

Tested on Alpine Linux v3.24 · · Client 29.9.0; Engine 29.9.0 · Compose 5.6.0; ubuntu:20.04

Test inputs, results and environment

The exact published code ran in a dedicated Linux test container with prepared inputs. Dedicated nested Docker daemon in a privileged test helper with no host Docker socket. No application data or host directories are mounted into the example containers. The outer helper has networking disabled; the base image is preloaded. Dockerfile examples are built and run; Compose examples start real containers. Fixtures and output records are isolated from the application database. Testing covers this example and its stated checks, not every input, operating system, or runtime version. The Linux container shares its host's kernel; this is not a native Windows test.

Architecture / host kernel
x86_64 / 6.18.54.1-microsoft-standard-WSL2
Snippet identity / locale
root inside disposable Docker-in-Docker helper / C.UTF-8
Interpreter invocation
docker compose --progress quiet run --rm -T app
Container image ID
sha256:aa45302a315fcbba307d6749ccbbac83af2e6f9447d6d714fce5b7b1c92b1081
Code SHA-256
14ccc94b936795bdbb976bc1832e5d86b2f8ba7597b3e42db3f04748659e8c4c
Dependencies
Compose 5.6.0; ubuntu:20.04

Test input setup

These are the prepared inputs used by the test harness. See Details above for the prerequisites and how to run this example.

# No input files required.

Observed standard output

dependency ready

Exit code: 0. Standard error: empty. Validation: Exact stdout comparison. System-dependent values above are one recorded run.

Additional assertions that passed

Checks recorded by the test harness. Where Python assertions are shown, browser cases use Playwright and simulator cases may inspect pin transitions: result.stdout is captured output, work is the temporary directory, and Path is pathlib.Path. The harness also imports json and csv.

All commands returned zero; exact output matched.