docker-entrypoint.sh 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #!/bin/bash
  2. set -e
  3. # usage: file_env VAR [DEFAULT]
  4. # ie: file_env 'XYZ_DB_PASSWORD' 'example'
  5. # (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of
  6. # "$XYZ_DB_PASSWORD" from a file, especially for Docker's secrets feature)
  7. file_env() {
  8. local var="$1"
  9. local fileVar="${var}_FILE"
  10. local def="${2:-}"
  11. if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
  12. echo >&2 "error: both $var and $fileVar are set (but are exclusive)"
  13. exit 1
  14. fi
  15. local val="$def"
  16. if [ "${!var:-}" ]; then
  17. val="${!var}"
  18. elif [ "${!fileVar:-}" ]; then
  19. val="$(< "${!fileVar}")"
  20. fi
  21. export "$var"="$val"
  22. unset "$fileVar"
  23. }
  24. if [ "${1:0:1}" = '-' ]; then
  25. set -- postgres "$@"
  26. fi
  27. # allow the container to be started with `--user`
  28. if [ "$1" = 'postgres' ] && [ "$(id -u)" = '0' ]; then
  29. mkdir -p "$PGDATA"
  30. chown -R postgres "$PGDATA"
  31. chmod 700 "$PGDATA"
  32. mkdir -p /var/run/postgresql
  33. chown -R postgres /var/run/postgresql
  34. chmod g+s /var/run/postgresql
  35. exec gosu postgres "$BASH_SOURCE" "$@"
  36. fi
  37. if [ "$1" = 'postgres' ]; then
  38. mkdir -p "$PGDATA"
  39. chown -R "$(id -u)" "$PGDATA" 2>/dev/null || :
  40. chmod 700 "$PGDATA" 2>/dev/null || :
  41. # look specifically for PG_VERSION, as it is expected in the DB dir
  42. if [ ! -s "$PGDATA/PG_VERSION" ]; then
  43. file_env 'POSTGRES_INITDB_ARGS'
  44. eval "initdb --username=postgres $POSTGRES_INITDB_ARGS"
  45. # check password first so we can output the warning before postgres
  46. # messes it up
  47. file_env 'POSTGRES_PASSWORD'
  48. if [ "$POSTGRES_PASSWORD" ]; then
  49. pass="PASSWORD '$POSTGRES_PASSWORD'"
  50. authMethod=md5
  51. else
  52. # The - option suppresses leading tabs but *not* spaces. :)
  53. cat >&2 <<-'EOWARN'
  54. ****************************************************
  55. WARNING: No password has been set for the database.
  56. This will allow anyone with access to the
  57. Postgres port to access your database. In
  58. Docker's default configuration, this is
  59. effectively any other container on the same
  60. system.
  61. Use "-e POSTGRES_PASSWORD=password" to set
  62. it in "docker run".
  63. ****************************************************
  64. EOWARN
  65. pass=
  66. authMethod=trust
  67. fi
  68. { echo; echo "host all all all $authMethod"; } | tee -a "$PGDATA/pg_hba.conf" > /dev/null
  69. # internal start of server in order to allow set-up using psql-client
  70. # does not listen on external TCP/IP and waits until start finishes
  71. PGUSER="${PGUSER:-postgres}" \
  72. pg_ctl -D "$PGDATA" \
  73. -o "-c listen_addresses='localhost'" \
  74. -w start
  75. file_env 'POSTGRES_USER' 'postgres'
  76. file_env 'POSTGRES_DB' "$POSTGRES_USER"
  77. psql=( psql -v ON_ERROR_STOP=1 )
  78. if [ "$POSTGRES_DB" != 'postgres' ]; then
  79. "${psql[@]}" --username postgres <<-EOSQL
  80. CREATE DATABASE "$POSTGRES_DB" ;
  81. EOSQL
  82. echo
  83. fi
  84. if [ "$POSTGRES_USER" = 'postgres' ]; then
  85. op='ALTER'
  86. else
  87. op='CREATE'
  88. fi
  89. "${psql[@]}" --username postgres <<-EOSQL
  90. $op USER "$POSTGRES_USER" WITH SUPERUSER $pass ;
  91. EOSQL
  92. echo
  93. psql+=( --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" )
  94. echo
  95. for f in /docker-entrypoint-initdb.d/*; do
  96. case "$f" in
  97. *.sh) echo "$0: running $f"; . "$f" ;;
  98. *.sql) echo "$0: running $f"; "${psql[@]}" -f "$f"; echo ;;
  99. *.sql.gz) echo "$0: running $f"; gunzip -c "$f" | "${psql[@]}"; echo ;;
  100. *) echo "$0: ignoring $f" ;;
  101. esac
  102. echo
  103. done
  104. PGUSER="${PGUSER:-postgres}" \
  105. pg_ctl -D "$PGDATA" -m fast -w stop
  106. echo
  107. echo 'PostgreSQL init process complete; ready for start up.'
  108. echo
  109. fi
  110. fi
  111. exec "$@"