tech_note

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

synapsematrix_on_docker_compose.md (6624B)


      1 ---
      2 title: Synapse(Matrix) on Docker Compose
      3 tags:
      4 - docker
      5 - matrix
      6 - selfhosted
      7 - synapse
      8 private: false
      9 updated_at: 2025-11-10 21:49
     10 id: null
     11 organization_url_name: null
     12 slide: false
     13 ---
     14 # 想定読者
     15 - Synapseのバックエンドをセルフホステッドする人
     16 - まともにCLI onlyのサーバを運用する人
     17 # Ref
     18 - [公式のDockerComposeについてのドキュメント](https://github.com/element-hq/synapse/tree/develop/contrib/docker)
     19 - [EclipseのSynapseリポジトリ](https://gitlab.eclipse.org/eclipsefdn/it/releng/chat-service/chat-service-provisioner)
     20 
     21 ## edit docker-compose.yaml
     22 
     23 docker-compose.yamlを完全に設定します。
     24 生成コマンドによって出力されたファイル数にはシークレット鍵等々の情報が記載されるので、あとからdocker-compose.yamlを変更することは非常に推奨されません。
     25 サンプル
     26 
     27 ```YAML
     28 # This compose file is compatible with Compose itself, it might need some
     29 # adjustments to run properly with stack.
     30 
     31 services:
     32 
     33   synapse:
     34     build:
     35       context: ../..
     36       dockerfile: docker/Dockerfile
     37     image: docker.io/matrixdotorg/synapse:latest
     38     # Since synapse does not retry to connect to the database, restart upon
     39     # failure
     40     restart: unless-stopped
     41     # See the readme for a full documentation of the environment settings
     42     # NOTE: You must edit homeserver.yaml to use postgres, it defaults to sqlite
     43     environment:
     44       - SYNAPSE_CONFIG_PATH=/data/homeserver.yaml
     45     volumes:
     46       # You may either store all the files in a local folder
     47       - ./files:/data
     48       # .. or you may split this between different storage points
     49       # - ./files:/data
     50       # - /path/to/ssd:/data/uploads
     51       # - /path/to/large_hdd:/data/media
     52     depends_on:
     53       - db
     54     # In order to expose Synapse, remove one of the following, you might for
     55     # instance expose the TLS port directly:
     56     ports:
     57       - 8448:8448/tcp
     58       - 8008:8008/tcp
     59 
     60   db:
     61     image: docker.io/postgres:15-alpine
     62     # Change that password, of course!
     63     environment:
     64       - POSTGRES_USER=synapse_user
     65       - POSTGRES_PASSWORD=changeme
     66       # ensure the database gets created correctly
     67       # https://element-hq.github.io/synapse/latest/postgres.html#set-up-database
     68       - POSTGRES_INITDB_ARGS=--encoding=UTF-8 --lc-collate=C --lc-ctype=C
     69     volumes:
     70       # You may store the database tables in a local folder..
     71       - ./schemas:/var/lib/postgresql/data
     72       # .. or store them on some high performance storage for better results
     73       # - /path/to/ssd/storage:/var/lib/postgresql/data
     74 ```
     75 
     76 ## generate homeserver files
     77 
     78 次にこのdocker-composeファイルを使って必要なファイル群を生成します。
     79 サーバー名のところは適宜書き換えてください。
     80 
     81 ```
     82 docker compose run --rm -e SYNAPSE_SERVER_NAME=my.matrix.host -e SYNAPSE_REPORT_STATS=yes synapse generate
     83 ```
     84 
     85 ## edit homeserver.yaml
     86 
     87 生成された`file`フォルダの中にあるhomeserver.yamlに必要な設定を書き込んでいきます。
     88 少なくとも必要な設定は以下です。
     89 
     90 ```diff
     91 - listeners:
     92 -   - port: 8008
     93 -     tls: false
     94 -     type: http
     95 -     x_forwarded: true
     96 -     resources:
     97 -       - names: [client,federation]
     98 -         compress: false
     99 
    100 + listeners:
    101 +   - port: 8008
    102 +     tls: false
    103 +     type: http
    104 +     x_forwarded: true
    105 +     resources:
    106 +       - names: [client]
    107 +         compress: false
    108 + 
    109 +   - port: 8448
    110 +     tls: false
    111 +     type: http
    112 +     x_forwarded: true
    113 +     resources:
    114 +       - names: [federation]
    115 +         compress: false
    116 
    117 -----------------------------------
    118 
    119 - database: 
    120 -   name: sqlite3
    121 -   
    122 
    123 + database:
    124 +   name: psycopg2
    125 +   args:
    126 +     user: synapse_user
    127 +     password: changeme
    128 +     dbname: synapse_user
    129 +     host: db
    130 +     cp_min: 5
    131 +     cp_max: 10
    132 ```
    133 
    134 この設定で細かなSynapseの動作を設定します。
    135 たとえばTokenの所持者のみ自己登録を可能にするなど。
    136 詳しい設定は公式ドキュメントを見てください。
    137 [Configuration Manual](https://element-hq.github.io/synapse/latest/usage/configuration/config_documentation.html)
    138 
    139 ### deploy synapse
    140 
    141 これらの設定が終了すれば、docker-compose.yamlのあるディレクトリに戻って、サーバーのデプロイを実行します。
    142 
    143 ```
    144 sudo docker compose up -d
    145 ```
    146 
    147 サーバーが無事に動いているかのテストは、curlを使うことで可能です。
    148 
    149 ```
    150 curl localhost:8008
    151 ```
    152 
    153 ```
    154 curl localhost:8448
    155 ```
    156 
    157 成功している場合の返答は以下のようになります。
    158 
    159 ```
    160 $ curl localhost:8008
    161 <html>
    162     <head>
    163         <meta http-equiv="refresh" content="0;URL=/_matrix/static">
    164     </head>
    165     <body bgcolor="#FFFFFF" text="#000000">
    166     <a href="/_matrix/static">click here</a>
    167     </body>
    168 </html>
    169 $ curl localhost:8448
    170 <html>
    171   <head><title>404 - No Such Resource</title></head>
    172   <body>
    173     <h1>No Such Resource</h1>
    174     <p>Sorry. No luck finding that resource.</p>
    175   </body>
    176 </html>
    177 ```
    178 
    179 ## Setting Cloudflare Tunnel
    180 
    181 Cloudflare One のダッシュボードからnetwork以下にあるtunnel項目を開き、tunnelの設定を開きます。
    182 「公開されたアプリケーション」タブに移動し、synapseのホスト名(とfederation用のホスト名)を追加します。
    183 フェデレーション用のホスト名にはhttpHostHeaderをsynapseのホスト名に設定します。
    184 
    185 ## .well-known/matrixの設定
    186 
    187 Cloudflare workersで適当なものを立ち上げます。
    188 worker.js
    189 
    190 ```JavaScript
    191 const HOMESERVER_URL = "https://my.matrix.host.net:443";
    192 const IDENTITY_SERVER_URL = "https://vector.im";
    193 const FEDERATION_SERVER = "my.matrix.host.net:443";
    194 
    195 export default {
    196   async fetch(request, env) {
    197     const path = new URL(request.url).pathname;
    198     switch (path) {
    199       case "/.well-known/matrix/client":
    200         return new Response(
    201           `{"m.homeserver": {"base_url": "${HOMESERVER_URL}"},"m.identity_server": {"base_url": "${IDENTITY_SERVER_URL}"}}`
    202         );
    203       case "/.well-known/matrix/server":
    204         return new Response(`{"m.server": "${FEDERATION_SERVER}"}`);
    205       default:
    206         return new Response("Invalid request");
    207     }
    208   },
    209 };
    210 ```
    211 
    212 適宜、以下のページに示されているクライアント向けにE2EEを無効化する設定を追加してください。
    213 
    214  https://github.com/element-hq/element-web/blob/develop/docs/e2ee.md