Skip to content

Commit 7bd44af

Browse files
Added test containers.
1 parent bc7a5bf commit 7bd44af

File tree

2 files changed

+167
-0
lines changed

2 files changed

+167
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# vim:set ft=dockerfile:
2+
FROM centos:centos7
3+
4+
# explicitly set user/group IDs
5+
RUN groupadd -r postgres && useradd -r -g postgres postgres
6+
7+
# grab gosu for easy step-down from root
8+
ENV GOSU_VERSION 1.7
9+
ENV GOSU_ARCH amd64
10+
RUN set -x \
11+
&& yum update -y && yum install -y ca-certificates wget && yum clean all \
12+
&& wget -O /usr/local/bin/gosu "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-${GOSU_ARCH}" \
13+
&& wget -O /usr/local/bin/gosu.asc "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-${GOSU_ARCH}.asc" \
14+
&& export GNUPGHOME="$(mktemp -d)" \
15+
&& gpg --keyserver ha.pool.sks-keyservers.net --recv-keys B42F6819007F00F88E364FD4036A9C25BF357DD4 \
16+
&& gpg --batch --verify /usr/local/bin/gosu.asc /usr/local/bin/gosu \
17+
&& rm -r "$GNUPGHOME" /usr/local/bin/gosu.asc \
18+
&& chmod +x /usr/local/bin/gosu \
19+
&& gosu nobody true \
20+
21+
# make the "en_US.UTF-8" locale so postgres will be utf-8 enabled by default
22+
RUN yum update -y \
23+
&& localedef -i en_US -c -f UTF-8 -A /usr/share/locale/locale.alias en_US.UTF-8
24+
25+
ENV LANG en_US.utf8
26+
27+
RUN mkdir /docker-entrypoint-initdb.d
28+
29+
ENV PG_REPOURL="https://download.postgresql.org/pub/repos/yum"
30+
ENV PG_MAJOR=%%PG_MAJOR%% \
31+
PG_MAJOR_NODOT=%%PG_MAJOR_NODOT%% \
32+
PG_MINOR=%%MINORVERSION% \
33+
PG_REVISION=%%REVISION%%
34+
35+
ENV PG_VERSION=${PG_MAJOR}.${PG_MINOR}-${PG_REVISION} \
36+
PGDG_VERSION=${PG_MAJOR}-${PG_REVISION} \
37+
OSV_NAME=rhel7
38+
39+
ENV PG_REPO="${PG_REPOURL}/${PG_MAJOR}/redhat/rhel-7.2-x86_64"
40+
41+
RUN rpm --import ${PG_REPOURL}/RPM-GPG-KEY-PGDG && rpm --import ${PG_REPOURL}/RPM-GPG-KEY-PGDG-${PG_MAJOR_NODOT}
42+
43+
RUN echo -e "[postgres_repo]\nname=Postgres Repo\nbaseurl=${PG_REPO}\nenabled=1\ngpgcheck=1" > /etc/yum.repos.d/postgres.repo
44+
45+
RUN yum update -y \
46+
&& yum install -y ${PG_REPO}/pgdg-centos${PG_MAJOR_NODOT}-${PGDG_VERSION}.noarch.rpm \
47+
&& yum install -y ${PG_REPO}/postgresql${PG_MAJOR_NODOT}-server-${PG_VERSION}PGDG.${OSV_NAME}.x86_64.rpm \
48+
# && sed -ri 's/#(create_main_cluster) .*$/\1 = false/' /etc/postgresql-common/createcluster.conf \
49+
&& yum install -y \
50+
${PG_REPO}/postgresql${PG_MAJOR_NODOT}-${PG_VERSION}PGDG.${OSV_NAME}.x86_64.rpm \
51+
${PG_REPO}/postgresql${PG_MAJOR_NODOT}-contrib-${PG_VERSION}PGDG.${OSV_NAME}.x86_64.rpm \
52+
&& yum clean all
53+
54+
# make the sample config easier to munge (and "correct by default")
55+
RUN sed -ri "s!^#?(listen_addresses)\s*=\s*\S+.*!\1 = '*'!" /usr/pgsql-${PG_MAJOR}/share/postgresql.conf.sample
56+
57+
RUN mkdir -p /var/run/postgresql && chown -R postgres /var/run/postgresql
58+
59+
ENV PATH /usr/pgsql-${PG_MAJOR}/bin:$PATH
60+
ENV PGDATA /var/lib/postgresql/data
61+
VOLUME /var/lib/postgresql/data
62+
63+
COPY docker-entrypoint.sh /
64+
65+
ENTRYPOINT ["/docker-entrypoint.sh"]
66+
67+
EXPOSE 5432
68+
CMD ["postgres"]
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#!/bin/bash
2+
set -e
3+
4+
if [ "${1:0:1}" = '-' ]; then
5+
set -- postgres "$@"
6+
fi
7+
8+
if [ "$1" = 'postgres' ]; then
9+
mkdir -p "$PGDATA"
10+
chmod 700 "$PGDATA"
11+
chown -R postgres "$PGDATA"
12+
13+
chmod g+s /run/postgresql
14+
chown -R postgres /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+
eval "gosu postgres initdb $POSTGRES_INITDB_ARGS"
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 external TCP/IP and waits until start finishes
49+
gosu postgres pg_ctl -D "$PGDATA" \
50+
-o "-c listen_addresses='localhost'" \
51+
-w start
52+
53+
: ${POSTGRES_USER:=postgres}
54+
: ${POSTGRES_DB:=$POSTGRES_USER}
55+
export POSTGRES_USER POSTGRES_DB
56+
57+
psql=( psql -v ON_ERROR_STOP=1 )
58+
59+
if [ "$POSTGRES_DB" != 'postgres' ]; then
60+
"${psql[@]}" --username postgres <<-EOSQL
61+
CREATE DATABASE "$POSTGRES_DB" ;
62+
EOSQL
63+
echo
64+
fi
65+
66+
if [ "$POSTGRES_USER" = 'postgres' ]; then
67+
op='ALTER'
68+
else
69+
op='CREATE'
70+
fi
71+
"${psql[@]}" --username postgres <<-EOSQL
72+
$op USER "$POSTGRES_USER" WITH SUPERUSER $pass ;
73+
EOSQL
74+
echo
75+
76+
psql+=( --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" )
77+
78+
echo
79+
for f in /docker-entrypoint-initdb.d/*; do
80+
case "$f" in
81+
*.sh) echo "$0: running $f"; . "$f" ;;
82+
*.sql) echo "$0: running $f"; "${psql[@]}" < "$f"; echo ;;
83+
*.sql.gz) echo "$0: running $f"; gunzip -c "$f" | "${psql[@]}"; echo ;;
84+
*) echo "$0: ignoring $f" ;;
85+
esac
86+
echo
87+
done
88+
89+
gosu postgres pg_ctl -D "$PGDATA" -m fast -w stop
90+
91+
echo
92+
echo 'PostgreSQL init process complete; ready for start up.'
93+
echo
94+
fi
95+
96+
exec gosu postgres "$@"
97+
fi
98+
99+
exec "$@"

0 commit comments

Comments
 (0)