Interview Q&A / Docker & Kubernetes

IntermediateQuick answer1 min readUpdated 3 Oct 2026

Does depends_on in Docker Compose wait until the database is ready?

SHORT ANSWER

By default no. depends_on only controls start order, not readiness. To wait for a healthy service, add a healthcheck and use depends_on with condition: service_healthy.

Explain it simply

Booking a taxi and the driver 'starting his car' are not the same as him reaching your gate. depends_on only guarantees the db container has been started before the api container, not that Postgres has finished initialising and accepts connections.

That gap causes the classic 'connection refused' crash on first boot. The fix is to give the database a healthcheck (for Postgres, pg_isready) and tell the api to wait for condition: service_healthy. Compose then holds the api back until the check passes. Other conditions are service_started (the default) and service_completed_successfully, useful for one-off migration jobs.

You should still make the app retry its database connection; databases can restart later, and no start-up ordering protects against that.

Example

yaml
services:
  db:
    image: postgres:16
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 5s
      retries: 5
  api:
    build: .
    depends_on:
      db:
        condition: service_healthy

What to say in the interview

"depends_on only orders container start, not readiness. I add a healthcheck to the database and use condition: service_healthy so the app waits, and I still add connection retries in the app."

Common mistakes

  • Assuming depends_on means the database is accepting connections.

#docker-compose#depends-on#healthcheck

Related questions

Practise more Docker & Kubernetes questions

Test your recall with short answers, then return to the detailed explanations when you need context.

Open Docker & Kubernetes quick answers →

Get new questions like this by email

One short email when something new and useful lands. No spam, unsubscribe in one tap.

By subscribing you agree to the Privacy Policy.