Skip to content

Commit 9d9f1fa

Browse files
committed
add postgres:9.5-alpine variant
build it from source
1 parent ec5ce80 commit 9d9f1fa

File tree

2 files changed

+177
-0
lines changed

2 files changed

+177
-0
lines changed

9.5/alpine/Dockerfile

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# vim:set ft=dockerfile:
2+
FROM alpine:3.3
3+
4+
# explicitly set user/group IDs
5+
#RUN adduser -S -G postgres -u 999 postgres
6+
7+
# make the "en_US.UTF-8" locale so postgres will be utf-8 enabled by default
8+
ENV LANG en_US.utf8
9+
10+
RUN mkdir /docker-entrypoint-initdb.d
11+
12+
ENV PG_MAJOR 9.5
13+
ENV PG_VERSION 9.5.0
14+
ENV PG_SHA256 f1c0d3a1a8aa8c92738cab0153fbfffcc4d4158b3fee84f7aa6bfea8283978bc
15+
16+
17+
# configure options taken from:
18+
# https://anonscm.debian.org/cgit/pkg-postgresql/postgresql.git/tree/debian/rules?h=9.5
19+
RUN set -x \
20+
&& apk add --no-cache --virtual .build-deps \
21+
su-exec \
22+
bash \
23+
bison \
24+
curl \
25+
flex \
26+
gcc \
27+
krb5-dev \
28+
libc-dev \
29+
libedit-dev \
30+
libxml2-dev \
31+
libxslt-dev \
32+
make \
33+
openldap-dev \
34+
openssl-dev \
35+
perl \
36+
perl-dev \
37+
python3-dev \
38+
tcl-dev \
39+
util-linux-dev \
40+
zlib-dev \
41+
&& curl -fSL "https://ftp.postgresql.org/pub/source/v$PG_VERSION/postgresql-$PG_VERSION.tar.bz2" -o postgresql.tar.bz2 \
42+
&& echo "$PG_SHA256 postgresql.tar.bz2" | sha256sum -c - \
43+
&& mkdir -p /usr/src \
44+
&& tar -jxf postgresql.tar.bz2 -C /usr/src \
45+
&& rm postgresql.tar.bz2 \
46+
&& cd /usr/src/postgresql-$PG_VERSION \
47+
&& ./configure \
48+
--enable-integer-datetimes \
49+
--enable-tap-tests \
50+
--enable-thread-safety \
51+
--prefix=/usr/local \
52+
--with-libedit-preferred \
53+
--with-openssl \
54+
--with-uuid=e2fs \
55+
&& make -j$(getconf _NPROCESSORS_ONLN) world \
56+
&& make install-world \
57+
&& make -C contrib install \
58+
&& runDeps="$( \
59+
scanelf --needed --nobanner --recursive /usr/local \
60+
| awk '{ gsub(/,/, "\nso:", $2); print "so:" $2 }' \
61+
| sort -u \
62+
| xargs -r apk info --installed \
63+
| sort -u \
64+
)" \
65+
&& apk add --virtual .postgresql-rundeps $runDeps su-exec \
66+
&& apk del .build-deps \
67+
&& cd / && rm -rf /usr/src/postgresql-* /usr/local/include/* \
68+
&& find /usr/local -name '*.a' -delete
69+
70+
RUN mkdir -p /var/run/postgresql && chown -R postgres /var/run/postgresql
71+
72+
ENV PATH /usr/lib/postgresql/$PG_MAJOR/bin:$PATH
73+
ENV PGDATA /var/lib/postgresql/data
74+
VOLUME /var/lib/postgresql/data
75+
76+
COPY docker-entrypoint.sh /
77+
78+
ENTRYPOINT ["/docker-entrypoint.sh"]
79+
80+
EXPOSE 5432
81+
CMD ["postgres"]

9.5/alpine/docker-entrypoint.sh

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#!/bin/sh -e
2+
3+
set_listen_addresses() {
4+
sedEscapedValue="$(echo "$1" | sed 's/[\/&]/\\&/g')"
5+
sed -ri "s/^#?(listen_addresses\s*=\s*)\S+/\1'$sedEscapedValue'/" "$PGDATA/postgresql.conf"
6+
}
7+
8+
if [ "$1" = 'postgres' ]; then
9+
mkdir -p "$PGDATA"
10+
chmod 700 "$PGDATA"
11+
chown -R postgres "$PGDATA"
12+
13+
chmod g+s /var/run/postgresql
14+
chown -R postgres /var/run/postgresql
15+
16+
# look specifically for PG_VERSION, as it is expected in the DB dir
17+
if [ ! -s "$PGDATA/PG_VERSION" ]; then
18+
su-exec postgres initdb
19+
20+
# check password first so we can output the warning before postgres
21+
# messes it up
22+
if [ "$POSTGRES_PASSWORD" ]; then
23+
pass="PASSWORD '$POSTGRES_PASSWORD'"
24+
authMethod=md5
25+
else
26+
# The - option suppresses leading tabs but *not* spaces. :)
27+
cat >&2 <<-'EOWARN'
28+
****************************************************
29+
WARNING: No password has been set for the database.
30+
This will allow anyone with access to the
31+
Postgres port to access your database. In
32+
Docker's default configuration, this is
33+
effectively any other container on the same
34+
system.
35+
36+
Use "-e POSTGRES_PASSWORD=password" to set
37+
it in "docker run".
38+
****************************************************
39+
EOWARN
40+
41+
pass=
42+
authMethod=trust
43+
fi
44+
45+
{ echo; echo "host all all 0.0.0.0/0 $authMethod"; } >> "$PGDATA/pg_hba.conf"
46+
47+
# internal start of server in order to allow set-up using psql-client
48+
# does not listen on TCP/IP and waits until start finishes
49+
su-exec postgres pg_ctl -D "$PGDATA" \
50+
-o "-c listen_addresses=''" \
51+
-w start
52+
53+
: ${POSTGRES_USER:=postgres}
54+
: ${POSTGRES_DB:=$POSTGRES_USER}
55+
export POSTGRES_USER POSTGRES_DB
56+
57+
if [ "$POSTGRES_DB" != 'postgres' ]; then
58+
psql --username postgres <<-EOSQL
59+
CREATE DATABASE "$POSTGRES_DB" ;
60+
EOSQL
61+
echo
62+
fi
63+
64+
if [ "$POSTGRES_USER" = 'postgres' ]; then
65+
op='ALTER'
66+
else
67+
op='CREATE'
68+
fi
69+
70+
psql --username postgres <<-EOSQL
71+
$op USER "$POSTGRES_USER" WITH SUPERUSER $pass ;
72+
EOSQL
73+
echo
74+
75+
echo
76+
for f in /docker-entrypoint-initdb.d/*; do
77+
case "$f" in
78+
*.sh) echo "$0: running $f"; . "$f" ;;
79+
*.sql) echo "$0: running $f"; psql --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" < "$f" && echo ;;
80+
*) echo "$0: ignoring $f" ;;
81+
esac
82+
echo
83+
done
84+
85+
su-exec postgres pg_ctl -D "$PGDATA" -m fast -w stop
86+
set_listen_addresses '*'
87+
88+
echo
89+
echo 'PostgreSQL init process complete; ready for start up.'
90+
echo
91+
fi
92+
93+
exec su-exec postgres "$@"
94+
fi
95+
96+
exec "$@"

0 commit comments

Comments
 (0)