Dockerfile 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 git \
  38. && rm -rf /var/lib/apt/lists/*
  39. # We install poetry in its own build stage to avoid its dependencies conflicting with
  40. # synapse's dependencies.
  41. # We use a specific commit from poetry's master branch instead of our usual 1.1.14,
  42. # to incorporate fixes to some bugs in `poetry export`. This commit corresponds to
  43. # https://github.com/python-poetry/poetry/pull/5156 and
  44. # https://github.com/python-poetry/poetry/issues/5141 ;
  45. # without it, we generate a requirements.txt with incorrect environment markers,
  46. # which causes necessary packages to be omitted when we `pip install`.
  47. #
  48. # NB: In poetry 1.2 `poetry export` will be moved into a plugin; we'll need to also
  49. # pip install poetry-plugin-export (https://github.com/python-poetry/poetry-plugin-export).
  50. RUN --mount=type=cache,target=/root/.cache/pip \
  51. pip install --user "poetry-core==1.1.0a7" "git+https://github.com/python-poetry/poetry.git@fb13b3a676f476177f7937ffa480ee5cff9a90a5"
  52. WORKDIR /synapse
  53. # Copy just what we need to run `poetry export`...
  54. COPY pyproject.toml poetry.lock /synapse/
  55. # If specified, we won't verify the hashes of dependencies.
  56. # This is only needed if the hashes of dependencies cannot be checked for some
  57. # reason, such as when a git repository is used directly as a dependency.
  58. ARG TEST_ONLY_SKIP_DEP_HASH_VERIFICATION
  59. RUN /root/.local/bin/poetry export --extras all -o /synapse/requirements.txt ${TEST_ONLY_SKIP_DEP_HASH_VERIFICATION:+--without-hashes}
  60. ###
  61. ### Stage 1: builder
  62. ###
  63. FROM docker.io/python:${PYTHON_VERSION}-slim as builder
  64. # install the OS build deps
  65. RUN \
  66. --mount=type=cache,target=/var/cache/apt,sharing=locked \
  67. --mount=type=cache,target=/var/lib/apt,sharing=locked \
  68. apt-get update -qq && apt-get install -yqq \
  69. build-essential \
  70. libffi-dev \
  71. libjpeg-dev \
  72. libpq-dev \
  73. libssl-dev \
  74. libwebp-dev \
  75. libxml++2.6-dev \
  76. libxslt1-dev \
  77. openssl \
  78. #rustc \
  79. curl \
  80. zlib1g-dev \
  81. git \
  82. && rm -rf /var/lib/apt/lists/*
  83. #RUN apt-cache policy rustc
  84. RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | bash -s -- -y
  85. # Add .cargo/bin to PATH
  86. ENV PATH="/root/.cargo/bin:${PATH}"
  87. # Check cargo is visible
  88. RUN cargo --help
  89. # To speed up rebuilds, install all of the dependencies before we copy over
  90. # the whole synapse project, so that this layer in the Docker cache can be
  91. # used while you develop on the source
  92. #
  93. # This is aiming at installing the `[tool.poetry.depdendencies]` from pyproject.toml.
  94. COPY --from=requirements /synapse/requirements.txt /synapse/
  95. RUN --mount=type=cache,target=/root/.cache/pip \
  96. pip install --prefix="/install" --no-deps --no-warn-script-location -r /synapse/requirements.txt
  97. # Copy over the rest of the synapse source code.
  98. COPY synapse /synapse/synapse/
  99. # ... and what we need to `pip install`.
  100. COPY pyproject.toml README.rst /synapse/
  101. # Install the synapse package itself.
  102. RUN pip install --prefix="/install" --no-deps --no-warn-script-location /synapse
  103. ###
  104. ### Stage 2: runtime
  105. ###
  106. FROM docker.io/python:${PYTHON_VERSION}-slim
  107. LABEL org.opencontainers.image.url='https://matrix.org/docs/projects/server/synapse'
  108. LABEL org.opencontainers.image.documentation='https://github.com/matrix-org/synapse/blob/master/docker/README.md'
  109. LABEL org.opencontainers.image.source='https://github.com/matrix-org/synapse.git'
  110. LABEL org.opencontainers.image.licenses='Apache-2.0'
  111. RUN \
  112. --mount=type=cache,target=/var/cache/apt,sharing=locked \
  113. --mount=type=cache,target=/var/lib/apt,sharing=locked \
  114. apt-get update -qq && apt-get install -yqq \
  115. curl \
  116. gosu \
  117. libjpeg62-turbo \
  118. libpq5 \
  119. libwebp6 \
  120. xmlsec1 \
  121. libjemalloc2 \
  122. libssl-dev \
  123. openssl \
  124. && rm -rf /var/lib/apt/lists/*
  125. COPY --from=builder /install /usr/local
  126. COPY ./docker/start.py /start.py
  127. COPY ./docker/conf /conf
  128. EXPOSE 8008/tcp 8009/tcp 8448/tcp
  129. ENTRYPOINT ["/start.py"]
  130. HEALTHCHECK --start-period=5s --interval=15s --timeout=5s \
  131. CMD curl -fSs http://localhost:8008/health || exit 1