Docker快速部署Mastodon

安装需要的软件包

1
2
apt -y update
apt -y install curl git nginx python-certbot-nginx

安装docker

1
2
curl -sSL https://get.docker.com/ | sh
systemctl enable --now nginx docker

安装docker-compose

1
2
curl -L https://github.com/docker/compose/releases/download/1.27.4/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

拉取mastodon项目文件

1
2
3
cd /opt
git clone https://github.com/tootsuite/mastodon.git
cd mastodon/

复制一份配置文件

1
cp .env.production.sample .env.production

把项目内自带的docker-compose.yml重命名或者删除都可,这个配置有问题🤨

1
mv docker-compose.yml docker-compose.yml.bak

新建一个docker-compose.yml

1
nano docker-compose.yml

写入以下内容:

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
version: '3.5'

services:
mastodon-db:
image: postgres:9.6-alpine
shm_size: 256mb
environment:
POSTGRES_DB: mastodon
POSTGRES_USER: imlala
POSTGRES_PASSWORD: password
healthcheck:
test: ["CMD", "pg_isready", "-U", "postgres"]
volumes:
- ./postgres:/var/lib/postgresql/data
restart: unless-stopped

mastodon-redis:
image: redis:6.0-alpine
healthcheck:
test: ["CMD", "redis-cli", "ping"]
volumes:
- ./redis:/data
restart: unless-stopped

mastodon-web:
image: tootsuite/mastodon
env_file: .env.production
command: bash -c "rm -f /mastodon/tmp/pids/server.pid; bundle exec rails s -p 3000"
depends_on:
- mastodon-db
- mastodon-redis
healthcheck:
test: ["CMD-SHELL", "wget -q --spider --proxy=off localhost:3000/health || exit 1"]
ports:
- "127.0.0.1:3000:3000"
volumes:
- ./public/system:/mastodon/public/system
restart: unless-stopped

mastodon-streaming:
image: tootsuite/mastodon
env_file: .env.production
command: node ./streaming
depends_on:
- mastodon-db
- mastodon-redis
healthcheck:
test: ["CMD-SHELL", "wget -q --spider --proxy=off localhost:4000/api/v1/streaming/health || exit 1"]
ports:
- "127.0.0.1:4000:4000"
restart: unless-stopped

mastodon-sidekiq:
image: tootsuite/mastodon
env_file: .env.production
command: bundle exec sidekiq
depends_on:
- mastodon-db
- mastodon-redis
volumes:
- ./public/system:/mastodon/public/system
restart: unless-stopped

运行这个命令,进入配置向导

1
docker-compose run --rm mastodon-web bundle exec rake mastodon:setup

配置如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Domain name: # 填写你的域名
Do you want to enable single user mode? # Yes
Are you using Docker to run Mastodon? # Yes
PostgreSQL host: # mastodon-db
PostgreSQL port: # 5432
Name of PostgreSQL database: # mastodon
Name of PostgreSQL user: # imlala
Password of PostgreSQL user: # 填你在compose内设置的密码
Redis host: # mastodon-redis
Redis port: # 6379
Redis password: # 留空,直接回车
Do you want to store uploaded files on the cloud? # No
Do you want to send e-mails from localhost? # Yes
Send a test e-mail with this configuration right now? # No
Save configuration? Yes

这个配置的是单用户模式,也就是默认不开放注册,其次没有配置SMTP。接下来它会把你填写的配置打印出来,现在需要把你之前填的这些配置复制下来。

⚠️注意到了一个非常坑的地方,接下来你会看到这个提示:

1
Prepare the database now? (Y/n)

不要选择Y或N,直接Ctrl+C退出来。然后清空如下配置文件内的默认配置:

1
echo > .env.production

编辑它:

1
nano .env.production

把你之前通过向导复制的配置粘贴到里面保存后重新运行一次向导:

1
docker-compose run --rm mastodon-web bundle exec rake mastodon:setup

这次的配置和之前的务必保持一致。再次来到下面这个位置的时候,就可以Yes了:

1
2
Prepare the database now? (Y/n) # Yes
Compile the assets now? (Y/n) # Yes

接下来的步骤就是创建管理员账户了,继续跟着这个向导走就OK了。全部完成之后,up起来其他的服务:

1
docker-compose up -d

现在还需要把public的目录所有者改为容器内的,不然后续不能上传头像:

1
chown -R 991:991 public

现在来复制一份nginx的站点配置文件:

1
cp /opt/mastodon/dist/nginx.conf /etc/nginx/conf.d/mastodon.conf

编辑mastodon.conf

1
nano /etc/nginx/conf.d/mastodon.conf

在这个配置文件内,官方都写好了模版,你只需要改动两个server块下面的位置:

1
2
3
4
5
6
7
8
9
10
11
12
13
server {
listen 80;
listen [::]:80;
server_name imlala.best; # 换成你的域名
root /opt/mastodon/public; # public目录的绝对路径
...
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name imlala.best; # 换成你的域名
root /opt/mastodon/public; # public目录的绝对路径
...
}

使用cerbot签发ssl证书

1
cerbot --nginx

Done,到这里就完成部署了。

后续要搬家,直接打包opt/mastodon这个目录就好了,传到别的机器内解压后直接up起来就可以了。