Skip to content

Commit 685e8bf

Browse files
committed
feat(compose): add info on healthchecks and depends_on conditions
1 parent 49a3a7b commit 685e8bf

File tree

1 file changed

+14
-2
lines changed
  • docs/docs/11_deploy_to_render/06_run_everything_docker_compose

1 file changed

+14
-2
lines changed

docs/docs/11_deploy_to_render/06_run_everything_docker_compose/README.md

+14-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ services:
1212
ports:
1313
- "5000:80"
1414
depends_on:
15-
- db
15+
db:
16+
condition: service_healthy
1617
env_file:
1718
- ./.env
1819
volumes:
@@ -24,6 +25,10 @@ services:
2425
- POSTGRES_DB=myapp
2526
volumes:
2627
- postgres_data:/var/lib/postgresql/data
28+
healthcheck:
29+
test: pg_isready -d $${POSTGRES_DB} -U postgres
30+
interval: 2s
31+
retries: 10
2732
volumes:
2833
postgres_data:
2934
```
@@ -48,6 +53,12 @@ DATABASE_URL=postgresql://postgres:password@db/myapp
4853
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.
4954
:::
5055

56+
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.
57+
58+
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.
59+
60+
Adding this means the `web` service won't start until the `db` service is ready to respond to requests.
61+
5162
## Named volumes in Docker Compose
5263

5364
You'll notice that our `docker-compose.yml` file has these lines:
@@ -113,4 +124,5 @@ Note you must be in the folder that contains your `docker-compose.yml` file in o
113124
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.
114125
:::
115126

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

0 commit comments

Comments
 (0)