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

Official Docker image #2482

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Copyright 2017 Vector Creations Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

FROM phusion/baseimage:0.9.22
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May I ask why this is used as a base image? As there is only one process running in this image (Synapse), afaik there is no advantage in using the extra supervisor employed by phusion-baseimage. The official ubuntu ( or debian) base image will probably be enough.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1. The ubuntu base image is better maintained, smaller in size, and in 10X broader usage. It seems most of the things phusion is solving for are no longer issues.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If an additonal opinion would be asked here, we're also running from debian:jessie in https://github.com/allmende/docker-synapse/blob/master/Dockerfile#L1 without issues.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alpine can also be used as the base, see https://github.com/ptman/synapse-docker


COPY ./ /synapse/source/

RUN apt-get update -y \
&& DEBIAN_FRONTEND=noninteractive apt-get upgrade -y \
&& DEBIAN_FRONTEND=noninteractive apt-get install -y \
build-essential \
libffi-dev \
libjpeg-dev \
libpq-dev \
libssl-dev \
libxslt1-dev \
python-pip \
python-setuptools \
python-virtualenv \
python2.7-dev \
sqlite3 \
&& virtualenv -p python2.7 /synapse \
&& . /synapse/bin/activate \
&& pip install --upgrade pip \
&& pip install --upgrade setuptools \
&& pip install --upgrade psycopg2 \
&& cd /synapse/source \
&& pip install --upgrade ./ \
&& cd / \
&& rm -rf /synapse/source \
&& apt-get autoremove -y \
build-essential \
libffi-dev \
libjpeg-dev \
libpq-dev \
libssl-dev \
libxslt1-dev \
python2.7-dev \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

COPY docker/rootfs/ /
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since L17 does a copy of ./, then AFAICT, you can replace this line by a "cp -r /synapse/source/docker/rootfs/* /" added to the RUN statement above (somewhere before the rm-rf /synapse/source), to avoid another layer in the image.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though we're discussing whether to have this Dockerfile in-tree or out-of-tree. In-tree means it's easier to build an image from dev code. Out-of-tree feels a bit cleaner and it can be maintained separate from synapse releases.


VOLUME /synapse/config/
VOLUME /synapse/data/

CMD ["/sbin/my_init"]
70 changes: 70 additions & 0 deletions docker/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Synapse Docker

## Build

Build the docker image with the `docker build` command from the root of the synapse repository.

```
docker build -t matrixdotorg/synapse:v0.22.1 .
```

The `-t` option sets the image tag. Official images are tagged `matrixdotorg/synapse:<version>` where `<version>` is the same as the release tag in the synapse git repository.

## Configure

Synapse provides a command for generating homeserver configuration files. These are a good starting point for setting up your own deployment.

The documentation below will refer to a `CONFIG_PATH` shell variable. This is a path to a directory where synapse configuration will be stored. It needs to be mapped into the container as a volume at `/synapse/config/` as can be seen in the example `docker run` command.

Docker container environment variables:
* `GENERATE_CONFIG` - Set this to any non-empty string, such as `yes`, to trigger generation of configuration files. Existing files in the `CONFIG_PATH` will **not** be overwritten.
* `POSTGRES_DATABASE` - The database name for the synapse postgres database. [default: `synapse`]
* `POSTGRES_HOST` - The host of the postgres database if you wish to use postgresql instead of sqlite3. [default: `postgres` which is useful when using a container on the same docker network in a compose file where the postgres service is called `postgres`] **NOTE**: `localhost` and `127.0.0.1` refer to the container itself unless running the container with `host` networking.
* `POSTGRES_PASSWORD` - The password for the synapse postgres database. **If this is set then postgres will be used instead of sqlite3.** [default: none] **NOTE**: You are highly encouraged to use postgresql! Please use the compose file to make it easier to deploy.
* `POSTGRES_USER` - The user for the synapse postgres database. [default: `postgres`]
* `REPORT_STATS` - Whether to send anonymous usage statistics back to the Matrix project which helps us to get funding! Must be `yes` or `no`. [default: `yes`]
* `SERVER_NAME` - The domain used for the Matrix homeserver. If you intend to run this synapse instance on a public domain, use that domain. [default: `localhost`]

```
CONFIG_PATH=/my/magical/config/path/
mkdir -p ${CONFIG_PATH}
docker run \
--rm \
-e GENERATE_CONFIG=yes \
-e POSTGRES_PASSWORD=MyVerySecretPassword \
-e REPORT_STATS=yes \
-e SERVER_NAME=example.com \
-v ${CONFIG_PATH}:/synapse/config/ \
matrixdotorg/synapse:v0.22.1
```

This will create a temporary container from the image and use the synapse code for generating configuration files and TLS keys and certificates for the specified `SERVER_NAME` domain. The files are written to `CONFIG_PATH`.

## Run

**NOTE**: If you are not using postgresql and are using sqlite3 as your database, you will need to make a directory to store the sqlite3 database file in and then mount this volume into the container at `/synapse/data/`. As it is so easy to use postgresql, when using Docker containers, this is not documented to somewhat discourage it. Choose a `POSTGRES_PASSWORD` instead.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't the data directory still needed even with postgresql, for the content repository? (In fact, I don't see where media_store_path gets set when the config gets generated.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good points. That needs fixing.


### Docker Compose

A `docker-compose.yaml` file is included to ease deployment of the basic synapse and postgres setup. Remember to set a `POSTGRES_PASSWORD` when generating your configuration above. You will need it for running the containers in the composition.

From the `docker/` subdirectory of the synapse repository:
```
CONFIG_PATH=/my/magical/config/path/
POSTGRES_PASSWORD=MyVerySecretPassword \
docker-compose \
-p synapse \
up -d
```

### Docker

Note that the following is just a guideline and you may need to add parameters to the docker run command to account for the network situation with your postgres database.

```
docker run \
-d \
--name synapse \
-v ${CONFIG_PATH}:/synapse/config/ \
matrixdotorg/synapse:v0.22.1
```
39 changes: 39 additions & 0 deletions docker/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright 2017 Vector Creations Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

version: '3'

services:
postgres:
image: postgres:9.6.5-alpine
environment:
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: synapse
expose:
- 5432
restart: unless-stopped
volumes:
- postgres-data:/var/lib/postgresql/data/

synapse:
image: matrixdotorg/synapse:v0.22.1
ports:
- 8008:8008
- 8448:8448
restart: unless-stopped
volumes:
- ${CONFIG_PATH}:/synapse/config/

volumes:
postgres-data:
17 changes: 17 additions & 0 deletions docker/rootfs/etc/service/synapse/finish
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash
#
# Copyright 2017 Vector Creations Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

kill -TERM 1
75 changes: 75 additions & 0 deletions docker/rootfs/etc/service/synapse/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/bin/bash
#
# Copyright 2017 Vector Creations Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -e

: ${CONFIG_PATH:="/synapse/config"}
: ${POSTGRES_DATABASE:="synapse"}
: ${POSTGRES_HOST:="postgres"}
: ${POSTGRES_USER:="postgres"}
: ${REPORT_STATS:="yes"}
: ${SERVER_NAME:="localhost"}

DATABASE_CONFIG_PATH="${CONFIG_PATH}/database.yaml"
HOMESERVER_CONFIG_PATH="${CONFIG_PATH}/homeserver.yaml"
SYNAPSE_COMMAND="python -m synapse.app.homeserver"

. /synapse/bin/activate
cd /synapse

if [[ -n "${GENERATE_CONFIG}" ]]; then
${SYNAPSE_COMMAND} \
--server-name ${SERVER_NAME} \
--config-path ${HOMESERVER_CONFIG_PATH} \
--generate-config \
--report-stats=${REPORT_STATS}

if [[ -f "${DATABASE_CONFIG_PATH}" ]]; then
echo "Config file '${DATABASE_CONFIG_PATH}' already exists. Remove it if you want it to be generated."
else
echo "Generating ${DATABASE_CONFIG_PATH}..."
if [[ -n "${POSTGRES_PASSWORD}" ]]; then
(cat > ${DATABASE_CONFIG_PATH}) <<EOF
database:
name: psycopg2
args:
host: ${POSTGRES_HOST}
user: ${POSTGRES_USER}
password: ${POSTGRES_PASSWORD}
database: ${POSTGRES_DATABASE}
cp_min: 5
cp_max: 10
EOF
else
(cat > ${DATABASE_CONFIG_PATH}) <<EOF
database:
name: "sqlite3"
args:
database: "/synapse/data/homeserver.db"
EOF
fi
cat ${DATABASE_CONFIG_PATH} | grep -v password
fi

exit 0
fi

COMMAND="${SYNAPSE_COMMAND} --config-path ${HOMESERVER_CONFIG_PATH}"
if [[ -r "${DATABASE_CONFIG_PATH}" ]]; then
COMMAND="${COMMAND} --config-path ${DATABASE_CONFIG_PATH}"
fi

exec ${COMMAND}