You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

docker-entrypoint.sh 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 775 /var/run/postgresql
  35. # Create the transaction log directory before initdb is run (below) so the directory is owned by the correct user
  36. if [ "$POSTGRES_INITDB_XLOGDIR" ]; then
  37. mkdir -p "$POSTGRES_INITDB_XLOGDIR"
  38. chown -R postgres "$POSTGRES_INITDB_XLOGDIR"
  39. chmod 700 "$POSTGRES_INITDB_XLOGDIR"
  40. fi
  41. exec gosu postgres "$BASH_SOURCE" "$@"
  42. fi
  43. if [ "$1" = 'postgres' ]; then
  44. mkdir -p "$PGDATA"
  45. chown -R "$(id -u)" "$PGDATA" 2>/dev/null || :
  46. chmod 700 "$PGDATA" 2>/dev/null || :
  47. # look specifically for PG_VERSION, as it is expected in the DB dir
  48. if [ ! -s "$PGDATA/PG_VERSION" ]; then
  49. file_env 'POSTGRES_INITDB_ARGS'
  50. if [ "$POSTGRES_INITDB_XLOGDIR" ]; then
  51. export POSTGRES_INITDB_ARGS="$POSTGRES_INITDB_ARGS --xlogdir $POSTGRES_INITDB_XLOGDIR"
  52. fi
  53. eval "initdb --username=postgres $POSTGRES_INITDB_ARGS"
  54. # check password first so we can output the warning before postgres
  55. # messes it up
  56. file_env 'POSTGRES_PASSWORD'
  57. if [ "$POSTGRES_PASSWORD" ]; then
  58. pass="PASSWORD '$POSTGRES_PASSWORD'"
  59. authMethod=md5
  60. else
  61. # The - option suppresses leading tabs but *not* spaces. :)
  62. cat >&2 <<-'EOWARN'
  63. ****************************************************
  64. WARNING: No password has been set for the database.
  65. This will allow anyone with access to the
  66. Postgres port to access your database. In
  67. Docker's default configuration, this is
  68. effectively any other container on the same
  69. system.
  70. Use "-e POSTGRES_PASSWORD=password" to set
  71. it in "docker run".
  72. ****************************************************
  73. EOWARN
  74. pass=
  75. authMethod=trust
  76. fi
  77. {
  78. echo
  79. echo "host all all all $authMethod"
  80. } >> "$PGDATA/pg_hba.conf"
  81. # internal start of server in order to allow set-up using psql-client
  82. # does not listen on external TCP/IP and waits until start finishes
  83. PGUSER="${PGUSER:-postgres}" \
  84. pg_ctl -D "$PGDATA" \
  85. -o "-c listen_addresses='localhost'" \
  86. -w start
  87. file_env 'POSTGRES_USER' 'postgres'
  88. file_env 'POSTGRES_DB' "$POSTGRES_USER"
  89. psql=( psql -v ON_ERROR_STOP=1 )
  90. if [ "$POSTGRES_DB" != 'postgres' ]; then
  91. "${psql[@]}" --username postgres <<-EOSQL
  92. CREATE DATABASE "$POSTGRES_DB" ;
  93. EOSQL
  94. echo
  95. fi
  96. if [ "$POSTGRES_USER" = 'postgres' ]; then
  97. op='ALTER'
  98. else
  99. op='CREATE'
  100. fi
  101. "${psql[@]}" --username postgres <<-EOSQL
  102. $op USER "$POSTGRES_USER" WITH SUPERUSER $pass ;
  103. EOSQL
  104. echo
  105. psql+=( --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" )
  106. echo
  107. for f in /docker-entrypoint-initdb.d/*; do
  108. case "$f" in
  109. *.sh) echo "$0: running $f"; . "$f" ;;
  110. *.sql) echo "$0: running $f"; "${psql[@]}" -f "$f"; echo ;;
  111. *.sql.gz) echo "$0: running $f"; gunzip -c "$f" | "${psql[@]}"; echo ;;
  112. *) echo "$0: ignoring $f" ;;
  113. esac
  114. echo
  115. done
  116. PGUSER="${PGUSER:-postgres}" \
  117. pg_ctl -D "$PGDATA" -m fast -w stop
  118. echo
  119. echo 'PostgreSQL init process complete; ready for start up.'
  120. echo
  121. fi
  122. fi
  123. exec "$@"