Skip to content

Instantly share code, notes, and snippets.

@purcell
Last active March 6, 2018 22:54
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save purcell/4d765dfd1b64ac06e41bf04c9db8e542 to your computer and use it in GitHub Desktop.
Save purcell/4d765dfd1b64ac06e41bf04c9db8e542 to your computer and use it in GitHub Desktop.
Run a command against a PostgreSQL DB installed and started on demand using Docker
#!/bin/bash -e
image=mdillon/postgis:9.6-alpine
container_name=my-app-postgresql
if [ -z "$1" ]; then
echo "Run command with a dockerised PostgreSQL DB.
usage: $(basename "$0") command
The various PG* environment variables are set so that if command uses
them - as postgresql's tools and most libpq-based programs do - then
they will automatically use this DB." >&2
exit 1
fi
export PGPASSWORD=bananapancakes
export PGPORT=15432
export PGUSER=postgres
export PGHOST=localhost
trace() { echo "$*" >&1; }
trace "Starting Dockerised DB"
if docker container inspect $container_name >/dev/null 2>&1; then
trace "* Using existing container"
docker container start $container_name >/dev/null
else
trace "* Creating fresh container"
docker run --name $container_name -p $PGPORT:5432 -e POSTGRES_PASSWORD=$PGPASSWORD -d $image >/dev/null
fi
cleanup() {
trace "Stopping Dockerised DB"
docker container stop "$container_name" >/dev/null;
}
trap cleanup EXIT
check_count=0
while ! psql -c 'select 1' >/dev/null 2>&1; do
check_count=$(( check_count + 1 ))
if [[ $check_count -eq 10 ]]; then
trace "Timed out waiting for port to be opened."
exit 1
fi
trace "* Waiting for DB to be available"
sleep 1
done
"$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment