|
| 1 | +# vim:set ft=dockerfile: |
| 2 | +FROM alpine:3.6 |
| 3 | + |
| 4 | +# alpine includes "postgres" user/group in base install |
| 5 | +# /etc/passwd:22:postgres:x:70:70::/var/lib/postgresql:/bin/sh |
| 6 | +# /etc/group:34:postgres:x:70: |
| 7 | +# the home directory for the postgres user, however, is not created by default |
| 8 | +# see https://github.com/docker-library/postgres/issues/274 |
| 9 | +RUN set -ex; \ |
| 10 | + postgresHome="$(getent passwd postgres)"; \ |
| 11 | + postgresHome="$(echo "$postgresHome" | cut -d: -f6)"; \ |
| 12 | + [ "$postgresHome" = '/var/lib/postgresql' ]; \ |
| 13 | + mkdir -p "$postgresHome"; \ |
| 14 | + chown -R postgres:postgres "$postgresHome" |
| 15 | + |
| 16 | +# su-exec (gosu-compatible) is installed further down |
| 17 | + |
| 18 | +# make the "en_US.UTF-8" locale so postgres will be utf-8 enabled by default |
| 19 | +# alpine doesn't require explicit locale-file generation |
| 20 | +ENV LANG en_US.utf8 |
| 21 | + |
| 22 | +RUN mkdir /docker-entrypoint-initdb.d |
| 23 | + |
| 24 | +ENV PG_MAJOR 10 |
| 25 | +ENV PG_VERSION 10beta1 |
| 26 | +ENV PG_SHA256 7eee02e6f6646c7d4d6e78893a4ff638cfa5f1025b706712da8c6ef2257b5e29 |
| 27 | + |
| 28 | +RUN set -ex \ |
| 29 | + \ |
| 30 | + && apk add --no-cache --virtual .fetch-deps \ |
| 31 | + ca-certificates \ |
| 32 | + openssl \ |
| 33 | + tar \ |
| 34 | + \ |
| 35 | + && wget -O postgresql.tar.bz2 "https://ftp.postgresql.org/pub/source/v$PG_VERSION/postgresql-$PG_VERSION.tar.bz2" \ |
| 36 | + && echo "$PG_SHA256 *postgresql.tar.bz2" | sha256sum -c - \ |
| 37 | + && mkdir -p /usr/src/postgresql \ |
| 38 | + && tar \ |
| 39 | + --extract \ |
| 40 | + --file postgresql.tar.bz2 \ |
| 41 | + --directory /usr/src/postgresql \ |
| 42 | + --strip-components 1 \ |
| 43 | + && rm postgresql.tar.bz2 \ |
| 44 | + \ |
| 45 | + && apk add --no-cache --virtual .build-deps \ |
| 46 | + bison \ |
| 47 | + coreutils \ |
| 48 | + dpkg-dev dpkg \ |
| 49 | + flex \ |
| 50 | + gcc \ |
| 51 | +# krb5-dev \ |
| 52 | + libc-dev \ |
| 53 | + libedit-dev \ |
| 54 | + libxml2-dev \ |
| 55 | + libxslt-dev \ |
| 56 | + make \ |
| 57 | +# openldap-dev \ |
| 58 | + openssl-dev \ |
| 59 | + perl \ |
| 60 | +# perl-dev \ |
| 61 | +# python-dev \ |
| 62 | +# python3-dev \ |
| 63 | +# tcl-dev \ |
| 64 | + util-linux-dev \ |
| 65 | + zlib-dev \ |
| 66 | + \ |
| 67 | + && cd /usr/src/postgresql \ |
| 68 | +# update "DEFAULT_PGSOCKET_DIR" to "/var/run/postgresql" (matching Debian) |
| 69 | +# see https://anonscm.debian.org/git/pkg-postgresql/postgresql.git/tree/debian/patches/51-default-sockets-in-var.patch?id=8b539fcb3e093a521c095e70bdfa76887217b89f |
| 70 | + && awk '$1 == "#define" && $2 == "DEFAULT_PGSOCKET_DIR" && $3 == "\"/tmp\"" { $3 = "\"/var/run/postgresql\""; print; next } { print }' src/include/pg_config_manual.h > src/include/pg_config_manual.h.new \ |
| 71 | + && grep '/var/run/postgresql' src/include/pg_config_manual.h.new \ |
| 72 | + && mv src/include/pg_config_manual.h.new src/include/pg_config_manual.h \ |
| 73 | + && gnuArch="$(dpkg-architecture --query DEB_BUILD_GNU_TYPE)" \ |
| 74 | +# explicitly update autoconf config.guess and config.sub so they support more arches/libcs |
| 75 | + && wget -O config/config.guess 'https://git.savannah.gnu.org/cgit/config.git/plain/config.guess?id=7d3d27baf8107b630586c962c057e22149653deb' \ |
| 76 | + && wget -O config/config.sub 'https://git.savannah.gnu.org/cgit/config.git/plain/config.sub?id=7d3d27baf8107b630586c962c057e22149653deb' \ |
| 77 | +# configure options taken from: |
| 78 | +# https://anonscm.debian.org/cgit/pkg-postgresql/postgresql.git/tree/debian/rules?h=9.5 |
| 79 | + && ./configure \ |
| 80 | + --build="$gnuArch" \ |
| 81 | +# "/usr/src/postgresql/src/backend/access/common/tupconvert.c:105: undefined reference to `libintl_gettext'" |
| 82 | +# --enable-nls \ |
| 83 | + --enable-integer-datetimes \ |
| 84 | + --enable-thread-safety \ |
| 85 | + --enable-tap-tests \ |
| 86 | +# skip debugging info -- we want tiny size instead |
| 87 | +# --enable-debug \ |
| 88 | + --disable-rpath \ |
| 89 | + --with-uuid=e2fs \ |
| 90 | + --with-gnu-ld \ |
| 91 | + --with-pgport=5432 \ |
| 92 | + --with-system-tzdata=/usr/share/zoneinfo \ |
| 93 | + --prefix=/usr/local \ |
| 94 | + --with-includes=/usr/local/include \ |
| 95 | + --with-libraries=/usr/local/lib \ |
| 96 | + \ |
| 97 | +# these make our image abnormally large (at least 100MB larger), which seems uncouth for an "Alpine" (ie, "small") variant :) |
| 98 | +# --with-krb5 \ |
| 99 | +# --with-gssapi \ |
| 100 | +# --with-ldap \ |
| 101 | +# --with-tcl \ |
| 102 | +# --with-perl \ |
| 103 | +# --with-python \ |
| 104 | +# --with-pam \ |
| 105 | + --with-openssl \ |
| 106 | + --with-libxml \ |
| 107 | + --with-libxslt \ |
| 108 | + && make -j "$(nproc)" world \ |
| 109 | + && make install-world \ |
| 110 | + && make -C contrib install \ |
| 111 | + \ |
| 112 | + && runDeps="$( \ |
| 113 | + scanelf --needed --nobanner --recursive /usr/local \ |
| 114 | + | awk '{ gsub(/,/, "\nso:", $2); print "so:" $2 }' \ |
| 115 | + | sort -u \ |
| 116 | + | xargs -r apk info --installed \ |
| 117 | + | sort -u \ |
| 118 | + )" \ |
| 119 | + && apk add --no-cache --virtual .postgresql-rundeps \ |
| 120 | + $runDeps \ |
| 121 | + bash \ |
| 122 | + su-exec \ |
| 123 | +# tzdata is optional, but only adds around 1Mb to image size and is recommended by Django documentation: |
| 124 | +# https://docs.djangoproject.com/en/1.10/ref/databases/#optimizing-postgresql-s-configuration |
| 125 | + tzdata \ |
| 126 | + && apk del .fetch-deps .build-deps \ |
| 127 | + && cd / \ |
| 128 | + && rm -rf \ |
| 129 | + /usr/src/postgresql \ |
| 130 | + /usr/local/share/doc \ |
| 131 | + /usr/local/share/man \ |
| 132 | + && find /usr/local -name '*.a' -delete |
| 133 | + |
| 134 | +# make the sample config easier to munge (and "correct by default") |
| 135 | +RUN sed -ri "s!^#?(listen_addresses)\s*=\s*\S+.*!\1 = '*'!" /usr/local/share/postgresql/postgresql.conf.sample |
| 136 | + |
| 137 | +RUN mkdir -p /var/run/postgresql && chown -R postgres:postgres /var/run/postgresql && chmod 2777 /var/run/postgresql |
| 138 | + |
| 139 | +ENV PATH /usr/lib/postgresql/$PG_MAJOR/bin:$PATH |
| 140 | +ENV PGDATA /var/lib/postgresql/data |
| 141 | +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) |
| 142 | +VOLUME /var/lib/postgresql/data |
| 143 | + |
| 144 | +COPY docker-entrypoint.sh /usr/local/bin/ |
| 145 | +RUN ln -s usr/local/bin/docker-entrypoint.sh / # backwards compat |
| 146 | +ENTRYPOINT ["docker-entrypoint.sh"] |
| 147 | + |
| 148 | +EXPOSE 5432 |
| 149 | +CMD ["postgres"] |
0 commit comments