Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 43423c7

Browse files
committed
Docker: copy postgres from base image
When building the docker images for complement testing, copy a preinstalled complement over from a base image, rather than apt installing it. This avoids network traffic and is much faster.
1 parent 0312ff4 commit 43423c7

File tree

3 files changed

+28
-13
lines changed

3 files changed

+28
-13
lines changed

changelog.d/13279.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Reduce the rebuild time for the complement-synapse docker image.

docker/complement/Dockerfile

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,35 @@
44
#
55
# Instructions for building this image from those it depends on is detailed in this guide:
66
# https://github.com/matrix-org/synapse/blob/develop/docker/README-testing.md#testing-with-postgresql-and-single-or-multi-process-synapse
7+
78
ARG SYNAPSE_VERSION=latest
9+
10+
# first of all, we create a base image with a postgres server and database,
11+
# which we can copy into the target image. For repeated rebuilds, this is
12+
# much faster than apt installing postgres each time.
13+
#
14+
# This trick only works because (a) the Synapse image happens to have all the
15+
# shared libraries that postgres wants, (v) we use a postgres image based on the same
16+
# debian version as Synapse's docker image (so the versions of the shared libraries
17+
# match).
18+
19+
FROM postgres:13-bullseye AS postgres_base
20+
# initialise the database cluster in /var/lib/postgresql
21+
RUN gosu postgres initdb --locale=C --encoding=UTF-8 --auth-host password
22+
23+
# Configure a password and create a database for Synapse
24+
RUN echo "ALTER USER postgres PASSWORD 'somesecret'" | gosu postgres postgres --single
25+
RUN echo "CREATE DATABASE synapse" | gosu postgres postgres --single
26+
827
FROM matrixdotorg/synapse-workers:$SYNAPSE_VERSION
928

10-
# Install postgresql
11-
RUN apt-get update && \
12-
DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -yqq postgresql-13
13-
14-
# Configure a user and create a database for Synapse
15-
RUN pg_ctlcluster 13 main start && su postgres -c "echo \
16-
\"ALTER USER postgres PASSWORD 'somesecret'; \
17-
CREATE DATABASE synapse \
18-
ENCODING 'UTF8' \
19-
LC_COLLATE='C' \
20-
LC_CTYPE='C' \
21-
template=template0;\" | psql" && pg_ctlcluster 13 main stop
29+
# copy the postgres installation over from the image we built above
30+
RUN adduser --system --uid 999 postgres --home /var/lib/postgresql
31+
COPY --from=postgres_base /var/lib/postgresql /var/lib/postgresql
32+
COPY --from=postgres_base /usr/lib/postgresql /usr/lib/postgresql
33+
COPY --from=postgres_base /usr/share/postgresql /usr/share/postgresql
34+
RUN mkdir /var/run/postgresql && chown postgres /var/run/postgresql
35+
ENV PATH="${PATH}:/usr/lib/postgresql/13/bin"
2236

2337
# Extend the shared homeserver config to disable rate-limiting,
2438
# set Complement's static shared secret, enable registration, amongst other

docker/complement/conf/postgres.supervisord.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[program:postgres]
2-
command=/usr/local/bin/prefix-log /usr/bin/pg_ctlcluster 13 main start --foreground
2+
command=/usr/local/bin/prefix-log gosu postgres postgres -D /var/lib/postgresql/data
33

44
# Only start if START_POSTGRES=1
55
autostart=%(ENV_START_POSTGRES)s

0 commit comments

Comments
 (0)