Dockerfile 4.3 KB

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