Skip to content

Instantly share code, notes, and snippets.

@OrchidLuna
Last active July 29, 2024 22:35
Show Gist options
  • Save OrchidLuna/af8be81d4e66681225c6ae9d91083a41 to your computer and use it in GitHub Desktop.
Save OrchidLuna/af8be81d4e66681225c6ae9d91083a41 to your computer and use it in GitHub Desktop.
How to install Mastodon in docker (even easier imo, 2024)

I'm assuming you're running the latest version of Ubuntu (24.04) or Debian (12), so the installation of prerequisites will use apt.

  1. Login as root: sudo -i
  2. Install the prerequisites: apt install docker.io docker-compose nginx python3-venv
  3. Create directory for Mastodon: mkdir /opt/mastodon
  4. Enter to Mastodon directory: cd /opt/mastodon
  5. Create directory for Postgres: mkdir postgres14
  6. Create directory for Redis: mkdir redis
  7. Create directory for Mastodon files: mkdir -p public/system
  8. Set owner to Mastodon user (991) for public directory: chown -R 991:991 public
  9. Get docker-compose.yml for Mastodon: wget https://raw.githubusercontent.com/mastodon/mastodon/main/docker-compose.yml
  10. Edit docker-compose.yml in a text editor to comment out build lines:

Screenshot_3

Screenshot_5

Screenshot_6

  1. Generate a password for postgres database: openssl rand -hex 12
  2. Copy the generated password to another place.
  3. Deploy postgres container: docker run --rm --name postgres -v $PWD/postgres14:/var/lib/postgresql/data -e POSTGRES_PASSWORD="opensslgen" -d postgres:14-alpine (with "opensslgen" being the password you generate earlier.)
  4. Enter psql prompt: docker exec -it postgres psql -U postgres
  5. Create database for Mastodon: CREATE USER mastodon WITH PASSWORD 'opensslgen' CREATEDB; (with "opensslgen" being the password you generate earlier.)
  6. Exit from psql prompt: exit
  7. Stop postgres container: docker stop postgres
  8. Create .env.production file: touch .env.production
  9. Start Mastodon setup wizard: docker-compose run --rm web bundle exec rake mastodon:setup
  10. Answer the questions:
Domain name: mastodon.example.com

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

Are you using Docker to run Mastodon? Yes

PostgreSQL host: db
PostgreSQL port: 5432
Name of PostgreSQL database: mastodon
Name of PostgreSQL user: mastodon
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.example.com
SMTP port: 587
SMTP username: smtp@example.com
SMTP password:
SMTP authentication: plain
SMTP OpenSSL verify mode: none
Enable STARTTLS: auto
E-mail address to send e-mails "from": Mastodon <notifications@mastodon.example.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
  1. Before answering yes to "prepare database now?" question, copy the generated .env.production above to somewhere else.
  2. Continue answering questions:
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` ...


Created database 'mastodon'
Done!

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

Do you want to create an admin user straight away? no

Why not create the admin user? Because in my testing, either web container unable to contact redis or UniqueViolation error will occur if you say yes, and admin user won't have a password.

  1. Paste .env.production you copied earlier to /opt/mastodon/.env.production
  2. Create admin user for Mastodon: docker-compose run --rm web tootctl accounts create example --email email@example.com --confirmed --role Owner
  3. Save your password set for your Mastodon account. Now start Mastodon with docker-compose up -d

Congrats for completing Mastodon part of this tutorial! Let's configure nginx.

  1. Install certbot with these commands:
python3 -m venv /opt/certbot
/opt/certbot/bin/pip install --upgrade pip
/opt/certbot/bin/pip install certbot certbot-nginx
  1. For issuing SSL certificate, we need a temporary vhost. Create the directory for it: mkdir /var/www/mastodon
  2. Create an index.html: echo hello world >> /var/www/mastodon/index.html
  3. Insert this to /etc/nginx/conf.d/mastodon.conf:
server {
server_name mastodon.example.com;
listen 80;
root /var/www/mastodon;
}
  1. Reload nginx: systemctl reload nginx
  2. Obtain SSL certificate for Mastodon: /opt/certbot/bin/certbot --nginx -d mastodon.example.com
  3. Delete the temporary vhost: rm /etc/nginx/conf.d/mastodon.conf
  4. Delete temporary vhost's directory: rm /var/www/mastodon
  5. Download the pre-made nginx config file for Mastodon: wget -O /etc/nginx/conf.d/mastodon.conf https://raw.githubusercontent.com/mastodon/mastodon/main/dist/nginx.conf
  6. Edit /etc/nginx/conf.d/mastodon.conf to replace:
A. /home/mastodon/live/public with /opt/mastodon/public
B. example.com with your Mastodon domain.
C. Uncomment ssl_certificate and ssl_certificate_key lines and replace example.com with your Mastodon domain.
D. Replace =404 with @proxy
  1. Reload nginx again: systemctl reload nginx
  2. Visit your Mastodon domain, login to your admin account, and you'll see that your own account is pending approval. Approve yourself with docker-compose run --rm web tootctl accounts approve --all
  3. We're done! Enjoy tooting!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment