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

Commit 0a778c1

Browse files
Make pip install faster in Docker build for Complement testing (#9610)
Make pip install faster in Docker build for [Complement](https://github.com/matrix-org/complement) testing. If files have changed in a `COPY` command, Docker will invalidate all of the layers below. So I changed the order of operations to install all dependencies before we `COPY synapse /synapse/synapse/`. This allows Docker to use our cached layer of dependencies even when we change the source of Synapse and speed up builds dramatically! `53.5s` -> `3.7s` builds 🤘 As an alternative, I did try using BuildKit caches but this still took 30 seconds overall on that step. 15 seconds to gather the dependencies from the cache and another 15 seconds to `Installing collected packages`. Fix #9364
1 parent 7c8402d commit 0a778c1

File tree

2 files changed

+42
-43
lines changed

2 files changed

+42
-43
lines changed

changelog.d/9610.docker

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Speed up Docker builds and make it nicer to test against Complement while developing (install all dependencies before copying the project).

docker/Dockerfile

Lines changed: 41 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -25,42 +25,40 @@ LABEL org.opencontainers.image.licenses='Apache-2.0'
2525

2626
# install the OS build deps
2727
RUN apt-get update && apt-get install -y \
28-
build-essential \
29-
libffi-dev \
30-
libjpeg-dev \
31-
libpq-dev \
32-
libssl-dev \
33-
libwebp-dev \
34-
libxml++2.6-dev \
35-
libxslt1-dev \
36-
openssl \
37-
rustc \
38-
zlib1g-dev \
39-
&& rm -rf /var/lib/apt/lists/*
40-
41-
# Build dependencies that are not available as wheels, to speed up rebuilds
42-
RUN pip install --prefix="/install" --no-warn-script-location \
43-
cryptography \
44-
frozendict \
45-
jaeger-client \
46-
opentracing \
47-
# Match the version constraints of Synapse
48-
"prometheus_client>=0.4.0" \
49-
psycopg2 \
50-
pycparser \
51-
pyrsistent \
52-
pyyaml \
53-
simplejson \
54-
threadloop \
55-
thrift
56-
57-
# now install synapse and all of the python deps to /install.
58-
COPY synapse /synapse/synapse/
28+
build-essential \
29+
libffi-dev \
30+
libjpeg-dev \
31+
libpq-dev \
32+
libssl-dev \
33+
libwebp-dev \
34+
libxml++2.6-dev \
35+
libxslt1-dev \
36+
openssl \
37+
rustc \
38+
zlib1g-dev \
39+
&& rm -rf /var/lib/apt/lists/*
40+
41+
# Copy just what we need to pip install
5942
COPY scripts /synapse/scripts/
6043
COPY MANIFEST.in README.rst setup.py synctl /synapse/
44+
COPY synapse/__init__.py /synapse/synapse/__init__.py
45+
COPY synapse/python_dependencies.py /synapse/synapse/python_dependencies.py
6146

47+
# To speed up rebuilds, install all of the dependencies before we copy over
48+
# the whole synapse project so that we this layer in the Docker cache can be
49+
# used while you develop on the source
50+
#
51+
# This is aiming at installing the `install_requires` and `extras_require` from `setup.py`
6252
RUN pip install --prefix="/install" --no-warn-script-location \
63-
/synapse[all]
53+
/synapse[all]
54+
55+
# Copy over the rest of the project
56+
COPY synapse /synapse/synapse/
57+
58+
# Install the synapse package itself and all of its children packages.
59+
#
60+
# This is aiming at installing only the `packages=find_packages(...)` from `setup.py
61+
RUN pip install --prefix="/install" --no-deps --no-warn-script-location /synapse
6462

6563
###
6664
### Stage 1: runtime
@@ -69,16 +67,16 @@ RUN pip install --prefix="/install" --no-warn-script-location \
6967
FROM docker.io/python:${PYTHON_VERSION}-slim
7068

7169
RUN apt-get update && apt-get install -y \
72-
curl \
73-
gosu \
74-
libjpeg62-turbo \
75-
libpq5 \
76-
libwebp6 \
77-
xmlsec1 \
78-
libjemalloc2 \
79-
libssl-dev \
80-
openssl \
81-
&& rm -rf /var/lib/apt/lists/*
70+
curl \
71+
gosu \
72+
libjpeg62-turbo \
73+
libpq5 \
74+
libwebp6 \
75+
xmlsec1 \
76+
libjemalloc2 \
77+
libssl-dev \
78+
openssl \
79+
&& rm -rf /var/lib/apt/lists/*
8280

8381
COPY --from=builder /install /usr/local
8482
COPY ./docker/start.py /start.py
@@ -91,4 +89,4 @@ EXPOSE 8008/tcp 8009/tcp 8448/tcp
9189
ENTRYPOINT ["/start.py"]
9290

9391
HEALTHCHECK --interval=1m --timeout=5s \
94-
CMD curl -fSs http://localhost:8008/health || exit 1
92+
CMD curl -fSs http://localhost:8008/health || exit 1

0 commit comments

Comments
 (0)