|
| 1 | +# vim:set ft=dockerfile: |
| 2 | +FROM debian:stretch-slim |
| 3 | + |
| 4 | +RUN set -ex; \ |
| 5 | + if ! command -v gpg > /dev/null; then \ |
| 6 | + apt-get update; \ |
| 7 | + apt-get install -y --no-install-recommends \ |
| 8 | + gnupg \ |
| 9 | + dirmngr \ |
| 10 | + ; \ |
| 11 | + rm -rf /var/lib/apt/lists/*; \ |
| 12 | + fi |
| 13 | + |
| 14 | +# explicitly set user/group IDs |
| 15 | +RUN groupadd -r postgres --gid=999 && useradd -r -g postgres --uid=999 postgres |
| 16 | + |
| 17 | +# grab gosu for easy step-down from root |
| 18 | +ENV GOSU_VERSION 1.10 |
| 19 | +RUN set -x \ |
| 20 | + && apt-get update && apt-get install -y --no-install-recommends ca-certificates wget && rm -rf /var/lib/apt/lists/* \ |
| 21 | + && wget -O /usr/local/bin/gosu "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$(dpkg --print-architecture)" \ |
| 22 | + && wget -O /usr/local/bin/gosu.asc "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$(dpkg --print-architecture).asc" \ |
| 23 | + && export GNUPGHOME="$(mktemp -d)" \ |
| 24 | + && gpg --keyserver ha.pool.sks-keyservers.net --recv-keys B42F6819007F00F88E364FD4036A9C25BF357DD4 \ |
| 25 | + && gpg --batch --verify /usr/local/bin/gosu.asc /usr/local/bin/gosu \ |
| 26 | + && rm -rf "$GNUPGHOME" /usr/local/bin/gosu.asc \ |
| 27 | + && chmod +x /usr/local/bin/gosu \ |
| 28 | + && gosu nobody true \ |
| 29 | + && apt-get purge -y --auto-remove ca-certificates wget |
| 30 | + |
| 31 | +# make the "en_US.UTF-8" locale so postgres will be utf-8 enabled by default |
| 32 | +RUN set -eux; \ |
| 33 | + if [ -f /etc/dpkg/dpkg.cfg.d/docker ]; then \ |
| 34 | +# if this file exists, we're likely in "debian:xxx-slim", and locales are thus being excluded so we need to remove that exclusion (since we need locales) |
| 35 | + grep -q '/usr/share/locale' /etc/dpkg/dpkg.cfg.d/docker; \ |
| 36 | + sed -ri '/\/usr\/share\/locale/d' /etc/dpkg/dpkg.cfg.d/docker; \ |
| 37 | + ! grep -q '/usr/share/locale' /etc/dpkg/dpkg.cfg.d/docker; \ |
| 38 | + fi; \ |
| 39 | + apt-get update; apt-get install -y locales; rm -rf /var/lib/apt/lists/*; \ |
| 40 | + localedef -i en_US -c -f UTF-8 -A /usr/share/locale/locale.alias en_US.UTF-8 |
| 41 | +ENV LANG en_US.utf8 |
| 42 | + |
| 43 | +# install "nss_wrapper" in case we need to fake "/etc/passwd" and "/etc/group" (especially for OpenShift) |
| 44 | +# https://github.com/docker-library/postgres/issues/359 |
| 45 | +# https://cwrap.org/nss_wrapper.html |
| 46 | +RUN set -eux; \ |
| 47 | + apt-get update; \ |
| 48 | + apt-get install -y --no-install-recommends libnss-wrapper; \ |
| 49 | + rm -rf /var/lib/apt/lists/* |
| 50 | + |
| 51 | +RUN mkdir /docker-entrypoint-initdb.d |
| 52 | + |
| 53 | +RUN set -ex; \ |
| 54 | +# pub 4096R/ACCC4CF8 2011-10-13 [expires: 2019-07-02] |
| 55 | +# Key fingerprint = B97B 0AFC AA1A 47F0 44F2 44A0 7FCC 7D46 ACCC 4CF8 |
| 56 | +# uid PostgreSQL Debian Repository |
| 57 | + key='B97B0AFCAA1A47F044F244A07FCC7D46ACCC4CF8'; \ |
| 58 | + export GNUPGHOME="$(mktemp -d)"; \ |
| 59 | + gpg --keyserver ha.pool.sks-keyservers.net --recv-keys "$key"; \ |
| 60 | + gpg --export "$key" > /etc/apt/trusted.gpg.d/postgres.gpg; \ |
| 61 | + rm -rf "$GNUPGHOME"; \ |
| 62 | + apt-key list |
| 63 | + |
| 64 | +ENV PG_MAJOR 11 |
| 65 | +ENV PG_VERSION 11~beta1-2.pgdg90+1 |
| 66 | + |
| 67 | +RUN set -ex; \ |
| 68 | + \ |
| 69 | + dpkgArch="$(dpkg --print-architecture)"; \ |
| 70 | + case "$dpkgArch" in \ |
| 71 | + amd64|i386|ppc64el) \ |
| 72 | +# arches officialy built by upstream |
| 73 | + echo "deb http://apt.postgresql.org/pub/repos/apt/ stretch-pgdg main $PG_MAJOR" > /etc/apt/sources.list.d/pgdg.list; \ |
| 74 | + apt-get update; \ |
| 75 | + ;; \ |
| 76 | + *) \ |
| 77 | +# we're on an architecture upstream doesn't officially build for |
| 78 | +# let's build binaries from their published source packages |
| 79 | + echo "deb-src http://apt.postgresql.org/pub/repos/apt/ stretch-pgdg main $PG_MAJOR" > /etc/apt/sources.list.d/pgdg.list; \ |
| 80 | + \ |
| 81 | + tempDir="$(mktemp -d)"; \ |
| 82 | + cd "$tempDir"; \ |
| 83 | + \ |
| 84 | + savedAptMark="$(apt-mark showmanual)"; \ |
| 85 | + \ |
| 86 | +# build .deb files from upstream's source packages (which are verified by apt-get) |
| 87 | + apt-get update; \ |
| 88 | + apt-get build-dep -y \ |
| 89 | + postgresql-common pgdg-keyring \ |
| 90 | + "postgresql-$PG_MAJOR=$PG_VERSION" \ |
| 91 | + ; \ |
| 92 | + DEB_BUILD_OPTIONS="nocheck parallel=$(nproc)" \ |
| 93 | + apt-get source --compile \ |
| 94 | + postgresql-common pgdg-keyring \ |
| 95 | + "postgresql-$PG_MAJOR=$PG_VERSION" \ |
| 96 | + ; \ |
| 97 | +# we don't remove APT lists here because they get re-downloaded and removed later |
| 98 | + \ |
| 99 | +# reset apt-mark's "manual" list so that "purge --auto-remove" will remove all build dependencies |
| 100 | +# (which is done after we install the built packages so we don't have to redownload any overlapping dependencies) |
| 101 | + apt-mark showmanual | xargs apt-mark auto > /dev/null; \ |
| 102 | + apt-mark manual $savedAptMark; \ |
| 103 | + \ |
| 104 | +# create a temporary local APT repo to install from (so that dependency resolution can be handled by APT, as it should be) |
| 105 | + ls -lAFh; \ |
| 106 | + dpkg-scanpackages . > Packages; \ |
| 107 | + grep '^Package: ' Packages; \ |
| 108 | + echo "deb [ trusted=yes ] file://$tempDir ./" > /etc/apt/sources.list.d/temp.list; \ |
| 109 | +# work around the following APT issue by using "Acquire::GzipIndexes=false" (overriding "/etc/apt/apt.conf.d/docker-gzip-indexes") |
| 110 | +# Could not open file /var/lib/apt/lists/partial/_tmp_tmp.ODWljpQfkE_._Packages - open (13: Permission denied) |
| 111 | +# ... |
| 112 | +# E: Failed to fetch store:/var/lib/apt/lists/partial/_tmp_tmp.ODWljpQfkE_._Packages Could not open file /var/lib/apt/lists/partial/_tmp_tmp.ODWljpQfkE_._Packages - open (13: Permission denied) |
| 113 | + apt-get -o Acquire::GzipIndexes=false update; \ |
| 114 | + ;; \ |
| 115 | + esac; \ |
| 116 | + \ |
| 117 | + apt-get install -y postgresql-common; \ |
| 118 | + sed -ri 's/#(create_main_cluster) .*$/\1 = false/' /etc/postgresql-common/createcluster.conf; \ |
| 119 | + apt-get install -y \ |
| 120 | + "postgresql-$PG_MAJOR=$PG_VERSION" \ |
| 121 | + ; \ |
| 122 | + \ |
| 123 | + rm -rf /var/lib/apt/lists/*; \ |
| 124 | + \ |
| 125 | + if [ -n "$tempDir" ]; then \ |
| 126 | +# if we have leftovers from building, let's purge them (including extra, unnecessary build deps) |
| 127 | + apt-get purge -y --auto-remove; \ |
| 128 | + rm -rf "$tempDir" /etc/apt/sources.list.d/temp.list; \ |
| 129 | + fi |
| 130 | + |
| 131 | +# make the sample config easier to munge (and "correct by default") |
| 132 | +RUN mv -v "/usr/share/postgresql/$PG_MAJOR/postgresql.conf.sample" /usr/share/postgresql/ \ |
| 133 | + && ln -sv ../postgresql.conf.sample "/usr/share/postgresql/$PG_MAJOR/" \ |
| 134 | + && sed -ri "s!^#?(listen_addresses)\s*=\s*\S+.*!\1 = '*'!" /usr/share/postgresql/postgresql.conf.sample |
| 135 | + |
| 136 | +RUN mkdir -p /var/run/postgresql && chown -R postgres:postgres /var/run/postgresql && chmod 2777 /var/run/postgresql |
| 137 | + |
| 138 | +ENV PATH $PATH:/usr/lib/postgresql/$PG_MAJOR/bin |
| 139 | +ENV PGDATA /var/lib/postgresql/data |
| 140 | +RUN mkdir -p "$PGDATA" && chown -R postgres:postgres "$PGDATA" && chmod 777 "$PGDATA" # this 777 will be replaced by 700 at runtime (allows semi-arbitrary "--user" values) |
| 141 | +VOLUME /var/lib/postgresql/data |
| 142 | + |
| 143 | +COPY docker-entrypoint.sh /usr/local/bin/ |
| 144 | +RUN ln -s usr/local/bin/docker-entrypoint.sh / # backwards compat |
| 145 | +ENTRYPOINT ["docker-entrypoint.sh"] |
| 146 | + |
| 147 | +EXPOSE 5432 |
| 148 | +CMD ["postgres"] |
0 commit comments