Sub topic 3
Started is not ready
This chapter assumes you can read a compose.yaml file, run docker compose up and docker compose down, and know what an image and a container are. It covers one idea that the rest of the tutorial builds on: a container that has started is not necessarily a service that is ready.
Two different states
Docker tracks a container's process. A container counts as started once its main process has been launched. That tells you nothing about whether the program inside can do useful work yet.
A service is ready when it can accept and answer real requests. PostgreSQL shows the gap well. When the postgres:16 container starts, the server may still be:
- initializing the data directory on a first run,
- running init scripts,
- replaying its write-ahead log after an unclean shutdown,
- restarting once after initialization is finished.
During this time the container is running, but a client that connects gets "connection refused" or "the database system is starting up".
What depends_on guarantees
The short syntax of depends_on controls the order in which containers are created and started.
services:
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: example
web:
image: myapp:latest
depends_on:
- dbCompose creates and starts db first, then web. It does not wait for PostgreSQL to accept connections. As soon as the db process has launched, Compose moves on to web.
If web tries to connect to the database on startup and fails, you will see a crash or a restart loop. The problem is a race between two processes, not a wrong order.
Why the race is intermittent
This failure is easy to misdiagnose because it does not always happen. The outcome depends on timing:
| Situation | Typical result |
|---|---|
| First run, empty data volume, database initializes | `web` often fails |
| Later run, existing data volume | `web` often succeeds |
| Slow machine or busy CI runner | `web` fails more often |
| Fast machine with a small database | `web` may succeed by luck |
A setup that works on your laptop and fails in CI usually has this race. Adding sleep 10 before the application starts hides it on one machine and breaks on another, so it is not a fix.
What you need instead
Closing the gap takes two pieces:
- A way for the dependency to report when it is actually ready. Compose calls this a health check, and the next chapters show how to write one.
- A way for the dependent service to wait for that report. This is the
conditionfield in the long syntax ofdepends_on.
A third piece stays useful even after both are in place. Databases restart, networks drop, and dependencies can become unavailable long after startup. The application should retry its connections, which a later chapter covers.
Seeing the problem yourself
You can watch the race with the short syntax shown above. Start the stack and follow the logs of both services:
docker compose up -d
docker compose logs --follow web dbIf web connects too early, its log shows a connection error before the db log shows that the database system is ready to accept connections. The order of those two lines tells you whether web won or lost the race.
Exercise
A teammate's stack uses depends_on: [db] for the web service. It works locally but fails in CI with "connection refused" from web. Which statement explains this, and why is adding sleep 10 a poor fix?
Show answer
Short-syntax depends_on only orders container start. It does not wait for PostgreSQL to accept connections, so web races against database initialization. CI runs are usually on clean volumes and slower hardware, which makes the race fail more often. A fixed sleep only guesses at the timing, so it still breaks when initialization takes longer. A health check with a service_healthy condition waits for the real ready signal instead.