You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

172 lines
5.9 KiB

  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 PYTHON_VERSION build argument which sets the
  13. # version of python to build against: for example:
  14. #
  15. # DOCKER_BUILDKIT=1 docker build -f docker/Dockerfile --build-arg PYTHON_VERSION=3.10 .
  16. #
  17. # Irritatingly, there is no blessed guide on how to distribute an application with its
  18. # poetry-managed environment in a docker image. We have opted for
  19. # `poetry export | pip install -r /dev/stdin`, but there are known bugs in
  20. # in `poetry export` whose fixes (scheduled for poetry 1.2) have yet to be released.
  21. # In case we get bitten by those bugs in the future, the recommendations here might
  22. # be useful:
  23. # https://github.com/python-poetry/poetry/discussions/1879#discussioncomment-216865
  24. # https://stackoverflow.com/questions/53835198/integrating-python-poetry-with-docker?answertab=scoredesc
  25. ARG PYTHON_VERSION=3.9
  26. ###
  27. ### Stage 0: generate requirements.txt
  28. ###
  29. FROM docker.io/python:${PYTHON_VERSION}-slim as requirements
  30. # RUN --mount is specific to buildkit and is documented at
  31. # https://github.com/moby/buildkit/blob/master/frontend/dockerfile/docs/syntax.md#build-mounts-run---mount.
  32. # Here we use it to set up a cache for apt (and below for pip), to improve
  33. # rebuild speeds on slow connections.
  34. RUN \
  35. --mount=type=cache,target=/var/cache/apt,sharing=locked \
  36. --mount=type=cache,target=/var/lib/apt,sharing=locked \
  37. apt-get update -qq && apt-get install -yqq \
  38. build-essential cargo git libffi-dev libssl-dev \
  39. && rm -rf /var/lib/apt/lists/*
  40. # We install poetry in its own build stage to avoid its dependencies conflicting with
  41. # synapse's dependencies.
  42. RUN --mount=type=cache,target=/root/.cache/pip \
  43. pip install --user "poetry==1.2.0"
  44. WORKDIR /synapse
  45. # Copy just what we need to run `poetry export`...
  46. COPY pyproject.toml poetry.lock /synapse/
  47. # If specified, we won't verify the hashes of dependencies.
  48. # This is only needed if the hashes of dependencies cannot be checked for some
  49. # reason, such as when a git repository is used directly as a dependency.
  50. ARG TEST_ONLY_SKIP_DEP_HASH_VERIFICATION
  51. # If specified, we won't use the Poetry lockfile.
  52. # Instead, we'll just install what a regular `pip install` would from PyPI.
  53. ARG TEST_ONLY_IGNORE_POETRY_LOCKFILE
  54. # Export the dependencies, but only if we're actually going to use the Poetry lockfile.
  55. # Otherwise, just create an empty requirements file so that the Dockerfile can
  56. # proceed.
  57. RUN if [ -z "$TEST_ONLY_IGNORE_POETRY_LOCKFILE" ]; then \
  58. /root/.local/bin/poetry export --extras all -o /synapse/requirements.txt ${TEST_ONLY_SKIP_DEP_HASH_VERIFICATION:+--without-hashes}; \
  59. else \
  60. touch /synapse/requirements.txt; \
  61. fi
  62. ###
  63. ### Stage 1: builder
  64. ###
  65. FROM docker.io/python:${PYTHON_VERSION}-slim as builder
  66. # install the OS build deps
  67. RUN \
  68. --mount=type=cache,target=/var/cache/apt,sharing=locked \
  69. --mount=type=cache,target=/var/lib/apt,sharing=locked \
  70. apt-get update -qq && apt-get install -yqq \
  71. build-essential \
  72. libffi-dev \
  73. libjpeg-dev \
  74. libpq-dev \
  75. libssl-dev \
  76. libwebp-dev \
  77. libxml++2.6-dev \
  78. libxslt1-dev \
  79. openssl \
  80. zlib1g-dev \
  81. git \
  82. curl \
  83. && rm -rf /var/lib/apt/lists/*
  84. # Install rust and ensure its in the PATH
  85. ENV RUSTUP_HOME=/rust
  86. ENV CARGO_HOME=/cargo
  87. ENV PATH=/cargo/bin:/rust/bin:$PATH
  88. RUN mkdir /rust /cargo
  89. RUN curl -sSf https://sh.rustup.rs | sh -s -- -y --no-modify-path --default-toolchain stable
  90. # To speed up rebuilds, install all of the dependencies before we copy over
  91. # the whole synapse project, so that this layer in the Docker cache can be
  92. # used while you develop on the source
  93. #
  94. # This is aiming at installing the `[tool.poetry.depdendencies]` from pyproject.toml.
  95. COPY --from=requirements /synapse/requirements.txt /synapse/
  96. RUN --mount=type=cache,target=/root/.cache/pip \
  97. pip install --prefix="/install" --no-deps --no-warn-script-location -r /synapse/requirements.txt
  98. # Copy over the rest of the synapse source code.
  99. COPY synapse /synapse/synapse/
  100. COPY rust /synapse/rust/
  101. # ... and what we need to `pip install`.
  102. COPY pyproject.toml README.rst build_rust.py /synapse/
  103. # Repeat of earlier build argument declaration, as this is a new build stage.
  104. ARG TEST_ONLY_IGNORE_POETRY_LOCKFILE
  105. # Install the synapse package itself.
  106. # If we have populated requirements.txt, we don't install any dependencies
  107. # as we should already have those from the previous `pip install` step.
  108. RUN if [ -z "$TEST_ONLY_IGNORE_POETRY_LOCKFILE" ]; then \
  109. pip install --prefix="/install" --no-deps --no-warn-script-location /synapse[all]; \
  110. else \
  111. pip install --prefix="/install" --no-warn-script-location /synapse[all]; \
  112. fi
  113. ###
  114. ### Stage 2: runtime
  115. ###
  116. FROM docker.io/python:${PYTHON_VERSION}-slim
  117. LABEL org.opencontainers.image.url='https://matrix.org/docs/projects/server/synapse'
  118. LABEL org.opencontainers.image.documentation='https://github.com/matrix-org/synapse/blob/master/docker/README.md'
  119. LABEL org.opencontainers.image.source='https://github.com/matrix-org/synapse.git'
  120. LABEL org.opencontainers.image.licenses='Apache-2.0'
  121. RUN \
  122. --mount=type=cache,target=/var/cache/apt,sharing=locked \
  123. --mount=type=cache,target=/var/lib/apt,sharing=locked \
  124. apt-get update -qq && apt-get install -yqq \
  125. curl \
  126. gosu \
  127. libjpeg62-turbo \
  128. libpq5 \
  129. libwebp6 \
  130. xmlsec1 \
  131. libjemalloc2 \
  132. libssl-dev \
  133. openssl \
  134. && rm -rf /var/lib/apt/lists/*
  135. COPY --from=builder /install /usr/local
  136. COPY ./docker/start.py /start.py
  137. COPY ./docker/conf /conf
  138. EXPOSE 8008/tcp 8009/tcp 8448/tcp
  139. ENTRYPOINT ["/start.py"]
  140. HEALTHCHECK --start-period=5s --interval=15s --timeout=5s \
  141. CMD curl -fSs http://localhost:8008/health || exit 1