Why up -d alone isn’t enough
A plain docker compose up -d returns control the moment the kernel spawns the container process. At that point the
database is still replaying its journal, the web app is running migrations, or it is about to crash on a config typo —
yet up -d already exited with code 0. An update script assumes everything worked and moves on.
--wait (which implies detached mode -d) changes that: Compose waits until every service in the stack reaches
healthy. If a container turns unhealthy, exits with an error, or exceeds --wait-timeout, the command returns a
non-zero exit code. That’s what automatic rollback in a deploy script relies on (Deploy without SSH).
If a service defines no healthcheck in docker-compose.yaml or in the image’s Dockerfile, --wait waits only for
running and does not check whether the app inside is ready.
Defining a healthcheck in Compose
services:
app:
image: app:1.4
depends_on:
db:
condition: service_healthy
healthcheck:
test: ["CMD", "wget", "-q", "--spider", "http://127.0.0.1:8080/healthz"]
interval: 5m
timeout: 5s
retries: 3
start_period: 30s
start_interval: 2sThe app starts only after the database passes its healthcheck — without condition: service_healthy, Compose waits only for the container to start (Docker).
db:
image: postgres:16
healthcheck:
test: ["CMD-SHELL", "pg_isready -U app -d app"]
interval: 5m
timeout: 5s
retries: 3
start_period: 60s
start_interval: 2sDuring start_period (60 s), failed probes don't count toward retries, while the first passing probe marks the container healthy immediately. With start_interval: 2s, probes run every 2 seconds during startup so up --wait doesn't stall; once started, the interval drops to the infrequent interval and avoids extra load.
Pick a test command from tools already inside the image: minimal Alpine images usually lack curl but ship wget,
while databases and proxies include their own probe binaries. Use 127.0.0.1 instead of localhost so probes don’t
hit IPv6 when the service listens only on IPv4.
| Service | test command |
|---|---|
Alpine web service (wget) |
["CMD", "wget", "-q", "--spider", "http://127.0.0.1:8080/healthz"] |
| PostgreSQL | ["CMD-SHELL", "pg_isready -U app -d app"] |
| MariaDB | ["CMD", "healthcheck.sh", "--connect", "--innodb_initialized"] |
Traefik (with ping: in traefik.yml block by block) |
["CMD", "traefik", "healthcheck", "--ping"] |
start_interval requires Docker Engine 25 and Compose v2.20.2. On older versions, omit it and use a shorter interval
(such as 10s–30s).
healthcheck itself never restarts a container: after retries consecutive failures, Docker only marks it unhealthy.
That status is what up --wait, docker ps, and monitoring (Beszel) read.
Starting with a timeout
In scripts, pair --wait with --wait-timeout in seconds:
docker compose up -d --wait --wait-timeout 180
Without a timeout, the command can hang for the entire start_period if a container gets stuck in starting. With the
limit set, the script gets a non-zero exit code after 180 seconds and can roll back or send an alert.
If it doesn’t work
docker compose up -d --wait hangs or times out even though the service inside works
- Cause
- The command in
healthcheck.testreturns an error: the image has nocurlorwget, the service listens on a different port, orlocalhostresolves to IPv6 (::1) while the service listens only on127.0.0.1. - Fix
- Inspect the last probe output via
docker inspect --format '{{json .State.Health}}' <container>and run thetestcommand manually withdocker compose exec.
The container turns unhealthy on every start, then runs fine
- Cause
- The app or database takes longer to boot than
start_period, so failed probes hitretriesbefore startup finishes. - Fix
- Raise
start_period(e.g. to60sfor a database running migrations) and setstart_interval: 2sso Compose catches readiness right after the service comes up.
The container is unhealthy, yet Docker doesn't restart it
- Cause
healthcheckonly changes the container status, whilerestart: unless-stoppedrestarts only exited processes.- Fix
- Set up an alert on
unhealthyin monitoring (Beszel) and check the container logs to see why it hung.
up -d --wait exits with 0 right away, then the service crashes a few seconds later
- Cause
- The service has no
healthcheckindocker-compose.yamlor in the image itself: for such containers,--waittreatsrunningas ready. - Fix
- Add a
healthchecksection todocker-compose.yaml.
How to check
Start the stack with --wait, then check the exit code and container status:
docker compose up -d --wait --wait-timeout 180
echo $?
# 0
docker ps --format 'table {{.Names}}\t{{.Status}}'
# app Up 12 seconds (healthy)
If a container turns unhealthy, docker inspect prints the last five test runs along with their output:
docker inspect --format '{{json .State.Health}}' app