Created
January 5, 2018 18:34
-
-
Save yogeek/4ced62b9c5a68d0603b81d004dc6f38c to your computer and use it in GitHub Desktop.
docker-compose generic readiness condition
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Example : waiting for DB and Elasticsearch to be ready before launching a service | |
# The svc docker image must contain the start-when-ready.sh script | |
services: | |
svc: | |
image: svc | |
environment: | |
READINESS_CONDITION: '[ $$(curl --write-out %{http_code} --silent --output /dev/null http://elasticsearch:9200/_cat/health?h=st) = 200 ] && nc -w 1 -z $$DB_HOST $$DB_PORT 2>/dev/null' | |
READINESS_SLEEP: 5 | |
READINESS_TIMEOUT: 1000 | |
command: start-when-ready.sh |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Flag to exit if trying to use an uninitialised variable | |
set -u | |
# Flag to exit if any statement returns a non-true return value | |
set -e | |
# ------------------------ Wait for READINESS_CONDITION : begin | |
# Generic wait condition using READINESS_CONDITION, READINESS_TIMEOUT and READINESS_SLEEP env var given in docker-compose.yml | |
[[ ! -z READINESS_CONDITION ]] && echo "Waiting condition : ${READINESS_CONDITION}" | |
# Loop until READINESS_CONDITION is true or WAIT_TIMEOUT is reached | |
timeout ${READINESS_TIMEOUT:-10} bash -c "until ${READINESS_CONDITION:-true}; do echo ... waiting to be ready...; sleep ${READINESS_SLEEP:-5}; done" | |
# If READINESS_CONDITION times out, exit with an error message | |
[[ $? -eq 124 ]] && { echo "Timeout reached and waiting condition still false. Check READINESS_CONDITION or increase READINESS_TIMEOUT."; exit 1; } | |
# ------------------------ Wait for READINESS_CONDITION : end | |
echo "Service ready" | |
# Start the service... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment