架設參考自: https://peterbabic.dev/blog/running-mastodon-with-docker-compose/
網友的mastodon使用指引: https://mantyke.icu/posts/2021/386276df/
其他架設參考文章: Mastodon Setup with Docker and Caddy | rriemann
開始安裝
Clone the Mastodon repository. It contains a docker-compose.yml
file as well as other files directly or indirectly referenced by it (for example package.json
or yarn.lock
):
SSH進NAS,切換到docker
根目錄
cd /volume1/docker/
git clone https://github.com/mastodon/mastodon.git
cd mastodon
Now here's what I occasionally do to help me keep track of the changes to the configuration files easily. Make a branch on a given tag, which at the time of writing was v3.5.3
:
切換目前專案最新的分支,2022.9月安裝時官方釋出的版號維持5月的v3.5.3
,原作者建議固定在穩定的分支,這樣即使未來git pull得到的更新也會限定在目前的分支上,比較安全
git checkout v3.5.3 -b v3.5.3-branch
Without creating a branch, the HEAD would be in a detached state (pointing at a tagged commit, not a branch), It would still track changes, but these would not be accessible after another checkout.
Tip: to get the latest available tag easily, you can use git rev-list
as follows:
git fetch --all --tags
git describe --tags `git rev-list --tags --max-count=1`
Also consider changing the mastodon image to some tagged version. In the section web
replace mastodon:latest
image with the tagged one:
image: tootsuite/mastodon:v3.5.3
It is useful for referencing and searching for issues, should some arise, at the very least. Even more important to me is that it requires a manual intervention to bump a version number, so things won't suddenly change when the docker-compose script get restarted without you understanding why. It is overall a good practice to avoid unnecessary surprises.
Postgres database
The referenced version of postgres in the docker-compose file is 9.6-alpine
. This might work, but I tested with 14-alpine
instead and found no problems so far, so I changes to this version under the db
section:
image: postgres:14-alpine
Start the container to setup the user, assuming the path to the docker-compose file is /home/mastodon/mastodon/docker-compose.yml
. If not, modify the path so the postgres
volume folder matches it. Consider setting a custom <u>password</u>:
sudo docker run --name postgres14 -v /volume1/docker/mastodon/postgres:/var/lib/postgresql/data -e POSTGRES_PASSWORD=dennis13 --rm -d postgres:14-alpine
Create a mastodon database user, use the password from above:
sudo docker exec -it postgres14 psql -U postgres
> CREATE USER mastodon WITH PASSWORD 'dennis13' CREATEDB;
> exit
sudo docker stop postgres14
This makes database setup complete.
create config file under root of mastodon folder
touch .env.production
start installation
sudo docker-compose run --rm web bundle exec rake mastodon:setup
這邊編譯時間會視硬體能力而不同,我的NAS CPU顯然不太夠力,花了快一小時才好Orz
Fill the domain name you intend to run the instance. This one is probably hard to change once the instance is running. Fill the next questions according to the table below:
| Question | Type in |
| :-------------------------------------- | :---------------- |
| Do you want to enable single user mode? | No |
| Are you using Docker to run Mastodon? | Yes |
| PostgreSQL host: | mastodon_db_1 |
| PostgreSQL port: | 5432 |
| Name of PostgreSQL database: | mastodon |
| Name of PostgreSQL user: | mastodon |
| Password of ProstgreSQL user: | password |
The above part should look like this in the terminal:
![Single user mode? N, Using Docker to run Mastodon? Y, PostgreSQL host: mastodon_db_1, PostgreSQL port: 5432, Name of PostgreSQL database: mastodon, Name of PostgreSQL user: mastodon, Password of ProstgreSQL user: password](masotdon docker安裝法.assets/mastodon_setup_1.png)
The setup then continues with email capabilities configuration questions. I am omitting details for this part, as my email provider required different SMTP settings, some of which were not offered via this setup wizard. I have not found a reliable way to send a test email from the UI or the console later, so it might be worth trying here to get the emails sent out. Setting up cloud storage or email capabilities can be also safely skipped now and configured later, if you wish to do so, use these options:
| Question | Type in |
| :---------------------------------------------------- | :------ |
| Do you want to store uploaded files on the cloud? | No |
| Do you want to send e-mails from localhost? | Yes |
| E-mail address to send e-mails "from": | Enter |
| Send a test e-mail with this configuration right now? | No |
| Save configuration? | Yes |
Your terminal should resemble this:
![Store uploaded files in cloud: No, Send e-mails from localhost: Yes, Send e-mail "from": Enter, Send a test email now: No, Save configuration: Yes](masotdon docker安裝法.assets/mastodon_setup_2.png)
The terminal then outputs the configuration, including secret keys. Copy and paste it into .env.production
file in the cloned repository already containing postgres/
directory and docker-compose.yml
file, among others.
The last part is to migrate the database and create an admin account. Answer Yes to both and proceed. The Mastodon instance admin user password will be generated and displayed, make sure to not lose it! If you lose it before logging in successfully, one way to obtain it again is to delete postgres/
folder and start over from the Postgres database step above.
Full-text search
This step is optional, although it is a nice addition to have a full-text search provided via ElasticSearch available. Edit the docker-compose.yml
and uncomment two es
related blocks:
# es:
# restart: always
# image: docker.elastic.co/elasticsearch/elasticsearch:7.17.4
# environment:
# - "ES_JAVA_OPTS=-Xms512m -Xmx512m -Des.enforce.bootstrap.checks=true"
# - "xpack.license.self_generated.type=basic"
# - "xpack.security.enabled=false"
# - "xpack.watcher.enabled=false"
# - "xpack.graph.enabled=false"
# - "xpack.ml.enabled=false"
# - "bootstrap.memory_lock=true"
# - "cluster.name=es-mastodon"
# - "discovery.type=single-node"
# - "thread_pool.write.queue_size=1000"
# networks:
# - external_network
# - internal_network
# healthcheck:
# test: ["CMD-SHELL", "curl --silent --fail localhost:9200/_cluster/health || exit 1"]
# interval: 15s
# retries: 12
# volumes:
# - ./elasticsearch:/usr/share/elasticsearch/data
# ulimits:
# memlock:
# soft: -1
# hard: -1
# nofile:
# soft: 65536
# hard: 65536
# ports:
# - '127.0.0.1:9200:9200'
web:
build: .
image: tootsuite/mastodon
restart: always
env_file: .env.production
command: bash -c "rm -f /mastodon/tmp/pids/server.pid; bundle exec rails s -p 3000"
networks:
- external_network
- internal_network
healthcheck:
test: ["CMD-SHELL", "wget -q --spider --proxy=off localhost:3000/health || exit 1"]
ports:
- "127.0.0.1:3000:3000"
depends_on:
- db
- redis
# - es
volumes:
- ./public/system:/mastodon/public/system
Edit .env.production
file and append the following:
ES_ENABLED=true
ES_HOST=mastodon_es_1
ES_PORT=9200
The instance should now be ready to start.
First run
Start the whole stack, this can take a while:
sudo docker-compose up -d
sudo docker-compose down
This generates other files and folders, consider setting the permissions for them and start the instance again:
sudo chown -R 70:70 ./postgres
sudo chown -R 991:991 ./public
sudo chown -R 1000:1000 ./elasticsearch
sudo docker-compose up -d
如果不是在一開始就安裝啟用elasticsearch,即使docker運作了也不會對已有的嘟文進行索引(index)
ssh輸入命令重建索引: sudo docker-compose run --rm web bundle exec rake chewy:deploy
`
Now without any modifications on docker-compose.yml
the instance should be available under the port 3000
. Configure the reverse proxy of your choice to terminate the SSL/TLS and to proxy the domain name inserted into the wizard earlier to this port. You can also find some inspiration about how to do so in my previous articles under tags Nginx and especially acme.sh, should you choose to use these two to manage this task and the certificates for you.
To access the web user interface, insert the admin user name and the password generated earlier, and you are ready to have fun in the fediverse!
--
維護
Mastodon:在定期任務中清理緩存 – Anti Social Science
介紹: Mastodon使用方式:聯邦、時間軸、跟隨概念解說 | Ivon的部落格 (ivonblog.com)
優化調教篇: 使用 Mastodon 搭建个人信息平台:调优篇 - 苏洋博客 (soulteary.com)
進階整合篇: 將Mastodon時間軸嵌入到網站 | Carlos Life Book (mynet.tw)