Docker Compose Tip #3: Service dependencies with health checks
“Connection refused” errors? The app starts before the database is ready. Here’s the fix. What doesn’t work This only waits for the container to start, not for it to be ready: services: app: depends_on: - db # Container starts, but database isn't ready yet What actually works Add health checks: services: db: image: postgres:16 environment: POSTGRES_PASSWORD: secret healthcheck: test: ["CMD-SHELL", "pg_isready -U postgres"] interval: 10s timeout: 5s retries: 5 start_period: 10s app: image: myapp depends_on: db: condition: service_healthy # Now it actually waits for the database Common health checks PostgreSQL: ...