From 747ed5a2cccef0ad1bb9aae4207048c5b524d399 Mon Sep 17 00:00:00 2001 From: f Date: Tue, 24 Sep 2019 14:04:19 -0300 Subject: [PATCH] postgresql --- Dockerfile | 24 +++++++++++++++++++++++- monit.conf | 4 ++++ postgresql.sh | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 monit.conf create mode 100644 postgresql.sh diff --git a/Dockerfile b/Dockerfile index 59c5898..90609e8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,2 +1,24 @@ -FROM sutty/monit:latest +FROM sutty/daemonize:latest AS build MAINTAINER "f " + +RUN echo /home/builder/packages/home > /etc/apk/repositories +RUN apk add --no-cache daemonize + +FROM sutty/monit:latest + +ENV PGDATA /var/lib/postgresql/11/data +ENV LANG en_US.utf8 +ENV PGUSER sutty +ENV PGDB sutty +ENV PGVER 11 + +RUN apk add --no-cache postgresql postgresql-contrib + +COPY --from=build /usr/sbin/daemonize /usr/sbin/daemonize +COPY ./postgresql.sh /usr/local/bin/postgresql +COPY ./monit.conf /etc/monit.d/postgresql.conf + +RUN chmod 750 /usr/local/bin/postgresql + +EXPOSE 5432 +VOLUME $PGDATA diff --git a/monit.conf b/monit.conf new file mode 100644 index 0000000..d968884 --- /dev/null +++ b/monit.conf @@ -0,0 +1,4 @@ +check process postgresql with pidfile /tmp/postgresql.pid + start program = "/usr/local/bin/postgresql start" + stop program = "/usr/local/bin/postgresql stop" + if failed port 5432 protocol pgsql for 3 times within 5 cycles then restart diff --git a/postgresql.sh b/postgresql.sh new file mode 100644 index 0000000..fc292e1 --- /dev/null +++ b/postgresql.sh @@ -0,0 +1,33 @@ +#!/bin/sh +set -e + +case $1 in + stop) + /bin/su postgres -c "pg_ctl stop -D ${PGDATA}" ;; + *) + pid=/tmp/postgresql.pid + user=postgres + log=${PGDATA}/postgresql.log + + install -dm 2750 -o postgres -g postgres /run/postgresql + chown -R postgres:postgres /var/lib/postgresql + + if test ! -f ${PGDATA}/PG_VERSION ; then + su - postgres -c "/usr/bin/initdb --locale ${LANG} -E UTF8 -D ${PGDATA}" + su - postgres -c "/usr/bin/pg_ctl start --pgdata ${PGDATA}" + su - postgres -c "/usr/bin/createuser --login ${PGUSER}" + su - postgres -c "/usr/bin/createdb --owner ${PGUSER} ${PGDB}" + su - postgres -c "/usr/bin/pg_ctl stop --pgdata ${PGDATA}" + + echo "host ${PGDB} ${PGUSER} samenet trust" >> ${PGDATA}/pg_hba.conf + echo "host ${PGDB}_test ${PGUSER} samenet trust" >> ${PGDATA}/pg_hba.conf + + echo "listen_addresses = '*'" >> ${PGDATA}/postgresql.conf + echo "external_pid_file = '${pid}'" >> ${PGDATA}/postgresql.conf + fi + + rm -f ${pid} + daemonize -u ${user} -c ${PGDATA} -o ${log} -a -e ${log} \ + /usr/bin/postgres + ;; +esac