【笔记】通过Docker部署Mastodon

前言

腾讯云通过Docker部署Mastodon,实现开源的、去中心化的微博客平台

准备工作

  • Ubuntu 22.04 LTS

切换root用户

1
sudo su

修改DNS(可选)

https://www.ipaddress.com/website/github.com/
https://www.ipaddress.com/website/raw.githubusercontent.com/

/etc/resolv.conf
1
2
3
4
5
140.82.113.4 github.com
185.199.108.133 raw.githubusercontent.com
185.199.109.133 raw.githubusercontent.com
185.199.110.133 raw.githubusercontent.com
185.199.111.133 raw.githubusercontent.com

下载依赖

1
2
apt update
apt install -y wget nginx

安装Docker

传送门

1
curl -fsSL get.docker.com | sh

配置Docker镜像源(可选)

传送门

1
2
echo '{"registry-mirrors":["https://mirror.ccs.tencentyun.com"]}' > /etc/docker/daemon.json
systemctl restart docker

创建工作目录

1
2
3
cd /opt
mkdir mastodon
cd mastodon

下载docker-compose.yml文件

/opt/mastodon/
1
wget https://raw.githubusercontent.com/mastodon/mastodon/refs/heads/main/docker-compose.yml

生成Docker环境配置

/opt/mastodon/
1
2
touch .env.production
docker compose run --rm web bundle exec rake mastodon:setup
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
Domain name: mastodon.loli.fj.cn

Single user mode disables registrations and redirects the landing page to your public profile.
Do you want to enable single user mode? No

Are you using Docker to run Mastodon? Yes

PostgreSQL host: db
PostgreSQL port: 5432
Name of PostgreSQL database: postgres
Name of PostgreSQL user: postgres
Password of PostgreSQL user:
Database configuration works! 🎆

Redis host: redis
Redis port: 6379
Redis password:
Redis configuration works! 🎆

Do you want to store uploaded files on the cloud? No

Do you want to send e-mails from localhost? No
SMTP server: smtp.126.com
SMTP port: 465
SMTP username: feiju_bot@126.com
SMTP password:
SMTP authentication: plain
SMTP OpenSSL verify mode: none
Enable STARTTLS: auto
E-mail address to send e-mails "from": feiju_bot@126.com
Send a test e-mail with this configuration right now? no

Do you want Mastodon to periodically check for important updates and notify you? (Recommended) Yes

This configuration will be written to .env.production
Save configuration? Yes
Below is your configuration, save it to an .env.production file outside Docker:

# Generated with mastodon:setup on 2026-02-26 09:40:39 UTC

...

It is also saved within this container so you can proceed with this wizard.

Now that configuration is saved, the database schema must be loaded.
If the database already exists, this will erase its contents.
Prepare the database now? Yes
Running `RAILS_ENV=production rails db:setup` ...


I, [2026-02-26T09:40:43.743014 #28] INFO -- : [dotenv] Loaded .env.production
Database 'postgres' already exists
Done!

All done! You can now power on the Mastodon server 🐘

Do you want to create an admin user straight away? Yes
Username: admin
E-mail: 00-isomer-cabbies@icloud.com
INFO 2026-02-26T09:41:47.330Z pid=7 tid=2h3: Sidekiq 8.0.9 connecting to Redis with options {size: 10, pool_name: "internal", url: "redis://redis:6379/0", driver: :hiredis}
I, [2026-02-26T09:41:47.369565 #7] INFO -- : [ActiveJob] Enqueued ActionMailer::MailDeliveryJob (Job ID: 5d9608d1-3a95-4a9b-aa3f-c6a3af3d3a60) to Sidekiq(mailers) at 2026-02-26 10:41:47 UTC with arguments: "UserMailer", "welcome", "deliver_now", {args: [#<GlobalID:0x00007fb14f5d7980 @uri=#<URI::GID gid://mastodon/User/1>>]}
You can login with the password: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
You can change your password once you login.
  • 将生成后的配置内容(...)手动写入到.env.production文件中
  • 注意保存初始管理员密码(xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

手动初始化数据库(可选)

  • 如果已经执行完成mastodon:setupDo you want to create an admin user straight away? Yes步骤,则无需手动初始化数据库
1
docker compose run --rm web rails db:setup

通过Docker启动Mastodon服务

/opt/mastodon/
1
docker compose up -d

Nginx配置

  • 下载默认Nginx配置
1
wget https://raw.githubusercontent.com/mastodon/mastodon/refs/heads/main/dist/nginx.conf -O /etc/nginx/conf.d/masdoton.conf
  • 修改/etc/nginx/conf.d/masdoton.conf配置文件

    • example.com替换为站点域名
    • /home/mastodon/live/public替换为/opt/mastodon/public
    • # ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;# ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;取消注释并设置为正确的SSL密钥文件路径
    • 去除所有# try_files $uri @mastodon;的注释
  • 重载Nginx配置

1
nginx -s reload

完成

  • 截图纪念

参考文献

Bingxin的博客