Collection of Docker Compose
healthcheck
examples
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 30s
timeout: 30s
retries: 3
redis:
image: redis
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 1s
timeout: 3s
retries: 30
healthcheck:
test: curl --silent http://localhost:9200 >/dev/null; if [[ $$? == 52 ]]; then echo 0; else echo 1; fi
interval: 30s
timeout: 10s
retries: 5
healthcheck:
test: timeout 5s bash -c ':> /dev/tcp/127.0.0.1/9000' || exit 1
start_period: 5s
interval: 10s
timeout: 5s
retries: 2
Source [1]
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"]
interval: 30s
timeout: 20s
retries: 3
Note: mongosh
may need to be used rather than mongo
, depending on which image you're using.
healthcheck:
test:
[
"CMD",
"mongo",
"--quiet",
"127.0.0.1/test",
"--eval",
"'quit(db.runCommand({ ping: 1 }).ok ? 0 : 2)'",
]
interval: 10s
timeout: 10s
retries: 5
start_period: 40s
To wait for a Docker container until its health state is healthy
before executing a command inside the container, use the following function in a shell script:
readonly DOCKER_CONTAINER='my_postgres'
wait_until_container_healthy() {
until
[ "$(docker inspect --format "{{.State.Health.Status}}" "$DOCKER_CONTAINER")" = 'healthy' ]
do
# Log container status
local container_status
container_status="$(docker inspect --format "{{.State.Health.Status}}" "$DOCKER_CONTAINER")"
echo "Waiting for container $DOCKER_CONTAINER to be 'healthy', current status is '$container_status'. Sleeping for five seconds..."
sleep 5
done
}
This snippet is an improved version of this StackOverflow answer.
docker/awesome-compose
- Official Docker Compose examples (includes healthchecks)docker-library/healthcheck
- Examples for Dockerfiles