GitXplorerGitXplorer
p

docker-compose-healthcheck

public
747 stars
59 forks
6 issues

Commits

List of commits on branch master.
Verified
1458811cbc24c002b2585f628e4af4e69b641f58

Merge pull request #14 from peter-evans/remove-args

ppeter-evans committed 4 years ago
Unverified
ef4de97241963a1672c633f2ce26bfb25d8e0647

Remove pg_isready args

ppeter-evans committed 4 years ago
Verified
48de357fdb5dd9bd54d440861755081d955dfffe

Merge pull request #11 from peter-evans/compose-spec

ppeter-evans committed 4 years ago
Unverified
8eb9cad908a349cdd3a4625d9a81648fca6a2808

Update readme

ppeter-evans committed 4 years ago
Unverified
dc645465e9ede68d1ed638898abdfaf875c8faef

Note version as optional

ppeter-evans committed 4 years ago
Unverified
2c5c94cff237e2af83fcb3d9f991c0b6ca422819

Set checkout to v2

ppeter-evans committed 4 years ago

README

The README file for this repository.

docker-compose-healthcheck The blog of Peter Evans: How to Wait for Container X Before Starting Y

Actions Status

The healthcheck property was originally introduced in the 2.1 Compose file format and is now part of the Compose Specification 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 and 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.

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.

Services that depend on PostgreSQL can then be configured with the depends_on parameter as follows:

depends_on:
  postgres-database:
    condition: service_healthy

Waiting for PostgreSQL before starting Kong

In this complete example docker-compose waits for the PostgreSQL service to be "healthy" before starting Kong, 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

Test by querying Kong's admin endpoint:

curl http://localhost:8001/

License

MIT License - see the LICENSE file for details