Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/peter-evans/docker-compose-healthcheck
How to wait for container X before starting Y using docker-compose healthcheck
https://github.com/peter-evans/docker-compose-healthcheck
docker docker-compose health-check healthcheck kong postgresql
Last synced: 1 day ago
JSON representation
How to wait for container X before starting Y using docker-compose healthcheck
- Host: GitHub
- URL: https://github.com/peter-evans/docker-compose-healthcheck
- Owner: peter-evans
- License: mit
- Created: 2017-03-02T08:41:55.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2022-02-15T04:24:42.000Z (almost 3 years ago)
- Last Synced: 2024-12-14T00:07:03.491Z (9 days ago)
- Topics: docker, docker-compose, health-check, healthcheck, kong, postgresql
- Homepage:
- Size: 602 KB
- Stars: 745
- Watchers: 12
- Forks: 59
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# docker-compose-healthcheck [](https://peterevans.dev/posts/how-to-wait-for-container-x-before-starting-y/)
[![Actions Status](https://github.com/peter-evans/docker-compose-healthcheck/workflows/docker-compose-healthcheck/badge.svg)](https://github.com/peter-evans/docker-compose-healthcheck/actions)The [healthcheck](https://docs.docker.com/compose/compose-file/compose-file-v2/#healthcheck) property was originally introduced in the 2.1 Compose file format and is now part of the [Compose Specification](https://github.com/compose-spec/compose-spec/blob/master/spec.md) used by recent versions of Docker Compose.
This allows a check to be configured in order to determine whether or not containers for a service are "healthy."## How can I wait for container X before starting Y?
This is a common problem and in earlier versions of docker-compose requires the use of additional tools and scripts such as [wait-for-it](https://github.com/vishnubob/wait-for-it) and [dockerize](https://github.com/jwilder/dockerize).
Using the `healthcheck` parameter the use of these additional tools and scripts is often no longer necessary.## Waiting for PostgreSQL to be "healthy"
A particularly common use case is a service that depends on a database, such as PostgreSQL.
We can configure docker-compose to wait for the PostgreSQL container to startup and be ready to accept requests before continuing.The following healthcheck has been configured to periodically check if PostgreSQL is ready using the `pg_isready` command. See the documentation for the `pg_isready` command [here](https://www.postgresql.org/docs/9.4/static/app-pg-isready.html).
```yml
healthcheck:
test: ["CMD-SHELL", "pg_isready"]
interval: 10s
timeout: 5s
retries: 5
```
If the check is successful the container will be marked as `healthy`. Until then it will remain in an `unhealthy` state.
For more details about the healthcheck parameters `interval`, `timeout` and `retries` see the documentation [here](https://docs.docker.com/engine/reference/builder/#healthcheck).Services that depend on PostgreSQL can then be configured with the `depends_on` parameter as follows:
```yml
depends_on:
postgres-database:
condition: service_healthy
```## Waiting for PostgreSQL before starting Kong
In [this complete example](docker-compose.yml) docker-compose waits for the PostgreSQL service to be "healthy" before starting [Kong](https://getkong.org/), an open-source API gateway. It also waits for an additional ephemeral container to complete Kong's database migration process.
Test it out with:
```
docker-compose up -d
```
Wait until all services are running:![Demo](/demo.gif?raw=true)
Test by querying Kong's admin endpoint:
```
curl http://localhost:8001/
```## License
MIT License - see the [LICENSE](LICENSE) file for details