Docker: copy postgres from base image (#13279)

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.
This commit is contained in:
Richard van der Hoff 2022-07-15 11:13:40 +01:00 committed by GitHub
parent cc21a431f3
commit 512486bbeb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 31 deletions

1
changelog.d/13279.misc Normal file
View File

@ -0,0 +1 @@
Reduce the rebuild time for the complement-synapse docker image.

View File

@ -4,21 +4,37 @@
# #
# Instructions for building this image from those it depends on is detailed in this guide: # Instructions for building this image from those it depends on is detailed in this guide:
# https://github.com/matrix-org/synapse/blob/develop/docker/README-testing.md#testing-with-postgresql-and-single-or-multi-process-synapse # https://github.com/matrix-org/synapse/blob/develop/docker/README-testing.md#testing-with-postgresql-and-single-or-multi-process-synapse
ARG SYNAPSE_VERSION=latest ARG SYNAPSE_VERSION=latest
# first of all, we create a base image with a postgres server and database,
# which we can copy into the target image. For repeated rebuilds, this is
# much faster than apt installing postgres each time.
#
# This trick only works because (a) the Synapse image happens to have all the
# shared libraries that postgres wants, (b) we use a postgres image based on
# the same debian version as Synapse's docker image (so the versions of the
# shared libraries match).
FROM postgres:13-bullseye AS postgres_base
# initialise the database cluster in /var/lib/postgresql
RUN gosu postgres initdb --locale=C --encoding=UTF-8 --auth-host password
# Configure a password and create a database for Synapse
RUN echo "ALTER USER postgres PASSWORD 'somesecret'" | gosu postgres postgres --single
RUN echo "CREATE DATABASE synapse" | gosu postgres postgres --single
# now build the final image, based on the Synapse image.
FROM matrixdotorg/synapse-workers:$SYNAPSE_VERSION FROM matrixdotorg/synapse-workers:$SYNAPSE_VERSION
# copy the postgres installation over from the image we built above
# Install postgresql RUN adduser --system --uid 999 postgres --home /var/lib/postgresql
RUN apt-get update && \ COPY --from=postgres_base /var/lib/postgresql /var/lib/postgresql
DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -yqq postgresql-13 COPY --from=postgres_base /usr/lib/postgresql /usr/lib/postgresql
COPY --from=postgres_base /usr/share/postgresql /usr/share/postgresql
# Configure a user and create a database for Synapse RUN mkdir /var/run/postgresql && chown postgres /var/run/postgresql
RUN pg_ctlcluster 13 main start && su postgres -c "echo \ ENV PATH="${PATH}:/usr/lib/postgresql/13/bin"
\"ALTER USER postgres PASSWORD 'somesecret'; \ ENV PGDATA=/var/lib/postgresql/data
CREATE DATABASE synapse \
ENCODING 'UTF8' \
LC_COLLATE='C' \
LC_CTYPE='C' \
template=template0;\" | psql" && pg_ctlcluster 13 main stop
# Extend the shared homeserver config to disable rate-limiting, # Extend the shared homeserver config to disable rate-limiting,
# set Complement's static shared secret, enable registration, amongst other # set Complement's static shared secret, enable registration, amongst other

View File

@ -1,5 +1,5 @@
[program:postgres] [program:postgres]
command=/usr/local/bin/prefix-log /usr/bin/pg_ctlcluster 13 main start --foreground command=/usr/local/bin/prefix-log gosu postgres postgres
# Only start if START_POSTGRES=1 # Only start if START_POSTGRES=1
autostart=%(ENV_START_POSTGRES)s autostart=%(ENV_START_POSTGRES)s