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

Docker: support passing additional commandline args to synapse #8390

Merged
merged 9 commits into from
Oct 11, 2020
1 change: 1 addition & 0 deletions changelog.d/8390.docker
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add support for passing commandline args to the synapse process. Contributed by @samuel-p.
14 changes: 13 additions & 1 deletion docker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ docker logs synapse
If all is well, you should now be able to connect to http://localhost:8008 and
see a confirmation message.

The following environment variables are supported in run mode:
The following environment variables are supported in `run` mode:

* `SYNAPSE_CONFIG_DIR`: where additional config files are stored. Defaults to
`/data`.
Expand All @@ -94,6 +94,18 @@ The following environment variables are supported in run mode:
* `UID`, `GID`: the user and group id to run Synapse as. Defaults to `991`, `991`.
* `TZ`: the [timezone](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) the container will run with. Defaults to `UTC`.

For more complex setups (e.g. for workers) you can also pass your args directly to synapse using `run` mode. For example like this:

```
docker run -d --name synapse \
--mount type=volume,src=synapse-data,dst=/data \
-p 8008:8008 \
matrixdotorg/synapse:latest run \
-m synapse.app.generic_worker \
--config-path=/data/homeserver.yaml \
--config-path=/data/generic_worker.yaml
```

samuel-p marked this conversation as resolved.
Show resolved Hide resolved
## Generating an (admin) user

After synapse is running, you may wish to create a user via `register_new_matrix_user`.
Expand Down
64 changes: 38 additions & 26 deletions docker/start.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def run_generate_config(environ, ownership):


def main(args, environ):
mode = args[1] if len(args) > 1 else None
mode = args[1] if len(args) > 1 else "run"
desired_uid = int(environ.get("UID", "991"))
desired_gid = int(environ.get("GID", "991"))
synapse_worker = environ.get("SYNAPSE_WORKER", "synapse.app.homeserver")
Expand All @@ -205,41 +205,53 @@ def main(args, environ):
config_dir, config_path, environ, ownership
)

if mode is not None:
error("Unknown execution mode '%s'" % (mode,))
if mode == "run":
samuel-p marked this conversation as resolved.
Show resolved Hide resolved
args = args[2:]

config_dir = environ.get("SYNAPSE_CONFIG_DIR", "/data")
config_path = environ.get("SYNAPSE_CONFIG_PATH", config_dir + "/homeserver.yaml")
if "-m" not in args:
args = ["-m", synapse_worker] + args

if not os.path.exists(config_path):
if "SYNAPSE_SERVER_NAME" in environ:
error(
"""\
Config file '%s' does not exist.
# if there are no config files passed to synapse, try adding the default file
if not any(p.startswith("--config-path") for p in args):
samuel-p marked this conversation as resolved.
Show resolved Hide resolved
config_dir = environ.get("SYNAPSE_CONFIG_DIR", "/data")
config_path = environ.get(
"SYNAPSE_CONFIG_PATH", config_dir + "/homeserver.yaml"
)

if not os.path.exists(config_path):
if "SYNAPSE_SERVER_NAME" in environ:
error(
"""\
Config file '%s' does not exist.

The synapse docker image no longer supports generating a config file on-the-fly
based on environment variables. You can migrate to a static config file by
running with 'migrate_config'. See the README for more details.
"""
% (config_path,)
)
% (config_path,)
)

error(
"Config file '%s' does not exist. You should either create a new "
"config file by running with the `generate` argument (and then edit "
"the resulting file before restarting) or specify the path to an "
"existing config file with the SYNAPSE_CONFIG_PATH variable."
% (config_path,)
)
error(
"Config file '%s' does not exist. You should either create a new "
"config file by running with the `generate` argument (and then edit "
"the resulting file before restarting) or specify the path to an "
"existing config file with the SYNAPSE_CONFIG_PATH variable."
% (config_path,)
)

log("Starting synapse with config file " + config_path)
args += ["--config-path", config_path]

args = ["python", "-m", synapse_worker, "--config-path", config_path]
if ownership is not None:
args = ["gosu", ownership] + args
os.execv("/usr/sbin/gosu", args)
else:
os.execv("/usr/local/bin/python", args)
log("Starting synapse with args " + " ".join(args))

args = ["python"] + args
if ownership is not None:
args = ["gosu", ownership] + args
os.execv("/usr/sbin/gosu", args)
else:
os.execv("/usr/local/bin/python", args)
return

error("Unknown execution mode '%s'" % (mode,))


if __name__ == "__main__":
Expand Down