Skip to content

Commit

Permalink
feat(compose): add info on healthchecks and depends_on conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
jslvtr committed Jun 21, 2024
1 parent 49a3a7b commit 685e8bf
Showing 1 changed file with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ services:
ports:
- "5000:80"
depends_on:
- db
db:
condition: service_healthy
env_file:
- ./.env
volumes:
Expand All @@ -24,6 +25,10 @@ services:
- POSTGRES_DB=myapp
volumes:
- postgres_data:/var/lib/postgresql/data
healthcheck:
test: pg_isready -d $${POSTGRES_DB} -U postgres
interval: 2s
retries: 10
volumes:
postgres_data:
```
Expand All @@ -48,6 +53,12 @@ DATABASE_URL=postgresql://postgres:password@db/myapp
When Docker Compose runs, it creates a virtual network[^1] which allows you to connect to `db`, which connects to the running `db` service container.
:::

In the `docker-compose.yml` file above you can also see that the `web` service depends on the `db` service, with the condition that it is healthy. A service is deemed "healthy" when its healthcheck passes.

We've added a healthcheck to the `db` service which runs the `pg_isready`[^2] program using the supplied database and PostgreSQL user. This just tells us whether the PostgreSQL server is ready to respond to requests.

Adding this means the `web` service won't start until the `db` service is ready to respond to requests.

## Named volumes in Docker Compose

You'll notice that our `docker-compose.yml` file has these lines:
Expand Down Expand Up @@ -113,4 +124,5 @@ Note you must be in the folder that contains your `docker-compose.yml` file in o
Running `docker compose down` will **not** delete your named volumes. You need to use the `-v` flag for that. Deleting the named volumes deletes the data in them irreversibly.
:::

[^1]: [Networking in Compose (official docs)](https://docs.docker.com/compose/networking/)
[^1]: [Networking in Compose (official docs)](https://docs.docker.com/compose/networking/)
[^2]: [pg_isready (PostgreSQL documentation)](https://www.postgresql.org/docs/current/app-pg-isready.html)

0 comments on commit 685e8bf

Please sign in to comment.