Skip to content

Instantly share code, notes, and snippets.

@yogeek
Created January 5, 2018 18:34
Show Gist options
  • Save yogeek/4ced62b9c5a68d0603b81d004dc6f38c to your computer and use it in GitHub Desktop.
Save yogeek/4ced62b9c5a68d0603b81d004dc6f38c to your computer and use it in GitHub Desktop.
docker-compose generic readiness condition
# 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
# 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