forked from sameersbn/docker-postgresql
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Dockerfile
157 lines (138 loc) · 6.76 KB
/
Dockerfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
ARG ALPINE_VERSION=alpine:latest
# ~~~~~~~ CREATE BUILD BASE ~~~~~~~
FROM ${ALPINE_VERSION} AS base
RUN \
# echo "@${ALPINE_VERSION} http://nl.alpinelinux.org/alpine/${ALPINE_VERSION}/main" >> /etc/apk/repositories && \
echo -e "\033[93m===== Downloading dependencies =====\033[0m" && \
apk add --update tzdata acl bash curl tar perl python3 libuuid libxml2 libldap libxslt xz su-exec && \
(rm -rf /var/cache/apk/* > /dev/null || true) && (rm -rf /tmp/* > /dev/null || true) && \
mkdir -p /tmp/files/
# ~~~~~~~ DOWNLOAD POSTGRES ~~~~~~~
FROM --platform=${BUILDPLATFORM} ${ALPINE_VERSION} AS downloader
ARG PG_VERSION
RUN apk add --update curl
WORKDIR /tmp/
RUN \
echo -e "\033[93m===== Downloading Postgres: https://ftp.postgresql.org/pub/source/v${PG_VERSION}/postgresql-${PG_VERSION}.tar.bz2 =====\033[0m" && \
curl -O --retry 5 --max-time 300 --connect-timeout 10 -fSL https://ftp.postgresql.org/pub/source/v${PG_VERSION}/postgresql-${PG_VERSION}.tar.bz2 && \
curl -O --retry 5 --max-time 300 --connect-timeout 10 -fSL https://ftp.postgresql.org/pub/source/v${PG_VERSION}/postgresql-${PG_VERSION}.tar.bz2.md5
RUN if ! md5sum -c *.md5; then echo "MD5 sum not mached, cannot continue!"; exit 1; fi
RUN \
echo -e "\033[93m===== Extracing Postgres ${PG_VERSION} =====\033[0m" && \
cat /tmp/postgres*.tar.bz2 | tar xfj - && \
mv /tmp/postgresql-${PG_VERSION} /tmp/postgres
# ~~~~~~~ INSTALL LOCALES ~~~~~~~
FROM base as locale
# set our environment variable
ENV MUSL_LOCPATH="/usr/share/i18n/locales/musl"
# install libintl
# then install dev dependencies for musl-locales
# clone the sources
# build and install musl-locales
# remove sources and compile artifacts
# lastly remove dev dependencies again
RUN echo -e "\033[93m===== Building locales =====\033[0m" && \
apk --no-cache add libintl && \
apk --no-cache --virtual .locale_build add cmake make musl-dev gcc gettext-dev git && \
git clone https://gitlab.com/rilian-la-te/musl-locales && \
cd musl-locales && cmake -DLOCALE_PROFILE=OFF -DCMAKE_INSTALL_PREFIX:PATH=/usr . && make && make install && \
cd .. && rm -r musl-locales && \
apk del .locale_build && \
cd /usr/share/i18n/locales/musl && \
find -iname \*.UTF-8 -exec bash -c 'ln -s '{}' $(basename '{}' .UTF-8).utf8' \;
COPY locale.alias /etc/locale.alias
# ~~~~~~~ BUILD POSTGRESQL ~~~~~~~
FROM locale AS build
ENV LANG=en_US.UTF-8
ENV MUSL_LOCPATH=${LANG}
COPY files/* /tmp/files/
RUN \
echo -e "\033[93m===== Downloading build dependencies =====\033[0m" && \
apk add --virtual .build-deps util-linux-dev python3-dev perl-dev openldap-dev libxslt-dev libxml2-dev build-base linux-headers libressl-dev zlib-dev make gcc pcre-dev zlib-dev ncurses-dev readline-dev
COPY --from=downloader /tmp/postgres /tmp/postgres
WORKDIR /tmp/postgres
RUN \
# explicitly update autoconf config.guess and config.sub so they support more arches/libcs
curl -o config/config.guess --retry 5 --max-time 300 --connect-timeout 10 -fSL 'https://git.savannah.gnu.org/cgit/config.git/plain/config.guess?id=7d3d27baf8107b630586c962c057e22149653deb' && \
curl -o config/config.sub --retry 5 --max-time 300 --connect-timeout 10 -fSL 'https://git.savannah.gnu.org/cgit/config.git/plain/config.sub?id=7d3d27baf8107b630586c962c057e22149653deb' && \
if [ -f /tmp/files/*.patch ]; then for i in /tmp/files/*.patch; do patch -p1 -i $i; done; fi
RUN \
echo -e "\033[93m===== Building Postgres, please be patient... =====\033[0m" && \
cd /tmp/postgres && \
./configure \
--build=$CBUILD \
--host=$CHOST \
--enable-integer-datetimes \
--enable-thread-safety \
--prefix=/opt/postgresql \
--mandir=/usr/share/man \
--with-openssl \
--with-ldap \
--with-libxml \
--with-libxslt \
--with-perl \
--with-python \
--with-system-tzdata=/usr/share/zoneinfo \
--with-libedit-preferred \
--with-uuid=e2fs
RUN \
make -j$(nproc) world
RUN \
make install && make -C contrib install && \
install -D -m755 /tmp/files/postgresql.initd /etc/init.d/postgresql && \
install -D -m644 /tmp/files/postgresql.confd /etc/conf.d/postgresql && \
install -D -m755 /tmp/files/pg-restore.initd /etc/init.d/pg-restore && \
install -D -m644 /tmp/files/pg-restore.confd /etc/conf.d/pg-restore
# ~~~~~~~ RUN POSTGRESQL ~~~~~~~
FROM locale
LABEL maintainer="Bojan Cekrlic <https://github.com/bokysan/postgresql>"
ARG PG_VERSION
ARG BUILD_DATE
ARG VCS_REF
LABEL org.label-schema.build-date=$BUILD_DATE \
org.label-schema.name="PostgreSQL ${PG_VERSION} Kitchensink edition" \
org.label-schema.description="PostgreSQL ${PG_VERSION} on Alphine linux, with lots of optional modules" \
org.label-schema.url="https://github.com/bokysan/postgresql" \
org.label-schema.vcs-ref=$VCS_REF \
org.label-schema.vcs-url="https://github.com/bokysan/postgresql" \
org.label-schema.vendor="Boky" \
org.label-schema.version="${PG_VERSION}-01" \
org.label-schema.schema-version="1.0"
ENV PG_APP_HOME="/etc/docker-postgresql"
ENV PG_USER=postgres
ENV PG_DATABASE=postgres
ENV PG_HOME=/var/lib/postgresql
ENV PG_RUNDIR=/run/postgresql
ENV PG_LOGDIR=/var/log/postgresql
ENV PG_CERTDIR=/etc/postgresql/certs
ENV PG_LOG_ARCHIVING_COMMAND="/var/lib/postgresql/wal-backup.sh %p %f"
ENV PG_BINDIR=/usr/bin/
ENV PG_DATADIR=${PG_HOME}/${PG_VERSION}/main
RUN addgroup -S ${PG_USER} && adduser -S -D -H ${PG_USER} ${PG_USER}
COPY runtime/ ${PG_APP_HOME}/
COPY entrypoint.sh /sbin/entrypoint.sh
COPY wal-backup.sh ${PG_HOME}/wal-backup.sh
COPY --from=build /etc/init.d/postgresql /etc/init.d/postgresql
COPY --from=build /etc/conf.d/postgresql /etc/conf.d/postgresql
COPY --from=build /etc/init.d/pg-restore /etc/init.d/pg-restore
COPY --from=build /etc/conf.d/pg-restore /etc/conf.d/pg-restore
COPY --from=build /opt/postgresql /opt/postgresql
RUN \
echo -e "\033[93m===== Preparing environment =====\033[0m" && \
ln -s /opt/postgresql/bin/* /usr/bin/
RUN \
echo "PG_USER=${PG_USER}" && \
mkdir -p ${PG_RUNDIR} && chown ${PG_USER}:${PG_USER} ${PG_RUNDIR} && \
mkdir -p ${PG_LOGDIR} && chown ${PG_USER}:${PG_USER} ${PG_LOGDIR} && \
mkdir -p ${PG_HOME} && chown ${PG_USER}:${PG_USER} ${PG_HOME} && \
mkdir /docker-entrypoint-initdb.d && \
mkdir -p ${PG_HOME}/wal-backup && chown ${PG_USER}:${PG_USER} ${PG_HOME}/wal-backup && \
chmod 755 /sbin/entrypoint.sh && \
chmod 755 ${PG_HOME}/wal-backup.sh && \
date +'%Y-%m-%d %H:%M:%S %z %Z' > /build-date.txt
VOLUME ${PG_LOGDIR}
VOLUME ${PG_HOME}
HEALTHCHECK --interval=10s --timeout=5s --retries=6 CMD (netstat -an | grep :5432 | grep LISTEN && echo "SELECT 1" | psql -1 -Upostgres -v ON_ERROR_STOP=1 -hlocalhost postgres) || exit 1
EXPOSE 5432/tcp
WORKDIR ${PG_HOME}
ENTRYPOINT ["/sbin/entrypoint.sh"]