Sub topic 1
Why services fail when they start in the wrong order
Prerequisites
This chapter assumes you can do the following:
- Run a container and know what an image is.
- Read and write a
compose.yamlfile with services, images, and environment variables. - Run
docker compose upanddocker compose down. - Use basic shell commands.
The startup order problem
A Compose project usually has services that depend on each other. A web application needs a database. A worker needs a message queue. If the application starts and tries to use the database before the database can answer, the first connection fails.
Here is a typical project with that problem:
services:
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: example
web:
image: myapp:latestNothing in this file says that web needs db. Compose starts both services at about the same time, so web can run before PostgreSQL is listening.
What the failure looks like
The exact message depends on your application and its database driver. These are the usual symptoms:
- The application logs
connection refusedorcould not connect to server. - The application exits at once, and Compose shows the container as
Exited. - The application works on a second run, because the database was already running and initialized.
- The failure is intermittent. It depends on how fast your machine pulls images and starts containers.
The last symptom makes the problem hard to find. A project can work on your laptop and fail in a continuous integration (CI) job, where cold caches and slower disks change the timing.
Why a database needs time
Starting a container means the main process has been launched. It does not mean the service inside is ready for requests. On the first run, the postgres image has to do several things before it accepts connections:
- Initialize the data directory.
- Run any setup scripts.
- Restart the server in its final mode.
- Begin listening for connections.
This takes seconds, and the container is already in the running state during all of it. Compose sees a running process. It cannot tell whether that process is ready.
Two separate questions
The problem splits into two questions. They have different answers in Compose.
| Question | Meaning | Tool in Compose |
|---|---|---|
| In what order should containers start? | Order of creation and start | `depends_on` |
| When is a service ready for work? | The service answers real requests | Health checks |
The rest of this tutorial covers both. You first control start order with depends_on. You then add a health check so Compose can tell when a dependency is ready, and combine the two so that web waits for a healthy db.
Quick check on your own project
Before you change any configuration, confirm that you have this problem. Start the project from a clean state and read the logs:
docker compose down -v
docker compose upThe -v flag removes the named volumes, so the database initializes from scratch as it would on a new machine. Look in the output for connection errors from web that appear before the database prints that it is ready to accept connections. If you see them, the next chapters apply to you.
Exercise
A project starts db and web together. web fails with "connection refused" on a fresh machine, but works when you run docker compose up a second time. Why does the second run succeed?
Show answer
On the first run, the database container is running but PostgreSQL is still initializing its data directory, so it refuses connections. On the second run, the data already exists and the database starts listening quickly, or it is already running, so web connects successfully. The cause is timing, not a configuration difference.