DEV Community

Silva Chijioke Michael
Silva Chijioke Michael

Posted on

"depends_on" vs "wait-for-it.sh" in Docker Compose

When working with multi-container setups (for example, an application and a database), it’s important to understand the difference between container startup order and service readiness.


depends_on

  • Ensures a container starts before another.
  • Example: Your app won’t start until the database container has been launched.
  • Limitation: It does not guarantee the database is ready to accept connections.

wait-for-it.sh

  • A lightweight shell script that makes one service wait until another is reachable (via TCP).
  • Example:
CMD ["wait-for-it", "db:5432", "--", "npm", "start"]
Enter fullscreen mode Exit fullscreen mode

This ensures the app starts only after Postgres is ready.


Visual Comparison

Without wait-for-it.sh:

App ----> tries DB   [DB not ready]   → fails
Enter fullscreen mode Exit fullscreen mode

With wait-for-it.sh:

App ----> waits ----> DB ready        → starts successfully
Enter fullscreen mode Exit fullscreen mode

Why prefer wait-for-it.sh (or similar scripts)?

  • Prevents race conditions such as applications crashing on startup.
  • Works across any service (databases, caches, APIs).
  • Simple, portable, and avoids complex retry logic in your application code.

Summary

  • Use depends_on for container startup order.
  • Use wait-for-it.sh (or healthchecks) for service readiness.

This small distinction saves time, prevents bugs, and ensures smoother container orchestration.

Top comments (0)