Skip to content

Instantly share code, notes, and snippets.

@k0staa
Created May 6, 2020 07:07
Show Gist options
  • Select an option

  • Save k0staa/233c28b92b54b52c02e064313b0ebbfe to your computer and use it in GitHub Desktop.

Select an option

Save k0staa/233c28b92b54b52c02e064313b0ebbfe to your computer and use it in GitHub Desktop.
wait-for script using /health endpoint
#!/bin/sh
#Usage in docker-compose.yml:
# scripts:
# build: scripts
# depends_on:
# - "app-tomcat"
# command: sh -c '/wait-for.sh app-tomcat:8080 -- sh /entrypoint.sh'
#
#entrypoint.sh is run after script connect to application /health endpoint
set -e
host="$1"
shift
cmd="$@"
until $(curl --output /dev/null --silent --head --fail "$host"); do
printf '.'
sleep 1
done
# First wait for service to start...
response=$(curl $host)
until [ "$response" = "200" ]; do
response=$(curl --write-out %{http_code} --silent --output /dev/null "$host/health")
>&2 echo "Service is unavailable - sleeping"
sleep 1
done
# Wait for service health status to turn UP
health="$(curl -fsSL "$host/health" | jq -r '.status' )"
until [ "$health" = "UP" ]; do
health="$(curl -fsSL "$host/health" | jq -r '.status' )"
>&2 echo "Service is unavailable - sleeping"
sleep 1
done
>&2 echo "Service is up"
exec $cmd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment