Heuristic:AFK surf Open agent Health Gated Depends On Ordering
| Knowledge Sources | |
|---|---|
| Domains | Deployment, Reliability, Docker |
| Last Updated | 2026-10-25 00:00 GMT |
Overview
Order the self-hosting stack with health-gated depends_on conditions: postgres + redis (healthy) → app-init (completed successfully) → app, so migrations run exactly once before the server accepts traffic.
Description
The Open-Agent `docker-compose.yml` uses Compose v2 `depends_on` with explicit `condition`s rather than plain `depends_on` lists. The data services (`postgres`, `redis`) declare healthchecks; `app-init` (the one-shot migration job, `restart: 'no'`) waits for both to be `service_healthy`; the long-running `app` additionally waits for `app-init` to reach `service_completed_successfully`. This guarantees the app never starts against an unmigrated database.
Usage
Apply this heuristic when structuring a multi-service Docker Compose stack that includes a one-shot migration/init job and a long-running app. Use healthcheck + `condition: service_healthy` for data deps and `condition: service_completed_successfully` for the init job.
The Insight (Rule of Thumb)
- Action: Give every data service a `healthcheck`, then chain: `app-init` `depends_on: [postgres: service_healthy, redis: service_healthy]` with `restart: 'no'`; `app` `depends_on: [postgres: service_healthy, redis: service_healthy, app-init: service_completed_successfully]`.
- Value: Conditions: `service_healthy` for long-lived deps, `service_completed_successfully` for the init job.
- Trade-off: Slower cold start (app waits for healthchecks + init) — but correctness over speed.
- Compatibility: Requires Compose v2 (`docker compose` plugin); the v1 standalone `docker-compose` does not support condition syntax.
Reasoning
Without health gates, `app` can start before Postgres is ready to accept connections (the container is "up" but still booting), causing connection-refused loops. Without the `app-init` completion gate, `app` can start before migrations finish, serving against a half-migrated schema. The `restart: 'no'` on `app-init` ensures the migration job runs exactly once; the `service_completed_successfully` condition makes `app` wait for it.
Code Evidence
Data-service healthchecks from `.docker/docker-compose.yml:10-14,21-25`:
postgres:
healthcheck:
test: ['CMD-SHELL', 'pg_isready -U postgres']
interval: 5s
timeout: 5s
retries: 5
redis:
healthcheck:
test: ['CMD', 'redis-cli', 'ping']
interval: 5s
timeout: 3s
retries: 5
Init job depends on healthy data services, runs once from `.docker/docker-compose.yml:37-42`:
app-init:
command: yarn predeploy
depends_on:
postgres:
condition: service_healthy
redis:
condition: service_healthy
restart: 'no' # only run once for initialization
App waits for init completion from `.docker/docker-compose.yml:54-60`:
app:
depends_on:
postgres:
condition: service_healthy
redis:
condition: service_healthy
app-init:
condition: service_completed_successfully
restart: unless-stopped