Ubuntu 20.04にDocker環境でMattermost Team版を作る

サーバー

以前もやりましたが、違うバージョンのDockerコンテナを利用してMattermost環境を作成しました。

Docker利用

DockerのSnapパッケージ

Ubuntu 20.04でインストール時にDockerを選択すると、git、docker、docker-composeはすでにインストールされています。ただ、インストール先が普通と少し違うようです。

/snap/docker/curent/binに実行ファイルがありましたが、他の場所からも参照できるので真のインストール先はわかりません。新しいパッケージシステムのSNAPを使っているようなので、基本的には/snap/binで見れば良いようです。

— .env 2021-12-22 18:01:38.087335640 +0000
+++ env.example 2021-12-22 17:55:24.875077525 +0000
@@ -59,7 +59,7 @@ MATTERMOST_BLEVE_INDEXES_PATH=./volumes/
MM_BLEVESETTINGS_INDEXDIR=/mattermost/bleve-indexes

## This will be ‘mattermost-enterprise-edition’ or ‘mattermost-team-edition’ based on the version of Mattermost you’re installing.
-MATTERMOST_IMAGE=mattermost-team-edition
+MATTERMOST_IMAGE=mattermost-enterprise-edition
MATTERMOST_IMAGE_TAG=5.39

## Make Mattermost container readonly. This interferes with the regeneration of root.html inside the container. Only use

いまいち、サービス登録などが良くわからず、仮想マシンインストール時にdockerを入れずにやり直しました。

dockerインストール

公式ドキュメントを見ながら、インストールを行います。

Install Docker Engine on Ubuntu
Jumpstart your client-side server applications with Docker Engine on Ubuntu. This guide details prerequisites and multiple methods to install.
sudo apt update
sudo apt upgrade -y
sudo apt remove docker docker-engine docker.io containerd runc
sudo apt install -y ca-certificates curl gnupg lsb-release #元から入っていた
curl -fsSL https://download.docker.com/linux/ubuntu/gpg \
| sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io
sudo docker run hello-world

これで、テスト用プログラム、hello-worldが動作します。

Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the “hello-world” image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/

For more examples and ideas, visit:
https://docs.docker.com/get-started/

作動して、メッセージを発した後、コンテナは停止しますが、残っていくので、削除までしてみます。

Ubuntu 20.04へのDockerのインストールおよび使用方法 | DigitalOcean
Dockerは、コンテナででアプリケーションプロセスを管理する過程を簡単にするアプリケーションです。このチュートリアルでは、Ubuntu 20.04にDocker Community Edition (CE) をインストールして使用します。Docker自体をインストールし、コンテナとイメージを操作し、イメージをD…
sudo docker ps -a

docker psだと動作中のコンテナのみ表示されるので、「-a」オプションをつけてリストを表示します。

ここで表示されたコンテナIDもしくはNamesを用いて、コンテナの操作をします。

$ sudo docker ps -a
CONTAINER ID   IMAGE         COMMAND    CREATED         STATUS                     PORTS     NAMES
2b8c76553a1d   hello-world   "/hello"   9 minutes ago   Exited (0) 9 minutes ago             blissful_thompson
046926bcbe63   hello-world   "/hello"   9 minutes ago   Exited (0) 9 minutes ago             unruffled_cray

この例だと、「2b8c76553a1d」がコンテナID、「unruffled_cray」が(dockerがつけた)コンテナ名です。

sudo docker rm 2b8c76553a1d
sudo docker rm unruffled_cray
sudo docker rmi hello-world

これで、ダウンロードされたイメージを含めて削除できました。

docker利用権限付与

毎回sudoは大変なので、カレントユーザー$USERにdocker実行権限を付与しておきます。

sudo groupadd docker
sudo usermod -aG docker $USER

dockerグループはすでに作成済みといわれましたが、無事に実行できました。

改変版

Hello Worldはいらないし、docker-composeが必要なので、以下に変更して実行します。

sudo apt update
sudo apt upgrade -y
sudo apt remove docker docker-engine docker.io containerd runc
sudo apt install -y ca-certificates curl gnupg lsb-release #元から入っていた
curl -fsSL https://download.docker.com/linux/ubuntu/gpg \
| sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-compose
sudo groupadd docker
sudo usermod -aG docker $USER

Docker版mattermost導入

mattermostリポジトリ

GitHub - mattermost/docker: Install Mattermost server via Docker
Install Mattermost server via Docker. Contribute to mattermost/docker development by creating an account on GitHub.

前回はpreviewで全部入り的なものでしたが、今回は普通の(?)を入れてみました。公式ページのreadmeを見ながらインストールです。

cd ~
git clone https://github.com/mattermost/docker
cd docker
cp env.example .env
mkdir -p ./volumes/app/mattermost/{config,data,logs,plugins,client/plugins,bleve-indexes}
sudo chown -R 2000:2000 ./volumes/app/mattermost
sudo systemctl enable --now docker

.env編集

DOMAIN=mm.example.com → DOMAIN=利用するドメイン

TZ=UTC → TZ=Asia/Tokyo

CERT_PATH=./volumes/web/cert/cert.pem
KEY_PATH=./volumes/web/cert/key-no-password.pem

MATTERMOST_IMAGE=mattermost-enterprise-edition → mattermost-team-edition

環境変数ファイル(?)「.env」にサーバー証明書を入れるところ(cert.pemとkey-no-password.pem)があるので、opensslを使って作成しました。

起動と停止は、サーバー付きのコマンドで、

sudo docker-compose -f docker-compose.yml -f docker-compose.nginx.yml up -d
sudo docker-compose -f docker-compose.yml -f docker-compose.nginx.yml down

を実行しました。

エラー回避

使用したdockerの設定ファイル.envの最後のほうにコメントが書いてありますが、.envだけではなく.yamlファイルも編集が必要です。しないと、

Error: failed to load configuration: failed to create store: unable to load on store creation: invalid config: Config.IsValid: model.config.is_valid.site_url.app_error,

https://issueexplorer.com/issue/mattermost/docker/41

こんなエラーが出ます。docker-compose.yml内で後半の部分を変更して追記します。

environment:
  # timezone inside container
  - TZ

  # necessary Mattermost options/variables (see env.example)
  - MM_SQLSETTINGS_DRIVERNAME=postgres
  - MM_SQLSETTINGS_DATASOURCE=postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB}?sslmode=disable&connect_timeout=10

  # necessary for bleve
  - MM_BLEVESETTINGS_INDEXDIR

  # additional settings
  - MM_SERVICESETTINGS_SITEURL=https://${DOMAIN}

定型部分は編集しなくても良いようにしてほしい・・・。

自己署名サーバー証明書

openssl genpkey -algorithm ED25519 -out key.pem
openssl pkey -in key.pem -pubout -out pubkey.pem
openssl req -new -sha256 -key key.pem -out server.csr
openssl x509 -req -in server.csr -signkey key.pem -days 365 -sha256 -out server.crt

EDDSAで作成したcrtファイルではダメでした。CRTファイル作成時に出るエラーは秘密鍵の形式の問題なので無視して良い感じです。

[openssl-users] Intermediate cert file failure

RSAに変更し、最初の行を

openssl genrsa -out key.pem 4096
openssl req -new -sha256 -key key.pem -out server.csr
openssl x509 -req -in server.csr -signkey key.pem -days 365 -sha256 -out server.crt

に変更して実行したら動作しました。ただし、自己署名なのでブラウザでエラーにはなります。

Lets’ Encryptを使った証明書をインストール

できればエラーを避けたいので、別記事にしているLets’ Encryptを利用するgetSSLプログラムを利用して作成したCRTファイルとKEYファイルをdockerのところにコピーします。

CERT_PATH=./volumes/web/cert/cert.pem
KEY_PATH=./volumes/web/cert/key-no-password.pem

ここを変更しない場合には、mattermostのディレクトリの下に作成した、volumes/web/certに置きますが、他の場所を指定する場合、ユーザー名の$USERとか、チルダ(~)とかを指定するとエラーでnginx_mattermostが起動しないことがあるので要注意でした。

DNS解決: dnsmasq

設定したホスト名で接続可能にするために、プライベートアドレス空間ではdnsmasqを使ってドメイン名を解決します。

DnsmasqをDockerで起動する
自宅のサーバーでは、独自ドメインを利用していくつかのサーバーを起動しているが、自宅内のLANからだと、ルータを通るため、独自ドメインでのアク
DockerでDnsmasqを使ったローカル環境の内部DNSを構築するメモ | 7me
ローカル環境で名前解決したいけど、 hostsの管理が面倒だったり、AndroidやiPhoneなどのスマホから名前解決が出来ない場合、ローカルに内部DNSサーバを立てて解決します サンプル nobiki/dnsmasq Dockerの方が手軽で楽ですが、Dockerを使わない(or使えない)場合の setup.sh...

dockerイメージはたくさんあり、1000万ダウンロードクラスのものも多いですが、アップデートされたのが1-4年前というものばかりなので、ちょっと使いづらいです。

それくらいなら、普通にインストールのほうが良いのかもと思います。KVMで作成した仮想マシンに単独でインストールしました。

Ubuntu 20.04 LTS : Dnsmasq : インストール : Server World

53ポートが使えない

Ubuntuでは、systemd-resolvedが53ポートを占有しているようです。

This method works on the Ubuntu releases 17.04 (Zesty), 17.10 (Artful), 18.04 (Bionic), 18.10 (Cosmic), 19.04 (Disco) and 20.04 (Focal):

Disable and stop the systemd-resolved service:

sudo systemctl disable systemd-resolved
sudo systemctl stop systemd-resolved

Then put the following line in the [main] section of your /etc/NetworkManager/NetworkManager.conf:

dns=default

Delete the symlink /etc/resolv.conf

rm /etc/resolv.conf

Restart NetworkManager

sudo systemctl restart NetworkManager

今回の環境の場合には、

sudo systemctl disable systemd-resolved
sudo systemctl stop systemd-resolved
sudo rm /etc/resolv.conf

を行うのと、/etc/dnsmasq.confで

resolv-file=/etc/dnsmasq.resolv.conf

を記載し、hostsに指定したいドメインを追記し、指定したdnsmasq.resolv.confに上流ネームサーバーを設定します。

#/etc/hosts
192.168.68.79 yourdomain.com

#/etc/dnsmasq.resolv.conf
nameserver 8.8.8.8

これでサービスを再起動すれば、自分用DNSが設定できます。

ゲートウェイに使用しているルーター等へDNS設定できれば、それ以上何もしなくてもばっちり認証できるようになります。

コメント

タイトルとURLをコピーしました