Skip to content

Instantly share code, notes, and snippets.

@Sybrand
Forked from stephenhillier/Dockerfile
Created May 7, 2020 18:55
Show Gist options
  • Save Sybrand/02ce6cf0f3333f1001d8d413e1c32636 to your computer and use it in GitHub Desktop.
Save Sybrand/02ce6cf0f3333f1001d8d413e1c32636 to your computer and use it in GitHub Desktop.
PostGIS Patroni Dockerfile
FROM registry.hub.docker.com/library/postgres:10
# Patroni install from https://github.com/zalando/patroni
# OpenShift config from https://github.com/BCDevOps/platform-services
# PostGIS install adapted from https://github.com/appropriate/docker-postgis
ARG PGHOME=/home/postgres
ENV POSTGIS_MAJOR 2.5
ENV POSTGIS_VERSION 2.5.4+dfsg-1.pgdg90+1
RUN export DEBIAN_FRONTEND=noninteractive \
&& echo 'APT::Install-Recommends "0";\nAPT::Install-Suggests "0";' > /etc/apt/apt.conf.d/01norecommend \
&& apt-get update -y \
&& apt-get upgrade -y \
&& apt-cache depends patroni | sed -n -e 's/.* Depends: \(python3-.\+\)$/\1/p' \
| grep -Ev '^python3-(sphinx|etcd|consul|kazoo|kubernetes)' \
| xargs apt-get install -y gettext curl jq locales git python3-pip python3-wheel pgloader \
&& apt-cache showpkg postgresql-$PG_MAJOR-postgis-$POSTGIS_MAJOR \
&& apt-get install -y --no-install-recommends \
postgresql-$PG_MAJOR-postgis-$POSTGIS_MAJOR=$POSTGIS_VERSION \
postgresql-$PG_MAJOR-postgis-$POSTGIS_MAJOR-scripts=$POSTGIS_VERSION \
## Make sure we have a en_US.UTF-8 locale available
&& localedef -i en_US -c -f UTF-8 -A /usr/share/locale/locale.alias en_US.UTF-8 \
&& pip3 install setuptools \
&& pip3 install 'git+https://github.com/zalando/patroni.git#egg=patroni[kubernetes]' \
&& mkdir -p $PGHOME \
&& sed -i "s|/var/lib/postgresql.*|$PGHOME:/bin/bash|" /etc/passwd \
# Set permissions for OpenShift
&& chmod 775 $PGHOME \
&& chmod 664 /etc/passwd \
&& mkdir -p $PGHOME/pgdata/pgroot \
&& chgrp -R 0 $PGHOME \
&& chown -R postgres $PGHOME \
&& chmod -R 775 $PGHOME \
# Clean up
&& apt-get remove -y git python3-pip python3-wheel \
&& apt-get autoremove -y \
&& apt-get clean -y \
&& rm -rf /var/lib/apt/lists/* /root/.cache
COPY contrib/root /
VOLUME /home/postgres/pgdata
EXPOSE 5432 8008
ENV LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8
USER postgres
WORKDIR /home/postgres
CMD ["/bin/bash", "/usr/bin/entrypoint.sh"]
#!/usr/bin/env bash
# contrib/root/usr/share/scripts/patroni/post_init.sh
set -Eeu
if [[ (! -z "$APP_USER") && (! -z "$APP_PASSWORD") && (! -z "$APP_DATABASE")]]; then
echo "Creating user ${APP_USER}"
psql "$1" -w -c "create user ${APP_USER} WITH LOGIN ENCRYPTED PASSWORD '${APP_PASSWORD}'"
echo "Creating database ${APP_DATABASE}"
psql "$1" -w -c "CREATE DATABASE ${APP_DATABASE} OWNER ${APP_USER} ENCODING '${APP_DB_ENCODING:-UTF8}' LC_COLLATE = '${APP_DB_LC_COLLATE:-en_US.UTF-8}' LC_CTYPE = '${APP_DB_LC_CTYPE:-en_US.UTF-8}'"
echo "Creating PostGIS extension"
psql $APP_DATABASE -w -c "CREATE EXTENSION IF NOT EXISTS POSTGIS"
psql $APP_DATABASE -w -c "CREATE EXTENSION IF NOT EXISTS pg_trgm"
else
echo "Skipping user creation"
echo "Skipping database creation"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment