|
| 1 | +# syntax=docker/dockerfile:1 |
| 2 | +# Dockerfile to build the matrixdotorg/synapse docker images. |
| 3 | +# |
| 4 | +# Note that it uses features which are only available in BuildKit - see |
| 5 | +# https://docs.docker.com/go/buildkit/ for more information. |
| 6 | +# |
| 7 | +# To build the image, run `docker build` command from the root of the |
| 8 | +# synapse repository: |
| 9 | +# |
| 10 | +# DOCKER_BUILDKIT=1 docker build -f docker/Dockerfile . |
| 11 | +# |
| 12 | +# There is an optional BASE_IMAGE build argument which sets the |
| 13 | +# base image being used, this must be a debian bullseye derivative |
| 14 | +# image with python already installed and in the $PATH. |
| 15 | +# e.g pyston/slim:2.3.3 or docker.io/python:3.9-slim (default) |
| 16 | +# |
| 17 | +# DOCKER_BUILDKIT=1 docker build -f docker/Dockerfile --build-arg BASE_IMAGE=pyston/slim:2.3.3 . |
| 18 | +# |
| 19 | + |
| 20 | +# Irritatingly, there is no blessed guide on how to distribute an application with its |
| 21 | +# poetry-managed environment in a docker image. We have opted for |
| 22 | +# `poetry export | pip install -r /dev/stdin`, but there are known bugs in |
| 23 | +# in `poetry export` whose fixes (scheduled for poetry 1.2) have yet to be released. |
| 24 | +# In case we get bitten by those bugs in the future, the recommendations here might |
| 25 | +# be useful: |
| 26 | +# https://github.com/python-poetry/poetry/discussions/1879#discussioncomment-216865 |
| 27 | +# https://stackoverflow.com/questions/53835198/integrating-python-poetry-with-docker?answertab=scoredesc |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | +ARG BASE_IMAGE=docker.io/python:3.9-slim |
| 32 | + |
| 33 | +### |
| 34 | +### Stage 0: builder |
| 35 | +### |
| 36 | +FROM $BASE_IMAGE as builder |
| 37 | + |
| 38 | +# RUN --mount is specific to buildkit and is documented at |
| 39 | +# https://github.com/moby/buildkit/blob/master/frontend/dockerfile/docs/syntax.md#build-mounts-run---mount. |
| 40 | +# Here we use it to set up a cache for apt (and below for pip), to improve |
| 41 | +# rebuild speeds on slow connections. |
| 42 | +RUN \ |
| 43 | + --mount=type=cache,target=/var/cache/apt,sharing=locked \ |
| 44 | + --mount=type=cache,target=/var/lib/apt,sharing=locked \ |
| 45 | + apt-get update && apt-get install -y \ |
| 46 | + git \ |
| 47 | + build-essential \ |
| 48 | + libffi-dev \ |
| 49 | + libjpeg-dev \ |
| 50 | + libpq-dev \ |
| 51 | + libssl-dev \ |
| 52 | + libwebp-dev \ |
| 53 | + libxml++2.6-dev \ |
| 54 | + libxslt1-dev \ |
| 55 | + openssl \ |
| 56 | + rustc \ |
| 57 | + zlib1g-dev \ |
| 58 | + && rm -rf /var/lib/apt/lists/* |
| 59 | + |
| 60 | +# We install poetry in its own prefix to avoid its dependencies conflicting with |
| 61 | +# synapse's dependencies. |
| 62 | +# We use a specific commit from poetry's master branch instead of our usual 1.1.12, |
| 63 | +# to incorporate fixes to some bugs in `poetry export`. This commit corresponds to |
| 64 | +# https://github.com/python-poetry/poetry/pull/5156 and |
| 65 | +# https://github.com/python-poetry/poetry/issues/5141 ; |
| 66 | +# without it, we generate a requirements.txt with incorrect environment markers, |
| 67 | +# which causes necessary packages to be omitted when we `pip install`. |
| 68 | +# |
| 69 | +# NB: In poetry 1.2 `poetry export` will be moved into a plugin; we'll need to also |
| 70 | +# pip install poetry-plugin-export (https://github.com/python-poetry/poetry-plugin-export). |
| 71 | +RUN --mount=type=cache,target=/root/.cache/pip \ |
| 72 | + pip install --prefix="/poetry" --no-warn-script-location \ |
| 73 | + "poetry-core==1.1.0a7" "git+https://github.com/python-poetry/poetry.git@fb13b3a676f476177f7937ffa480ee5cff9a90a5" |
| 74 | + |
| 75 | +WORKDIR /synapse |
| 76 | + |
| 77 | +# Copy just what we need to run `poetry export`... |
| 78 | +COPY pyproject.toml poetry.lock /synapse/ |
| 79 | + |
| 80 | +RUN PYTHONPATH=$(echo /poetry/lib/*/site-packages/) /poetry/bin/poetry export --extras all -o /synapse/requirements.txt |
| 81 | + |
| 82 | +# To speed up rebuilds, install all of the dependencies before we copy over |
| 83 | +# the whole synapse project, so that this layer in the Docker cache can be |
| 84 | +# used while you develop on the source |
| 85 | +# |
| 86 | +# This is aiming at installing the `[tool.poetry.depdendencies]` from pyproject.toml. |
| 87 | +RUN --mount=type=cache,target=/root/.cache/pip \ |
| 88 | + pip install --prefix="/install" --no-deps --no-warn-script-location -r /synapse/requirements.txt |
| 89 | + |
| 90 | +# Copy over the rest of the synapse source code. |
| 91 | +COPY synapse /synapse/synapse/ |
| 92 | +# ... and what we need to `pip install`. |
| 93 | +COPY README.rst /synapse/ |
| 94 | + |
| 95 | +# Install the synapse package itself. |
| 96 | +RUN pip install --prefix="/install" --no-deps --no-warn-script-location /synapse |
| 97 | + |
| 98 | +### |
| 99 | +### Stage 1: runtime |
| 100 | +### |
| 101 | +FROM $BASE_IMAGE |
| 102 | + |
| 103 | +LABEL org.opencontainers.image.url='https://matrix.org/docs/projects/server/synapse' |
| 104 | +LABEL org.opencontainers.image.documentation='https://github.com/matrix-org/synapse/blob/master/docker/README.md' |
| 105 | +LABEL org.opencontainers.image.source='https://github.com/matrix-org/synapse.git' |
| 106 | +LABEL org.opencontainers.image.licenses='Apache-2.0' |
| 107 | + |
| 108 | +RUN \ |
| 109 | + --mount=type=cache,target=/var/cache/apt,sharing=locked \ |
| 110 | + --mount=type=cache,target=/var/lib/apt,sharing=locked \ |
| 111 | + apt-get update && apt-get install -y \ |
| 112 | + curl \ |
| 113 | + gosu \ |
| 114 | + libjpeg62-turbo \ |
| 115 | + libpq5 \ |
| 116 | + libwebp6 \ |
| 117 | + xmlsec1 \ |
| 118 | + libjemalloc2 \ |
| 119 | + libssl-dev \ |
| 120 | + openssl \ |
| 121 | + && rm -rf /var/lib/apt/lists/* |
| 122 | + |
| 123 | + |
| 124 | +COPY --from=builder /install/bin /usr/local/bin |
| 125 | +# Copy the python site-packages into /install instead of /usr/local/lib as some environments use different paths |
| 126 | +# e.g pyston using /usr/lib, so we instead specify a PYTHONPATH environment variable to make everyone happy. |
| 127 | +COPY --from=builder /install/lib/*/site-packages /install |
| 128 | +ENV PYTHONPATH="/install" |
| 129 | + |
| 130 | +COPY ./docker/start.py /start.py |
| 131 | +COPY ./docker/conf /conf |
| 132 | + |
| 133 | +EXPOSE 8008/tcp 8009/tcp 8448/tcp |
| 134 | + |
| 135 | +ENTRYPOINT ["/start.py"] |
| 136 | + |
| 137 | +HEALTHCHECK --start-period=5s --interval=15s --timeout=5s \ |
| 138 | + CMD curl -fSs http://localhost:8008/health || exit 1 |
0 commit comments