Skip to content

Commit 4097ada

Browse files
authored
Optimize Dockerfile-workers (#18292)
- Use a `uv:python` image for the first build layer, to reduce the number of intermediate images required, as the main Dockerfile uses that image already - Use a cache mount for `apt` commands - Skip a pointless install of `redis-server`, since the redis Docker image is copied from instead - Move some RUN steps out of the final image layer & into the build layer Depends on #18275 ### Pull Request Checklist <!-- Please read https://element-hq.github.io/synapse/latest/development/contributing_guide.html before submitting your pull request --> * [x] Pull request is based on the develop branch * [x] Pull request includes a [changelog file](https://element-hq.github.io/synapse/latest/development/contributing_guide.html#changelog). The entry should: - Be a short description of your change which makes sense to users. "Fixed a bug that prevented receiving messages from other servers." instead of "Moved X method from `EventStore` to `EventWorkerStore`.". - Use markdown where necessary, mostly for `code blocks`. - End with either a period (.) or an exclamation mark (!). - Start with a capital letter. - Feel free to credit yourself, by adding a sentence "Contributed by @github_username." or "Contributed by [Your Name]." to the end of the entry. * [x] [Code style](https://element-hq.github.io/synapse/latest/code_style.html) is correct (run the [linters](https://element-hq.github.io/synapse/latest/development/contributing_guide.html#run-the-linters))
1 parent f79811e commit 4097ada

File tree

2 files changed

+28
-23
lines changed

2 files changed

+28
-23
lines changed

changelog.d/18292.docker

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Optimize the build of the workers image.

docker/Dockerfile-workers

+27-23
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,37 @@
33
ARG SYNAPSE_VERSION=latest
44
ARG FROM=matrixdotorg/synapse:$SYNAPSE_VERSION
55
ARG DEBIAN_VERSION=bookworm
6+
ARG PYTHON_VERSION=3.12
67

7-
# first of all, we create a base image with an nginx which we can copy into the
8+
# first of all, we create a base image with dependencies which we can copy into the
89
# target image. For repeated rebuilds, this is much faster than apt installing
910
# each time.
1011

11-
FROM docker.io/library/debian:${DEBIAN_VERSION}-slim AS deps_base
12+
FROM ghcr.io/astral-sh/uv:python${PYTHON_VERSION}-${DEBIAN_VERSION} AS deps_base
13+
14+
# Tell apt to keep downloaded package files, as we're using cache mounts.
15+
RUN rm -f /etc/apt/apt.conf.d/docker-clean; echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' > /etc/apt/apt.conf.d/keep-cache
16+
1217
RUN \
1318
--mount=type=cache,target=/var/cache/apt,sharing=locked \
1419
--mount=type=cache,target=/var/lib/apt,sharing=locked \
1520
apt-get update -qq && \
1621
DEBIAN_FRONTEND=noninteractive apt-get install -yqq --no-install-recommends \
17-
redis-server nginx-light
22+
nginx-light
23+
24+
RUN \
25+
# remove default page
26+
rm /etc/nginx/sites-enabled/default && \
27+
# have nginx log to stderr/out
28+
ln -sf /dev/stdout /var/log/nginx/access.log && \
29+
ln -sf /dev/stderr /var/log/nginx/error.log
30+
31+
# --link-mode=copy silences a warning as uv isn't able to do hardlinks between its cache
32+
# (mounted as --mount=type=cache) and the target directory.
33+
RUN --mount=type=cache,target=/root/.cache/uv \
34+
uv pip install --link-mode=copy --prefix="/uv/usr/local" supervisor~=4.2
35+
36+
RUN mkdir -p /uv/etc/supervisor/conf.d
1837

1938
# Similarly, a base to copy the redis server from.
2039
#
@@ -27,31 +46,16 @@ FROM docker.io/library/redis:7-${DEBIAN_VERSION} AS redis_base
2746
# now build the final image, based on the the regular Synapse docker image
2847
FROM $FROM
2948

30-
# Install supervisord with uv pip instead of apt, to avoid installing a second
31-
# copy of python.
32-
# --link-mode=copy silences a warning as uv isn't able to do hardlinks between its cache
33-
# (mounted as --mount=type=cache) and the target directory.
34-
RUN \
35-
--mount=type=bind,from=ghcr.io/astral-sh/uv:0.6.8,source=/uv,target=/uv \
36-
--mount=type=cache,target=/root/.cache/uv \
37-
/uv pip install --link-mode=copy --prefix="/usr/local" supervisor~=4.2
38-
39-
RUN mkdir -p /etc/supervisor/conf.d
40-
41-
# Copy over redis and nginx
49+
# Copy over dependencies
4250
COPY --from=redis_base /usr/local/bin/redis-server /usr/local/bin
43-
51+
COPY --from=deps_base /uv /
4452
COPY --from=deps_base /usr/sbin/nginx /usr/sbin
4553
COPY --from=deps_base /usr/share/nginx /usr/share/nginx
4654
COPY --from=deps_base /usr/lib/nginx /usr/lib/nginx
4755
COPY --from=deps_base /etc/nginx /etc/nginx
48-
RUN rm /etc/nginx/sites-enabled/default
49-
RUN mkdir /var/log/nginx /var/lib/nginx
50-
RUN chown www-data /var/lib/nginx
51-
52-
# have nginx log to stderr/out
53-
RUN ln -sf /dev/stdout /var/log/nginx/access.log
54-
RUN ln -sf /dev/stderr /var/log/nginx/error.log
56+
COPY --from=deps_base /var/log/nginx /var/log/nginx
57+
# chown to allow non-root user to write to http-*-temp-path dirs
58+
COPY --from=deps_base --chown=www-data:root /var/lib/nginx /var/lib/nginx
5559

5660
# Copy Synapse worker, nginx and supervisord configuration template files
5761
COPY ./docker/conf-workers/* /conf/

0 commit comments

Comments
 (0)