first commit
All checks were successful
continuous-integration/drone/tag Build is passing

This commit is contained in:
Mauro Torrez 2020-11-22 18:06:09 -03:00
commit 21ac76d801
4 changed files with 161 additions and 0 deletions

36
.drone.yml Normal file
View File

@ -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

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
*~
\#*
.#*

9
Dockerfile Normal file
View File

@ -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"]

113
custom-entrypoint.sh Executable file
View File

@ -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