commit 21ac76d801982cfc76006ee9c8fb2ff2901ee615 Author: Mauro Torrez Date: Sun Nov 22 18:06:09 2020 -0300 first commit diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..5c901c4 --- /dev/null +++ b/.drone.yml @@ -0,0 +1,36 @@ +--- +kind: pipeline +name: tags + +steps: + - name: docker + image: plugins/docker + settings: + repo: eumau/postgres + username: + from_secret: dockerhub_username + password: + from_secret: dockerhub_password + tags: + - ${DRONE_TAG} + build_args: + - PG_TAG=${DRONE_TAG} + +trigger: + event: + - tag + +# --- +# kind: pipeline +# name: default + +# steps: +# - name: docker +# image: plugins/docker +# settings: +# repo: eumau/postgres +# auto_tag: true +# username: +# from_secret: dockerhub_username +# password: +# from_secret: dockerhub_password diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..adac7f9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*~ +\#* +.#* diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..eea1b1a --- /dev/null +++ b/Dockerfile @@ -0,0 +1,9 @@ +ARG PG_TAG=latest +FROM postgres:${PG_TAG} +LABEL description="PostgreSQL with non-superuser db user." + +COPY custom-entrypoint.sh /usr/local/bin/ +ENTRYPOINT ["custom-entrypoint.sh"] +STOPSIGNAL SIGINT +EXPOSE 5432 +CMD ["postgres"] diff --git a/custom-entrypoint.sh b/custom-entrypoint.sh new file mode 100755 index 0000000..ce9e72b --- /dev/null +++ b/custom-entrypoint.sh @@ -0,0 +1,113 @@ +#!/usr/bin/env bash +set -Eeo pipefail + +# This script extends the original entrypoint with support for adding +# a non-superuser owner to the $POSTGRES_DB database + +# source vendor entrypoint +. /usr/local/bin/docker-entrypoint.sh + +# create initial database +# uses environment variables for input: POSTGRES_DB +docker_setup_db() { + if [ "$POSTGRES_DB_USER" != 'postgres' ]; then + if [ -n "$POSTGRES_DB_PASSWORD" ]; then + POSTGRES_DB= docker_process_sql --dbname postgres --set user="$POSTGRES_DB_USER" --set password="$POSTGRES_DB_PASSWORD" <<-'EOSQL' + CREATE USER :"user" WITH PASSWORD :'password' ; + EOSQL + echo + else + POSTGRES_DB= docker_process_sql --dbname postgres --set user="$POSTGRES_DB_USER" <<-'EOSQL' + CREATE USER :"user" ; + EOSQL + echo + fi + if [ "$POSTGRES_DB" != 'postgres' ]; then + POSTGRES_DB= docker_process_sql --dbname postgres --set db="$POSTGRES_DB" --set user="$POSTGRES_DB_USER" <<-'EOSQL' + CREATE DATABASE :"db" WITH OWNER = :"user" ; + EOSQL + echo + fi + else + if [ "$POSTGRES_DB" != 'postgres' ]; then + POSTGRES_DB= docker_process_sql --dbname postgres --set db="$POSTGRES_DB" <<-'EOSQL' + CREATE DATABASE :"db" ; + EOSQL + echo + fi + fi +} + +# Loads various settings that are used elsewhere in the script +# This should be called before any other functions +docker_setup_env() { + file_env 'POSTGRES_PASSWORD' + + file_env 'POSTGRES_USER' 'postgres' + file_env 'POSTGRES_DB' "$POSTGRES_USER" + file_env 'POSTGRES_INITDB_ARGS' + # default authentication method is md5 + : "${POSTGRES_HOST_AUTH_METHOD:=md5}" + + file_env 'POSTGRES_DB_USER' "$POSTGRES_USER" + file_env 'POSTGRES_DB_PASSWORD' + + declare -g DATABASE_ALREADY_EXISTS + # look specifically for PG_VERSION, as it is expected in the DB dir + if [ -s "$PGDATA/PG_VERSION" ]; then + DATABASE_ALREADY_EXISTS='true' + fi +} + +_main() { + # if first arg looks like a flag, assume we want to run postgres server + if [ "${1:0:1}" = '-' ]; then + set -- postgres "$@" + fi + + if [ "$1" = 'postgres' ] && ! _pg_want_help "$@"; then + docker_setup_env + # setup data directories and permissions (when run as root) + docker_create_db_directories + if [ "$(id -u)" = '0' ]; then + # then restart script as postgres user + exec gosu postgres "$BASH_SOURCE" "$@" + fi + + # only run initialization on an empty data directory + if [ -z "$DATABASE_ALREADY_EXISTS" ]; then + docker_verify_minimum_env + + # check dir permissions to reduce likelihood of half-initialized database + ls /docker-entrypoint-initdb.d/ > /dev/null + + docker_init_database_dir + pg_setup_hba_conf + + # PGPASSWORD is required for psql when authentication is required for 'local' connections via pg_hba.conf and is otherwise harmless + # e.g. when '--auth=md5' or '--auth-local=md5' is used in POSTGRES_INITDB_ARGS + export PGPASSWORD="${PGPASSWORD:-$POSTGRES_PASSWORD}" + docker_temp_server_start "$@" + + docker_setup_db + docker_process_init_files /docker-entrypoint-initdb.d/* + + docker_temp_server_stop + unset PGPASSWORD + + echo + echo 'PostgreSQL init process complete; ready for start up.' + echo + else + echo + echo 'PostgreSQL Database directory appears to contain a database; Skipping initialization' + echo + fi + fi + + exec "$@" +} + +if ! _is_sourced; then + _main "$@" +fi