Skip to content

Instantly share code, notes, and snippets.

@CzechJiri
Last active June 24, 2016 20:55
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 CzechJiri/29c9862393c6a9d4435a2bfd50cf546d to your computer and use it in GitHub Desktop.
Save CzechJiri/29c9862393c6a9d4435a2bfd50cf546d to your computer and use it in GitHub Desktop.
running application and DB using docker-compose.yml is tricky, because app will not wait for DB to be fully up and read to accept connections (docker-compose.yml only deals with dependencies). This function can be part of application entrypoint script, it waits up to 30 seconds for postgres to start. It uses pg_isready (works even without passwo…
function pg_ready()
{
waitTime=30 # keep trying for x seconds
n=0
until [ $n -ge $waitTime ]; do
# please note pg_isready does not care about username/dbname
# you can add correct values if you want to avoid errors in PG log
pg_isready --host=$DB_HOST --port=$DB_PORT && break
n=$[$n+1]
sleep 1;
done
}
if pg_ready ; then
echo "INFO: WHIIIIII let start the app."
else
echo "ERROR: Database $$DB_HOST:$DB_PORT not found."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment