docker-composeでサクッと構築。 今話題のMastodonのインストールに挑戦してみた!(その2)

カテゴリー │サーバーIT全般

こんにちは、たかしーです。

docker-composeでサクッと構築。 今話題のMastodonのインストールに挑戦してみた!(その2)


前回の記事でも書いた通り、先日から行っているMastodon(マストドン)の構築について書いていきたいと思います。
(dockerのインストールや、Mastodonソースのダウンロードについては前回の記事をご覧ください。)

git cloneを実行したディレクトリの中にmastodonフォルダができているはずなので、

cd mastodon
view raw mastodon_inst4 hosted with ❤ by GitHub


としてmastodonフォルダの中に入ります。

デフォルトでは、redis,DBの変更はエフェメラル(一時的)で、コンテナを停止するとデータが消えてしまうようです。

今何かと話題のマストドン(mastodon)鯖を自分用に無料で立てる方法 - jtwp470’s blog

永続化できるように先にdocker-compose.ymlを編集しておきます。

version: '2'
services:
db:
restart: always
image: postgres:alpine
### Uncomment to enable DB persistance
# volumes: #コメントを外す
# - ./postgres:/var/lib/postgresql/data  #コメントを外す
redis:
restart: always
image: redis:alpine
### Uncomment to enable REDIS persistance
# volumes: #コメントを外す
# - ./redis:/data #コメントを外す
web:
restart: always
build: .
image: gargron/mastodon
env_file: .env.production
command: bundle exec rails s -p 3000 -b '0.0.0.0'
ports:
- "3000:3000"
depends_on:
- db
- redis
volumes:
- ./public/assets:/mastodon/public/assets
- ./public/system:/mastodon/public/system
streaming:
restart: always
build: .
image: gargron/mastodon
env_file: .env.production
command: npm run start
ports:
- "4000:4000"
depends_on:
- db
- redis
sidekiq:
restart: always
build: .
image: gargron/mastodon
env_file: .env.production
command: bundle exec sidekiq -q default -q mailers -q pull -q push
depends_on:
- db
- redis
volumes:
- ./public/system:/mastodon/public/system


これで、コンテナを停止、削除してもデータは保持されます。

次にMastodon本体の設定を行っていきます。

Mastodonでは、デフォルトでは5つのコンテナ

mastodon_web_1
mastodon_sidekiq_1
mastodon_streaming_1
mastodon_db_1
mastodon_redis_1

を使用して動作する構成になっていますが、環境変数を用いることで、1つの設定ファイルを編集するだけで必要な設定がすべてできるようになっています。よく出来てますね。

環境変数設定ファイルを編集するにはまず、mastodonフォルダ内の
.env.production.sample

.env.production
という名前でコピーします。

$ pwd
/path/to/mastodon #今いるフォルダ
$ ls -a | grep .env.production.sample
.env.production.sample #.env.production.sampleというファイルがあるので...
$ cp .env.production.sample .env.production # .env.production.sampleを .env.productionという名前でコピーします。
view raw mastodon_inst5 hosted with ❤ by GitHub


設定ファイルの中身をのぞいてみます。

# Service dependencies
REDIS_HOST=redis
REDIS_PORT=6379
# REDIS_DB=0
DB_HOST=db
DB_USER=postgres
DB_NAME=postgres
DB_PASS=
DB_PORT=5432
# Federation
LOCAL_DOMAIN=example.com
LOCAL_HTTPS=true
# Use this only if you need to run mastodon on a different domain than the one used for federation.
# Do not use this unless you know exactly what you are doing.
# WEB_DOMAIN=mastodon.example.com
# Application secrets
# Generate each with the `rake secret` task (`docker-compose run --rm web rake secret` if you use docker compose)
PAPERCLIP_SECRET=
SECRET_KEY_BASE=
OTP_SECRET=
# Registrations
# Single user mode will disable registrations and redirect frontpage to the first profile
# SINGLE_USER_MODE=true
# Prevent registrations with following e-mail domains
# EMAIL_DOMAIN_BLACKLIST=example1.com|example2.de|etc
# Only allow registrations with the following e-mail domains
# EMAIL_DOMAIN_WHITELIST=example1.com|example2.de|etc
# Optionally change default language
# DEFAULT_LOCALE=de
# E-mail configuration
# Note: Mailgun and SparkPost (https://sparkpo.st/smtp) each have good free tiers
# If you want to use an SMTP server without authentication (e.g local Postfix relay)
# then set SMTP_AUTH_METHOD to 'none' and *comment* SMTP_LOGIN and SMTP_PASSWORD.
# Leaving them blank is not enough for authentication method 'none'.
SMTP_SERVER=smtp.mailgun.org
SMTP_PORT=587
SMTP_LOGIN=
SMTP_PASSWORD=
SMTP_FROM_ADDRESS=notifications@example.com
#SMTP_DOMAIN= # defaults to LOCAL_DOMAIN
#SMTP_DELIVERY_METHOD=smtp # delivery method can also be sendmail
#SMTP_AUTH_METHOD=plain
#SMTP_OPENSSL_VERIFY_MODE=peer
#SMTP_ENABLE_STARTTLS_AUTO=true
# Optional user upload path and URL (images, avatars). Default is :rails_root/public/system. If you set this variable, you are responsible for making your HTTP server (eg. nginx) serve these files.
# PAPERCLIP_ROOT_PATH=/var/lib/mastodon/public-system
# PAPERCLIP_ROOT_URL=/system
# Optional asset host for multi-server setups
# CDN_HOST=assets.example.com
# S3 (optional)
# S3_ENABLED=true
# S3_BUCKET=
# AWS_ACCESS_KEY_ID=
# AWS_SECRET_ACCESS_KEY=
# S3_REGION=
# S3_PROTOCOL=http
# S3_HOSTNAME=192.168.1.123:9000
# S3 (Minio Config (optional) Please check Minio instance for details)
# S3_ENABLED=true
# S3_BUCKET=
# AWS_ACCESS_KEY_ID=
# AWS_SECRET_ACCESS_KEY=
# S3_REGION=
# S3_PROTOCOL=https
# S3_HOSTNAME=
# S3_ENDPOINT=
# S3_SIGNATURE_VERSION=
# Optional alias for S3 if you want to use Cloudfront or Cloudflare in front
# S3_CLOUDFRONT_HOST=
# Streaming API integration
# STREAMING_API_BASE_URL=
# Advanced settings
# If you need to use pgBouncer, you need to disable prepared statements:
# PREPARED_STATEMENTS=false
# Cluster number setting for streaming API server.
# If you comment out following line, cluster number will be `numOfCpuCores - 1`.
STREAMING_CLUSTER_NUM=1


こんな中身になっています。
.env.productionの方を自分のサーバー環境に合わせて以下のように書き換えていきます。

# Service dependencies
REDIS_HOST=redis
REDIS_PORT=6379
# REDIS_DB=0
DB_HOST=db
DB_USER=postgres
DB_NAME=postgres
DB_PASS=
DB_PORT=5432
# Federation
LOCAL_DOMAIN=example.com #Mastodonで運用予定のドメインに書き換え。なお、ここで"example.com:8000"のようにポート番号を指定しても無視される。
LOCAL_HTTPS=true #FalseにするとSSL無効化できるが、https経由で接続するとWebSocket接続の際Chrome先生に怒られるのでtrue推奨
# Use this only if you need to run mastodon on a different domain than the one used for federation.
# Do not use this unless you know exactly what you are doing.
# WEB_DOMAIN=mastodon.example.com
# Application secrets
# Generate each with the `rake secret` task (`docker-compose run --rm web rake secret` if you use docker compose)
PAPERCLIP_SECRET= #後程解説
SECRET_KEY_BASE= #後程解説
OTP_SECRET= #後程解説
# Registrations
# Single user mode will disable registrations and redirect frontpage to the first profile
# SINGLE_USER_MODE=true
# Prevent registrations with following e-mail domains
# EMAIL_DOMAIN_BLACKLIST=example1.com|example2.de|etc
# Only allow registrations with the following e-mail domains
# EMAIL_DOMAIN_WHITELIST=example1.com|example2.de|etc
# Optionally change default language
# DEFAULT_LOCALE=de #さわらない(次回解説)
# E-mail configuration
# Note: Mailgun and SparkPost (https://sparkpo.st/smtp) each have good free tiers
# If you want to use an SMTP server without authentication (e.g local Postfix relay)
# then set SMTP_AUTH_METHOD to 'none' and *comment* SMTP_LOGIN and SMTP_PASSWORD.
# Leaving them blank is not enough for authentication method 'none'.
SMTP_SERVER=smtp.mailgun.org #メール送信に使うSMTPのホスト名を入力(ipアドレスOK)
SMTP_PORT=25 #メール送信に使うSMTPのポート番号(25 or 465 or 587)
SMTP_LOGIN= #SMTP認証が必要な場合は入力(いらない場合はコメントアウトしてok)
SMTP_PASSWORD= #SMTP認証が必要な場合は入力 (いらない場合はコメントアウトしてok)
SMTP_FROM_ADDRESS=notifications@example.com #送り元アドレスを指定(なんでもよいが、spfやDKIMがない(サブ)ドメインなどを設定すると迷惑メール扱いされる)
#SMTP_DOMAIN= # defaults to LOCAL_DOMAIN #さわらない
#SMTP_DELIVERY_METHOD=smtp # delivery method can also be sendmail 
#SMTP_AUTH_METHOD=plain #smtp認証する場合、認証方法を指定
#SMTP_OPENSSL_VERIFY_MODE=peer #SSLを無効化する場合はコメントを外して"peer"を"none"に書き換え(デフォルトはSSL有効)
#SMTP_ENABLE_STARTTLS_AUTO=true
# Optional user upload path and URL (images, avatars). Default is :rails_root/public/system. If you set this variable, you are responsible for making your HTTP server (eg. nginx) serve these files.
# PAPERCLIP_ROOT_PATH=/var/lib/mastodon/public-system
# PAPERCLIP_ROOT_URL=/system
# Optional asset host for multi-server setups
# CDN_HOST=assets.example.com
# S3 (optional)
# S3_ENABLED=true
# S3_BUCKET=
# AWS_ACCESS_KEY_ID=
# AWS_SECRET_ACCESS_KEY=
# S3_REGION=
# S3_PROTOCOL=http
# S3_HOSTNAME=192.168.1.123:9000
# S3 (Minio Config (optional) Please check Minio instance for details)
# S3_ENABLED=true
# S3_BUCKET=
# AWS_ACCESS_KEY_ID=
# AWS_SECRET_ACCESS_KEY=
# S3_REGION=
# S3_PROTOCOL=https
# S3_HOSTNAME=
# S3_ENDPOINT=
# S3_SIGNATURE_VERSION=
# Optional alias for S3 if you want to use Cloudfront or Cloudflare in front
# S3_CLOUDFRONT_HOST=
# Streaming API integration
# STREAMING_API_BASE_URL=
# Advanced settings
# If you need to use pgBouncer, you need to disable prepared statements:
# PREPARED_STATEMENTS=false
# Cluster number setting for streaming API server.
# If you comment out following line, cluster number will be `numOfCpuCores - 1`.
STREAMING_CLUSTER_NUM=1


設定ファイル中の

PAPERCLIP_SECRET
SECRET_KEY_BASE
OTP_SECRET

の3項目にはキーを生成してセットする必要があります。
この部分はそのままにして、一旦保存し、一度コンテナイメージのビルトを行います。

以下のコマンドを実行します。

docker-compose build # コンテナのビルド(一回の)
docker-compose run --rm web rake secret #キーの生成。3回繰り返し実行。それぞれのキーを控えて置く。
view raw mastodon_inst6 hosted with ❤ by GitHub


3項目別々のキーが必要になりたるため、

docker-compose run --rm web rake secret

を3回実行してキーを3つ生成します。
生成した3つのキーを

PAPERCLIP_SECRET
SECRET_KEY_BASE
OTP_SECRET

のそれぞれの"="の後につなげて挿入しておきます。

これで設定ファイルの準備は完了です。
DBとアセットの準備をします。

#Mastodon用DB初期化
docker-compose run --rm web rails db:migrate
#Mastodonのアセットの準備
docker-compose run --rm web rails assets:precompile
view raw mastodon_inst7 hosted with ❤ by GitHub


これで、起動準備は完了です。
実際にコンテナを立ち上げてみます。

#Mastodonコンテナの起動
docker-compose up -d
view raw mastodon_inst8 hosted with ❤ by GitHub


これで、Mastodon本体が立ち上がるはずです。
docker-compose up -d
を実行したサーバーのポート3000番でhttp(s)のアクセスを待ち受けるようになります。
したがって、このままの状態ではアドレスを打っただけではうまくブラウザからMastodonに接続できません。(サーバーでGUIが使える場合は ブラウザで https://localhost:3000 とすればテストすることはできるはず。)

Mastodonを通常のサイト通り443(80)番ポートで見ることができるようにするためには、"リバースプロキシ"を設置して、通信を仲介させる必要があります。
次回は公式でも推奨されている"Nginx"を使ったリバースプロキシを構築してみたいと思います。

------------------------------------------
参考サイト:
今何かと話題のマストドン(mastodon)鯖を自分用に無料で立てる方法 - jtwp470’s blog


同じカテゴリー(サーバー)の記事


上の画像に書かれている文字を入力して下さい
 
<ご注意>
書き込まれた内容は公開され、ブログの持ち主だけが削除できます。

削除
docker-composeでサクッと構築。 今話題のMastodonのインストールに挑戦してみた!(その2)
    コメント(0)