Dockerized Postgres Pro Standard 10.4 with external data, config and logs
4.4K
This postgres install has 2 locales - en_US.UTF-8 and ru_RU.UTF-8. Bug fixed from official repo - while obtaining keys. Installed packages postgrespro-std-10-server postgrespro-std-10-client postgrespro-std-10-libs postgrespro-std-10-contrib
Dockerfile
FROM ubuntu:18.04
ENV DEBIAN_FRONTEND noninteractive #ARGUMENTS FOR BUILD WITH DEFAULT VALUES ARG LANGUAGE=en_US ARG LANGUAGE2=ru_RU ARG ENCODING=UTF-8 ARG LANG_ENCODING=$LANGUAGE.$ENCODING ARG LANG_ENCODING2=$LANGUAGE2.$ENCODING LABEL maintainer="[email protected]" #take from argument on build, but set default value if not specified
ENV PG_MAJOR 10 ENV PGDATA /var/lib/pgpro/std-10/data ENV PGLOGS /var/lib/pgpro/std-10/logs ENV PATH=/opt/pgpro/std-10/bin:$PATH
RUN echo $MBSTD_LOGLEVEL
#install locale
RUN apt-get update && apt-get install -y --no-install-recommends apt-utils sudo locales wget
&& localedef -i $LANGUAGE -c -f $ENCODING -A /usr/share/locale/locale.alias $LANG_ENCODING
&& localedef -i $LANGUAGE2 -c -f $ENCODING -A /usr/share/locale/locale.alias $LANG_ENCODING2
&& update-locale LANG=$LANG_ENCODING
ENV LC_ALL=$LANG_ENCODING ENV LANGUAGE=$LANG_ENCODING ENV LANG=$LANG_ENCODING
RUN groupadd -r postgres --gid=999 && useradd -r -g postgres --uid=999 postgres
#RUN mkdir -p "$PGDATA" && chown -R postgres:postgres "$PGDATA" && chmod 777 "$PGDATA" RUN mkdir -p "$PGLOGS" && chown -R postgres:postgres "$PGLOGS" && chmod 777 "$PGLOGS"
VOLUME $PGDATA VOLUME $PGLOGS
RUN set -x
&& apt-get update
&& {
which gpg \
|| apt-get install -y --no-install-recommends gnupg2 \
|| apt-get install -y --no-install-recommends gnupg \
; } \
&& { \
gpg --version | grep -q '^gpg (GnuPG) 1\.' \
|| apt-get install -y --no-install-recommends dirmngr \
; } \
&& rm -rf /var/lib/apt/lists/*
ENV GOSU_VERSION 1.7
RUN set -x
&& apt-get update && apt-get install -y --no-install-recommends ca-certificates
&& wget -O /usr/local/bin/gosu "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$(dpkg --print-architecture)"
&& wget -O /usr/local/bin/gosu.asc "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$(dpkg --print-architecture).asc"
&& export GNUPGHOME="$(mktemp -d)" \
&& for server in $(shuf -e ha.pool.sks-keyservers.net
hkp://p80.pool.sks-keyservers.net:80
keyserver.ubuntu.com
hkp://keyserver.ubuntu.com:80
pgp.mit.edu) ; do
gpg --keyserver "$server" --recv-keys B42F6819007F00F88E364FD4036A9C25BF357DD4 && break || : ;
done
&& gpg --batch --verify /usr/local/bin/gosu.asc /usr/local/bin/gosu
&& rm -r "$GNUPGHOME" /usr/local/bin/gosu.asc
&& chmod +x /usr/local/bin/gosu
RUN apt-get update -y #RUN apt-get install -y wget gnupg2 || apt-get install -y gnupg RUN wget -O - http://repo.postgrespro.ru/keys/GPG-KEY-POSTGRESPRO | apt-key add - RUN echo deb http://repo.postgrespro.ru/pgpro-archive/pgpro-10.4.1/ubuntu bionic main > /etc/apt/sources.list.d/postgrespro-std.list RUN apt-get update -y RUN apt-get install -y --no-install-recommends postgrespro-std-10-server postgrespro-std-10-client postgrespro-std-10-libs postgrespro-std-10-contrib RUN rm -rf /var/lib/apt/lists/* RUN ls -la /var/lib/pgpro/std-10/data
COPY docker-entrypoint.sh /usr/local/bin/ RUN chmod +x /usr/local/bin/docker-entrypoint.sh RUN ln -s usr/local/bin/docker-entrypoint.sh / # backwards compat ENTRYPOINT ["docker-entrypoint.sh"]
EXPOSE 5432 CMD ["postgres"]
entrypoint.sh #!/usr/bin/env bash env set -Eeo pipefail
file_env() { local var="$1" local fileVar="${var}_FILE" local def="${2:-}" if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then echo >&2 "error: both $var and $fileVar are set (but are exclusive)" exit 1 fi local val="$def" if [ "${!var:-}" ]; then val="${!var}" elif [ "${!fileVar:-}" ]; then val="$(< "${!fileVar}")" fi export "$var"="$val" unset "$fileVar" }
if [ "${1:0:1}" = '-' ]; then set -- postgres "$@" fi
--userif [ "$1" = 'postgres' ] && [ "$(id -u)" = '0' ]; then mkdir -p "$PGDATA" chown -R postgres "$PGDATA" chmod 700 "$PGDATA"
mkdir -p /var/run/postgresql
chown -R postgres /var/run/postgresql
chmod 775 /var/run/postgresql
# Create the transaction log directory before initdb is run (below) so the directory is owned by the correct user
if [ "$POSTGRES_INITDB_WALDIR" ]; then
mkdir -p "$POSTGRES_INITDB_WALDIR"
chown -R postgres "$POSTGRES_INITDB_WALDIR"
chmod 700 "$POSTGRES_INITDB_WALDIR"
fi
exec gosu postgres "$BASH_SOURCE" "$@"
fi
if [ "$1" = 'postgres' ]; then mkdir -p "$PGDATA" chown -R "$(id -u)" "$PGDATA" 2>/dev/null || : chmod 700 "$PGDATA" 2>/dev/null || :
# look specifically for PG_VERSION, as it is expected in the DB dir
if [ ! -s "$PGDATA/PG_VERSION" ]; then
# "initdb" is particular about the current user existing in "/etc/passwd", so we use "nss_wrapper" to fake that if necessary
# see https://github.com/docker-library/postgres/pull/253, https://github.com/docker-library/postgres/issues/359, https://cwrap.org/nss_wrapper.html
if ! getent passwd "$(id -u)" &> /dev/null && [ -e /usr/lib/libnss_wrapper.so ]; then
export LD_PRELOAD='/usr/lib/libnss_wrapper.so'
export NSS_WRAPPER_PASSWD="$(mktemp)"
export NSS_WRAPPER_GROUP="$(mktemp)"
echo "postgres:x:$(id -u):$(id -g):PostgreSQL:$PGDATA:/bin/false" > "$NSS_WRAPPER_PASSWD"
echo "postgres:x:$(id -g):" > "$NSS_WRAPPER_GROUP"
fi
file_env 'POSTGRES_INITDB_ARGS'
if [ "$POSTGRES_INITDB_WALDIR" ]; then
export POSTGRES_INITDB_ARGS="$POSTGRES_INITDB_ARGS --waldir $POSTGRES_INITDB_WALDIR"
fi
eval "initdb --username=postgres $POSTGRES_INITDB_ARGS"
# unset/cleanup "nss_wrapper" bits
if [ "${LD_PRELOAD:-}" = '/usr/lib/libnss_wrapper.so' ]; then
rm -f "$NSS_WRAPPER_PASSWD" "$NSS_WRAPPER_GROUP"
unset LD_PRELOAD NSS_WRAPPER_PASSWD NSS_WRAPPER_GROUP
fi
# check password first so we can output the warning before postgres
# messes it up
file_env 'POSTGRES_PASSWORD'
if [ "$POSTGRES_PASSWORD" ]; then
pass="PASSWORD '$POSTGRES_PASSWORD'"
authMethod=md5
else
# The - option suppresses leading tabs but *not* spaces. :)
cat >&2 <<-'EOWARN'
****************************************************
WARNING: No password has been set for the database.
This will allow anyone with access to the
Postgres port to access your database. In
Docker's default configuration, this is
effectively any other container on the same
system.
Use "-e POSTGRES_PASSWORD=password" to set
it in "docker run".
****************************************************
EOWARN
pass=
authMethod=trust
fi
{
echo
echo "host all all all $authMethod"
} >> "$PGDATA/pg_hba.conf"
# internal start of server in order to allow set-up using psql-client
# does not listen on external TCP/IP and waits until start finishes
PGUSER="${PGUSER:-postgres}" \
pg_ctl -D "$PGDATA" \
-o "-c listen_addresses=''" \
-w start \
-l "$PGLOGS"/postgres_pro_10.log \
-o "-p 5432"
file_env 'POSTGRES_USER' 'postgres'
file_env 'POSTGRES_DB' "$POSTGRES_USER"
psql=( psql -v ON_ERROR_STOP=1 )
if [ "$POSTGRES_DB" != 'postgres' ]; then
"${psql[@]}" --username postgres <<-EOSQL
CREATE DATABASE "$POSTGRES_DB" ;
EOSQL
echo
fi
if [ "$POSTGRES_USER" = 'postgres' ]; then
op='ALTER'
else
op='CREATE'
fi
"${psql[@]}" --username postgres <<-EOSQL
$op USER "$POSTGRES_USER" WITH SUPERUSER $pass ;
EOSQL
echo
psql+=( --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" )
echo
for f in /docker-entrypoint-initdb.d/*; do
case "$f" in
*.sh)
# https://github.com/docker-library/postgres/issues/450#issuecomment-393167936
# https://github.com/docker-library/postgres/pull/452
if [ -x "$f" ]; then
echo "$0: running $f"
"$f"
else
echo "$0: sourcing $f"
. "$f"
fi
;;
*.sql) echo "$0: running $f"; "${psql[@]}" -f "$f"; echo ;;
*.sql.gz) echo "$0: running $f"; gunzip -c "$f" | "${psql[@]}"; echo ;;
*) echo "$0: ignoring $f" ;;
esac
echo
done
PGUSER="${PGUSER:-postgres}" \
pg_ctl -D "$PGDATA" -m fast -w stop -l "$PGLOGS"/postgres_pro_10.log -o "-p 5432"
echo
echo 'PostgreSQL init process complete; ready for start up.'
echo
fi
fi
exec "$@"
Using in docker-compose
version: '3' services: postgres: image: mobsted/postgres_pro_10:latest command: postgres -c config_file=/etc/postgresql.conf container_name: mbst_postgres10 volumes: - .pgpass:/root/.pgpass - ./database/dumps/:/dumps - ./database/postgres-pro-10:/var/lib/pgpro/std-10/data - ./logs/postgres_pro_10:/logs - ./conf/postgresql/postgresql.conf:/etc/postgresql.conf environment: PGDATA: /var/lib/pgpro/std-10/data PGUSER: postgres MBSTD_PGDATA: ${MBSTD_PGDATA} ports: - "5432:5432" networks: - backend pgbouncer: image: mobsted/pgbouncer:latest container_name: mbst_pgbouncer volumes: - ./conf/pgbouncer/pgbouncer.ini:/etc/pgbouncer/pgbouncer.ini - ./logs/pgbouncer/pgbouncer.log:/logs/pgbouncer.log ports: - "6432:6432" depends_on: - postgres networks: - backend - frontend networks: backend: frontend:
Content type
Image
Digest
Size
130.9 MB
Last updated
about 8 years ago
docker pull mobsted/postgres_pro_10