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

Commit d41c465

Browse files
authored
Use buildkit's cache feature to speed up docker builds (#11691)
Having spent much of the last week attempting to run complement tests from somewhere with damp string instead of internet... something had to be done.
1 parent 338e70c commit d41c465

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed

.github/workflows/tests.yml

+2
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,8 @@ jobs:
366366
# Build initial Synapse image
367367
- run: docker build -t matrixdotorg/synapse:latest -f docker/Dockerfile .
368368
working-directory: synapse
369+
env:
370+
DOCKER_BUILDKIT: 1
369371

370372
# Build a ready-to-run Synapse image based on the initial image above.
371373
# This new image includes a config file, keys for signing and TLS, and

changelog.d/11691.misc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Use buildkit's cache feature to speed up docker builds.

docker/Dockerfile

+21-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
# Dockerfile to build the matrixdotorg/synapse docker images.
22
#
3+
# Note that it uses features which are only available in BuildKit - see
4+
# https://docs.docker.com/go/buildkit/ for more information.
5+
#
36
# To build the image, run `docker build` command from the root of the
47
# synapse repository:
58
#
6-
# docker build -f docker/Dockerfile .
9+
# DOCKER_BUILDKIT=1 docker build -f docker/Dockerfile .
710
#
811
# There is an optional PYTHON_VERSION build argument which sets the
912
# version of python to build against: for example:
1013
#
11-
# docker build -f docker/Dockerfile --build-arg PYTHON_VERSION=3.6 .
14+
# DOCKER_BUILDKIT=1 docker build -f docker/Dockerfile --build-arg PYTHON_VERSION=3.9 .
1215
#
1316

1417
ARG PYTHON_VERSION=3.8
@@ -19,7 +22,16 @@ ARG PYTHON_VERSION=3.8
1922
FROM docker.io/python:${PYTHON_VERSION}-slim as builder
2023

2124
# install the OS build deps
22-
RUN apt-get update && apt-get install -y \
25+
#
26+
# RUN --mount is specific to buildkit and is documented at
27+
# https://github.com/moby/buildkit/blob/master/frontend/dockerfile/docs/syntax.md#build-mounts-run---mount.
28+
# Here we use it to set up a cache for apt, to improve rebuild speeds on
29+
# slow connections.
30+
#
31+
RUN \
32+
--mount=type=cache,target=/var/cache/apt,sharing=locked \
33+
--mount=type=cache,target=/var/lib/apt,sharing=locked \
34+
apt-get update && apt-get install -y \
2335
build-essential \
2436
libffi-dev \
2537
libjpeg-dev \
@@ -44,7 +56,8 @@ COPY synapse/python_dependencies.py /synapse/synapse/python_dependencies.py
4456
# used while you develop on the source
4557
#
4658
# This is aiming at installing the `install_requires` and `extras_require` from `setup.py`
47-
RUN pip install --prefix="/install" --no-warn-script-location \
59+
RUN --mount=type=cache,target=/root/.cache/pip \
60+
pip install --prefix="/install" --no-warn-script-location \
4861
/synapse[all]
4962

5063
# Copy over the rest of the project
@@ -66,7 +79,10 @@ LABEL org.opencontainers.image.documentation='https://github.com/matrix-org/syna
6679
LABEL org.opencontainers.image.source='https://github.com/matrix-org/synapse.git'
6780
LABEL org.opencontainers.image.licenses='Apache-2.0'
6881

69-
RUN apt-get update && apt-get install -y \
82+
RUN \
83+
--mount=type=cache,target=/var/cache/apt,sharing=locked \
84+
--mount=type=cache,target=/var/lib/apt,sharing=locked \
85+
apt-get update && apt-get install -y \
7086
curl \
7187
gosu \
7288
libjpeg62-turbo \

scripts-dev/complement.sh

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
# Exit if a line returns a non-zero exit code
2424
set -e
2525

26+
# enable buildkit for the docker builds
27+
export DOCKER_BUILDKIT=1
28+
2629
# Change to the repository root
2730
cd "$(dirname $0)/.."
2831

@@ -65,4 +68,5 @@ if [[ -n "$1" ]]; then
6568
fi
6669

6770
# Run the tests!
71+
echo "Images built; running complement"
6872
go test -v -tags synapse_blacklist,msc2403 -count=1 $EXTRA_COMPLEMENT_ARGS ./tests/...

0 commit comments

Comments
 (0)