Guest User

Untitled

a guest
Mar 15th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. #!/bin/bash -e
  2.  
  3. image=mdillon/postgis:9.6-alpine
  4. container_name=my-app-postgresql
  5.  
  6. if [ -z "$1" ]; then
  7. echo "Run command with a dockerised PostgreSQL DB.
  8.  
  9. usage: $(basename "$0") command
  10.  
  11. The various PG* environment variables are set so that if command uses
  12. them - as postgresql's tools and most libpq-based programs do - then
  13. they will automatically use this DB." >&2
  14. exit 1
  15. fi
  16.  
  17. export PGPASSWORD=bananapancakes
  18. export PGPORT=15432
  19. export PGUSER=postgres
  20. export PGHOST=localhost
  21.  
  22. trace() { echo "$*" >&1; }
  23.  
  24. trace "Starting Dockerised DB"
  25. if docker container inspect $container_name >/dev/null 2>&1; then
  26. trace "* Using existing container"
  27. docker container start $container_name >/dev/null
  28. else
  29. trace "* Creating fresh container"
  30. docker run --name $container_name -p $PGPORT:5432 -e POSTGRES_PASSWORD=$PGPASSWORD -d $image >/dev/null
  31. fi
  32.  
  33. cleanup() {
  34. trace "Stopping Dockerised DB"
  35. docker container stop "$container_name" >/dev/null;
  36. }
  37.  
  38. trap cleanup EXIT
  39.  
  40. check_count=0
  41. while ! psql -c 'select 1' >/dev/null 2>&1; do
  42. check_count=$(( check_count + 1 ))
  43. if [[ $check_count -eq 10 ]]; then
  44. trace "Timed out waiting for port to be opened."
  45. exit 1
  46. fi
  47. trace "* Waiting for DB to be available"
  48. sleep 1
  49. done
  50.  
  51. "$@"
Add Comment
Please, Sign In to add comment