gotchaMajorpending
Docker compose depends_on doesn't wait for service readiness
Viewed 0 times
depends_onservice_healthyhealthcheckwait-for-itpg_isreadycontainer readiness
dockerlinuxmacos
Error Messages
Problem
Docker Compose starts services in dependency order, but the application container crashes because the database isn't ready yet. depends_on only waits for the container to start, not for the service inside to be ready.
Solution
(1) depends_on with condition (Compose v2): depends_on: db: condition: service_healthy. (2) Add healthcheck to the depended service: healthcheck: test: ['CMD', 'pg_isready', '-U', 'postgres'] interval: 5s timeout: 5s retries: 5. (3) Application-level retry: build connection retry with backoff into your app — this is the most robust approach regardless of orchestration. (4) For MySQL: mysqladmin ping -h localhost. For Redis: redis-cli ping. (5) Don't use wait-for-it.sh or dockerize scripts if healthchecks are available — they're cleaner. (6) Compose v1 depends_on ignores conditions — upgrade to v2.
Why
Container 'started' means the process is running, not that the service is accepting connections. Database containers need time to initialize data directories, run migrations, and start listening on their port.
Revisions (0)
No revisions yet.