|
| 1 | +# vim:set ft=dockerfile: |
| 2 | +FROM debian:buster-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 set -eux; \ |
| 16 | + groupadd -r postgres --gid=999; \ |
| 17 | +# https://salsa.debian.org/postgresql/postgresql-common/blob/997d842ee744687d99a2b2d95c1083a2615c79e8/debian/postgresql-common.postinst#L32-35 |
| 18 | + useradd -r -g postgres --uid=999 --home-dir=/var/lib/postgresql --shell=/bin/bash postgres; \ |
| 19 | +# also create the postgres user's home directory with appropriate permissions |
| 20 | +# see https://github.com/docker-library/postgres/issues/274 |
| 21 | + mkdir -p /var/lib/postgresql; \ |
| 22 | + chown -R postgres:postgres /var/lib/postgresql |
| 23 | + |
| 24 | +# grab gosu for easy step-down from root |
| 25 | +ENV GOSU_VERSION 1.11 |
| 26 | +RUN set -x \ |
| 27 | + && apt-get update && apt-get install -y --no-install-recommends ca-certificates wget && rm -rf /var/lib/apt/lists/* \ |
| 28 | + && wget -O /usr/local/bin/gosu "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$(dpkg --print-architecture)" \ |
| 29 | + && wget -O /usr/local/bin/gosu.asc "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$(dpkg --print-architecture).asc" \ |
| 30 | + && export GNUPGHOME="$(mktemp -d)" \ |
| 31 | + && gpg --batch --keyserver ha.pool.sks-keyservers.net --recv-keys B42F6819007F00F88E364FD4036A9C25BF357DD4 \ |
| 32 | + && gpg --batch --verify /usr/local/bin/gosu.asc /usr/local/bin/gosu \ |
| 33 | + && { command -v gpgconf > /dev/null && gpgconf --kill all || :; } \ |
| 34 | + && rm -rf "$GNUPGHOME" /usr/local/bin/gosu.asc \ |
| 35 | + && chmod +x /usr/local/bin/gosu \ |
| 36 | + && gosu nobody true \ |
| 37 | + && apt-get purge -y --auto-remove ca-certificates wget |
| 38 | + |
| 39 | +# make the "en_US.UTF-8" locale so postgres will be utf-8 enabled by default |
| 40 | +RUN set -eux; \ |
| 41 | + if [ -f /etc/dpkg/dpkg.cfg.d/docker ]; then \ |
| 42 | +# 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) |
| 43 | + grep -q '/usr/share/locale' /etc/dpkg/dpkg.cfg.d/docker; \ |
| 44 | + sed -ri '/\/usr\/share\/locale/d' /etc/dpkg/dpkg.cfg.d/docker; \ |
| 45 | + ! grep -q '/usr/share/locale' /etc/dpkg/dpkg.cfg.d/docker; \ |
| 46 | + fi; \ |
| 47 | + apt-get update; apt-get install -y locales; rm -rf /var/lib/apt/lists/*; \ |
| 48 | + localedef -i en_US -c -f UTF-8 -A /usr/share/locale/locale.alias en_US.UTF-8 |
| 49 | +ENV LANG en_US.utf8 |
| 50 | + |
| 51 | +# install "nss_wrapper" in case we need to fake "/etc/passwd" and "/etc/group" (especially for OpenShift) |
| 52 | +# https://github.com/docker-library/postgres/issues/359 |
| 53 | +# https://cwrap.org/nss_wrapper.html |
| 54 | +RUN set -eux; \ |
| 55 | + apt-get update; \ |
| 56 | + apt-get install -y --no-install-recommends libnss-wrapper; \ |
| 57 | + rm -rf /var/lib/apt/lists/* |
| 58 | + |
| 59 | +RUN mkdir /docker-entrypoint-initdb.d |
| 60 | + |
| 61 | +RUN set -ex; \ |
| 62 | +# pub 4096R/ACCC4CF8 2011-10-13 [expires: 2019-07-02] |
| 63 | +# Key fingerprint = B97B 0AFC AA1A 47F0 44F2 44A0 7FCC 7D46 ACCC 4CF8 |
| 64 | +# uid PostgreSQL Debian Repository |
| 65 | + key='B97B0AFCAA1A47F044F244A07FCC7D46ACCC4CF8'; \ |
| 66 | + export GNUPGHOME="$(mktemp -d)"; \ |
| 67 | + gpg --batch --keyserver ha.pool.sks-keyservers.net --recv-keys "$key"; \ |
| 68 | + gpg --batch --export "$key" > /etc/apt/trusted.gpg.d/postgres.gpg; \ |
| 69 | + command -v gpgconf > /dev/null && gpgconf --kill all; \ |
| 70 | + rm -rf "$GNUPGHOME"; \ |
| 71 | + apt-key list |
| 72 | + |
| 73 | +ENV PG_MAJOR 12 |
| 74 | +ENV PG_VERSION 12~beta1-1.pgdg100+1 |
| 75 | + |
| 76 | +RUN set -ex; \ |
| 77 | + \ |
| 78 | +# see note below about "*.pyc" files |
| 79 | + export PYTHONDONTWRITEBYTECODE=1; \ |
| 80 | + \ |
| 81 | + dpkgArch="$(dpkg --print-architecture)"; \ |
| 82 | + case "$dpkgArch" in \ |
| 83 | + amd64|i386|ppc64el) \ |
| 84 | +# arches officialy built by upstream |
| 85 | + echo "deb http://apt.postgresql.org/pub/repos/apt/ buster-pgdg main $PG_MAJOR" > /etc/apt/sources.list.d/pgdg.list; \ |
| 86 | + apt-get update; \ |
| 87 | + ;; \ |
| 88 | + *) \ |
| 89 | +# we're on an architecture upstream doesn't officially build for |
| 90 | +# let's build binaries from their published source packages |
| 91 | + echo "deb-src http://apt.postgresql.org/pub/repos/apt/ buster-pgdg main $PG_MAJOR" > /etc/apt/sources.list.d/pgdg.list; \ |
| 92 | + \ |
| 93 | + case "$PG_MAJOR" in \ |
| 94 | + 9.* | 10 ) ;; \ |
| 95 | + *) \ |
| 96 | +# https://github.com/docker-library/postgres/issues/484 (clang-6.0 required, only available in stretch-backports) |
| 97 | +# TODO remove this once we hit buster+ |
| 98 | + echo 'deb http://deb.debian.org/debian buster-backports main' >> /etc/apt/sources.list.d/pgdg.list; \ |
| 99 | + ;; \ |
| 100 | + esac; \ |
| 101 | + \ |
| 102 | + tempDir="$(mktemp -d)"; \ |
| 103 | + cd "$tempDir"; \ |
| 104 | + \ |
| 105 | + savedAptMark="$(apt-mark showmanual)"; \ |
| 106 | + \ |
| 107 | +# build .deb files from upstream's source packages (which are verified by apt-get) |
| 108 | + apt-get update; \ |
| 109 | + apt-get build-dep -y \ |
| 110 | + postgresql-common pgdg-keyring \ |
| 111 | + "postgresql-$PG_MAJOR=$PG_VERSION" \ |
| 112 | + ; \ |
| 113 | + DEB_BUILD_OPTIONS="nocheck parallel=$(nproc)" \ |
| 114 | + apt-get source --compile \ |
| 115 | + postgresql-common pgdg-keyring \ |
| 116 | + "postgresql-$PG_MAJOR=$PG_VERSION" \ |
| 117 | + ; \ |
| 118 | +# we don't remove APT lists here because they get re-downloaded and removed later |
| 119 | + \ |
| 120 | +# reset apt-mark's "manual" list so that "purge --auto-remove" will remove all build dependencies |
| 121 | +# (which is done after we install the built packages so we don't have to redownload any overlapping dependencies) |
| 122 | + apt-mark showmanual | xargs apt-mark auto > /dev/null; \ |
| 123 | + apt-mark manual $savedAptMark; \ |
| 124 | + \ |
| 125 | +# create a temporary local APT repo to install from (so that dependency resolution can be handled by APT, as it should be) |
| 126 | + ls -lAFh; \ |
| 127 | + dpkg-scanpackages . > Packages; \ |
| 128 | + grep '^Package: ' Packages; \ |
| 129 | + echo "deb [ trusted=yes ] file://$tempDir ./" > /etc/apt/sources.list.d/temp.list; \ |
| 130 | +# work around the following APT issue by using "Acquire::GzipIndexes=false" (overriding "/etc/apt/apt.conf.d/docker-gzip-indexes") |
| 131 | +# Could not open file /var/lib/apt/lists/partial/_tmp_tmp.ODWljpQfkE_._Packages - open (13: Permission denied) |
| 132 | +# ... |
| 133 | +# 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) |
| 134 | + apt-get -o Acquire::GzipIndexes=false update; \ |
| 135 | + ;; \ |
| 136 | + esac; \ |
| 137 | + \ |
| 138 | + apt-get install -y postgresql-common; \ |
| 139 | + sed -ri 's/#(create_main_cluster) .*$/\1 = false/' /etc/postgresql-common/createcluster.conf; \ |
| 140 | + apt-get install -y \ |
| 141 | + "postgresql-$PG_MAJOR=$PG_VERSION" \ |
| 142 | + ; \ |
| 143 | + \ |
| 144 | + rm -rf /var/lib/apt/lists/*; \ |
| 145 | + \ |
| 146 | + if [ -n "$tempDir" ]; then \ |
| 147 | +# if we have leftovers from building, let's purge them (including extra, unnecessary build deps) |
| 148 | + apt-get purge -y --auto-remove; \ |
| 149 | + rm -rf "$tempDir" /etc/apt/sources.list.d/temp.list; \ |
| 150 | + fi; \ |
| 151 | + \ |
| 152 | +# some of the steps above generate a lot of "*.pyc" files (and setting "PYTHONDONTWRITEBYTECODE" beforehand doesn't propagate properly for some reason), so we clean them up manually (as long as they aren't owned by a package) |
| 153 | + find /usr -name '*.pyc' -type f -exec bash -c 'for pyc; do dpkg -S "$pyc" &> /dev/null || rm -vf "$pyc"; done' -- '{}' + |
| 154 | + |
| 155 | +# make the sample config easier to munge (and "correct by default") |
| 156 | +RUN set -eux; \ |
| 157 | + dpkg-divert --add --rename --divert "/usr/share/postgresql/postgresql.conf.sample.dpkg" "/usr/share/postgresql/$PG_MAJOR/postgresql.conf.sample"; \ |
| 158 | + cp -v /usr/share/postgresql/postgresql.conf.sample.dpkg /usr/share/postgresql/postgresql.conf.sample; \ |
| 159 | + ln -sv ../postgresql.conf.sample "/usr/share/postgresql/$PG_MAJOR/"; \ |
| 160 | + sed -ri "s!^#?(listen_addresses)\s*=\s*\S+.*!\1 = '*'!" /usr/share/postgresql/postgresql.conf.sample; \ |
| 161 | + grep -F "listen_addresses = '*'" /usr/share/postgresql/postgresql.conf.sample |
| 162 | + |
| 163 | +RUN mkdir -p /var/run/postgresql && chown -R postgres:postgres /var/run/postgresql && chmod 2777 /var/run/postgresql |
| 164 | + |
| 165 | +ENV PATH $PATH:/usr/lib/postgresql/$PG_MAJOR/bin |
| 166 | +ENV PGDATA /var/lib/postgresql/data |
| 167 | +# this 777 will be replaced by 700 at runtime (allows semi-arbitrary "--user" values) |
| 168 | +RUN mkdir -p "$PGDATA" && chown -R postgres:postgres "$PGDATA" && chmod 777 "$PGDATA" |
| 169 | +VOLUME /var/lib/postgresql/data |
| 170 | + |
| 171 | +COPY docker-entrypoint.sh /usr/local/bin/ |
| 172 | +RUN ln -s usr/local/bin/docker-entrypoint.sh / # backwards compat |
| 173 | +ENTRYPOINT ["docker-entrypoint.sh"] |
| 174 | + |
| 175 | +EXPOSE 5432 |
| 176 | +CMD ["postgres"] |
0 commit comments