Skip to content

Instantly share code, notes, and snippets.

@phillijw
Created August 6, 2020 19:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phillijw/d8466359efd41e75425c70d138eaecb7 to your computer and use it in GitHub Desktop.
Save phillijw/d8466359efd41e75425c70d138eaecb7 to your computer and use it in GitHub Desktop.
Docker health checks for services
```
healthcheck:
test: ["CMD-SHELL", "[ $$(expr $$(date +%s) - $$(stat -c '%Y' \"/app/health_check\")) -lt 60 ] || exit 1"
interval: 60s
timeout: 5s
retries: 3
```
This checks the last modified date of a file `/app/health_check` which the service should update
whenever it wants to mark itself as "alive". This command will check that it was updated in the
last 60 seconds and exit with a zero code. If its more than 60 seconds, it will exit with 1.
The command is actually this:
`[ $(expr $(date +%s) - $(stat -c '%Y' "/app/health_check")) -lt 60 ] || exit 1`
but the `$` and `"` are escaped.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment