Skip to content

Commit 745ca31

Browse files
authored
Merge pull request #5 from duanhongyi/master
feat(postgres): modify the docker base image as postgres:11-alpine
2 parents 2e39f6e + 90431d8 commit 745ca31

File tree

10 files changed

+55
-61
lines changed

10 files changed

+55
-61
lines changed

Dockerfile

Lines changed: 31 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,40 @@
1-
FROM postgres:11
2-
3-
ARG DEBIAN_FRONTEND=noninteractive
4-
ARG BUILD_DEPS='gcc git libffi-dev libssl-dev python3-dev python3-pip python3-wheel'
5-
6-
RUN apt-get update && \
7-
apt-get install -y --no-install-recommends \
8-
$BUILD_DEPS \
9-
gosu \
10-
lzop \
11-
libpq-dev \
12-
pv \
13-
python3 \
14-
util-linux \
15-
# swift package needs pkg_resources and setuptools
16-
python3-pkg-resources \
17-
python3-setuptools \
18-
python3-pip && \
19-
ln -sf /usr/bin/python3 /usr/bin/python && \
20-
ln -sf /usr/bin/pip3 /usr/bin/pip
21-
22-
# setuptools from ubuntu archives is too old for googleapis-common-protos
23-
RUN pip install --upgrade setuptools && \
24-
pip install --disable-pip-version-check --no-cache-dir \
25-
envdir==1.0.1 \
26-
wal-e[aws,azure,google,swift]==1.1.0 \
27-
gcloud==0.18.3 \
28-
oauth2client==4.1.3 \
29-
azure-storage==0.20.0
30-
31-
# cleanup
32-
RUN apt-get purge -y --auto-remove $BUILD_DEPS && \
33-
apt-get autoremove -y && \
34-
apt-get clean -y
1+
FROM postgres:11-alpine
2+
3+
ENV WALE_LOG_DESTINATION stderr
4+
ENV WALE_ENVDIR /etc/wal-e.d/env
5+
6+
RUN mkdir -p $WALE_ENVDIR \
7+
&& echo 'http://dl-cdn.alpinelinux.org/alpine/v3.5/main' >> /etc/apk/repositories \
8+
&& apk add --update --virtual .build-deps \
9+
git \
10+
build-base \
11+
libffi-dev \
12+
openssl-dev \
13+
python3-dev=3.5.6-r0 \
14+
linux-headers \
15+
&& apk add \
16+
lzo \
17+
pv \
18+
util-linux \
19+
ca-certificates \
20+
python3=3.5.6-r0 \
21+
&& pip3 install --upgrade pip setuptools \
22+
&& pip install --disable-pip-version-check --no-cache-dir \
23+
psycopg2-binary==2.7.6.1 \
24+
envdir==1.0.1 \
25+
wal-e[aws,azure,google,swift]==1.1.0 \
26+
gcloud==0.18.3 \
27+
oauth2client==4.1.3 \
28+
azure-storage==0.20.0 \
29+
&& apk del .build-deps \
30+
&& rm -rf /var/cache/apk/*
3531

3632
COPY rootfs /
37-
ENV WALE_ENVDIR=/etc/wal-e.d/env
38-
RUN mkdir -p $WALE_ENVDIR
3933

4034
ARG PATCH_CMD="python3 /patcher-script.py"
4135
RUN $PATCH_CMD file /bin/create_bucket /patcher-script.d/patch_boto_s3.py
42-
RUN $PATCH_CMD file /usr/local/bin/wal-e /patcher-script.d/patch_boto_s3.py
36+
RUN $PATCH_CMD module wal_e.cmd /patcher-script.d/patch_boto_s3.py
4337
RUN $PATCH_CMD module wal_e.worker.worker_util /patcher-script.d/patch_wal_e_s3.py
4438

45-
4639
CMD ["/docker-entrypoint.sh", "postgres"]
4740
EXPOSE 5432

charts/database/templates/database-deployment.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,15 @@ spec:
4141
value: "{{.Values.global.storage}}"
4242
- name: PGCTLTIMEOUT
4343
value: "{{.Values.postgres.timeout}}"
44+
{{- if eq .Values.global.storage "s3" }}
45+
- name: S3_SSE
46+
value: "{{.Values.s3.use_sse}}"
47+
{{- end}}
4448
lifecycle:
4549
preStop:
4650
exec:
4751
command:
48-
- gosu
52+
- su-exec
4953
- postgres
5054
- do_backup
5155
readinessProbe:

rootfs/bin/create_bucket

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env python
1+
#!/usr/bin/env python3
22

33
import os
44

rootfs/bin/do_backup

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export BACKUPS_TO_RETAIN=${BACKUPS_TO_RETAIN:-5}
55
echo "Performing a base backup..."
66
if [[ -f "$PGDATA/recovery.conf" ]] ; then
77
echo "Database is currently recovering from a backup. Aborting"
8+
sleep 9
89
else
910
# perform a backup
1011
envdir "$WALE_ENVDIR" wal-e backup-push "$PGDATA"

rootfs/bin/is_running

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ if [[ -f "$PGDATA/recovery.conf" ]]; then
88
exit 1
99
fi
1010

11-
gosu postgres pg_ctl status
11+
su-exec postgres pg_ctl status

rootfs/docker-entrypoint-initdb.d/001_setup_envdir.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ if [[ "$DATABASE_STORAGE" == "s3" || "$DATABASE_STORAGE" == "minio" ]]; then
1717
else
1818
echo "https+path://s3-${AWS_REGION}.amazonaws.com:443" > WALE_S3_ENDPOINT
1919
fi
20+
if [[ $S3_SSE ]]; then
21+
echo $S3_SSE > WALE_S3_SSE
22+
else
23+
echo "false" > WALE_S3_SSE
24+
fi
2025
else
2126
AWS_REGION="us-east-1"
2227
BUCKET_NAME="dbwal"

rootfs/docker-entrypoint-initdb.d/003_restore_from_backup.sh

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ chmod 0700 "$PGDATA"
1313

1414
# reboot the server for wal_level to be set before backing up
1515
echo "Rebooting postgres to enable archive mode"
16-
gosu postgres pg_ctl -D "$PGDATA" -w restart
16+
su-exec postgres pg_ctl -D "$PGDATA" -w restart
1717

1818
# check if there are any backups -- if so, let's restore
1919
# we could probably do better than just testing number of lines -- one line is just a heading, meaning no backups
2020
if [[ $(envdir "$WALE_ENVDIR" wal-e --terse backup-list | wc -l) -gt "1" ]]; then
2121
echo "Found backups. Restoring from backup..."
22-
gosu postgres pg_ctl -D "$PGDATA" -w stop
22+
su-exec postgres pg_ctl -D "$PGDATA" -w stop
2323
rm -rf "$PGDATA/*"
2424
envdir "$WALE_ENVDIR" wal-e backup-fetch "$PGDATA" LATEST
2525
cat << EOF > "$PGDATA/postgresql.conf"
@@ -50,20 +50,11 @@ EOF
5050
echo "restore_command = 'envdir /etc/wal-e.d/env wal-e wal-fetch \"%f\" \"%p\"'" >> "$PGDATA/recovery.conf"
5151
chown -R postgres:postgres "$PGDATA"
5252
chmod 0700 "$PGDATA"
53-
gosu postgres pg_ctl -D "$PGDATA" \
53+
su-exec postgres pg_ctl -D "$PGDATA" \
5454
-o "-c listen_addresses=''" \
5555
-w start
56-
57-
echo "Waiting for recovery completion..."
58-
while [ ! -f "$PGDATA/recovery.done" ]
59-
do
60-
sleep 2
61-
done
6256
fi
6357

64-
echo "Performing an initial backup..."
65-
gosu postgres envdir "$WALE_ENVDIR" wal-e backup-push "$PGDATA"
66-
6758
# ensure $PGDATA has the right permissions
6859
chown -R postgres:postgres "$PGDATA"
6960
chmod 0700 "$PGDATA"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#!/usr/bin/env bash
22

33
# Run periodic backups in the background
4-
gosu postgres backup &
4+
su-exec postgres backup &

rootfs/docker-entrypoint.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ if [ "$1" = 'postgres' ]; then
2323

2424
# look specifically for PG_VERSION, as it is expected in the DB dir
2525
if [ ! -s "$PGDATA/PG_VERSION" ]; then
26-
gosu postgres initdb
26+
su-exec postgres initdb
2727

2828
# check password first so we can output the warning before postgres
2929
# messes it up
@@ -54,7 +54,7 @@ if [ "$1" = 'postgres' ]; then
5454

5555
# internal start of server in order to allow set-up using psql-client
5656
# does not listen on TCP/IP and waits until start finishes
57-
gosu postgres pg_ctl -D "$PGDATA" \
57+
su-exec postgres pg_ctl -D "$PGDATA" \
5858
-o "-c listen_addresses=''" \
5959
-w start
6060

@@ -94,15 +94,15 @@ if [ "$1" = 'postgres' ]; then
9494
echo
9595
done
9696

97-
gosu postgres pg_ctl -D "$PGDATA" -m fast -w stop
97+
su-exec postgres pg_ctl -D "$PGDATA" -m fast -w stop
9898
set_listen_addresses '*'
9999

100100
echo
101101
echo 'PostgreSQL init process complete; ready for start up.'
102102
echo
103103
fi
104104

105-
exec gosu postgres "$@"
105+
exec su-exec postgres "$@"
106106
fi
107107

108108
exec "$@"

rootfs/patcher-script.d/patch_wal_e_s3.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ def wrap_uri_put_file(creds, uri, fp, content_type=None, conn=None):
77
k = s3_util._uri_to_key(creds, uri, conn=conn)
88
if content_type is not None:
99
k.content_type = content_type
10+
encrypt_key = False
1011
if os.getenv('DATABASE_STORAGE') == 's3':
11-
encrypt_key=True
12-
else:
13-
encrypt_key=False
12+
if os.getenv('WALE_S3_SSE', 'false') == 'true':
13+
encrypt_key = True
1414
k.set_contents_from_file(fp, encrypt_key=encrypt_key)
1515
return k
1616
s3.uri_put_file = wrap_uri_put_file

0 commit comments

Comments
 (0)