Mastodon logo

Mastodon

Your self-hosted, globally interconnected microblogging community

Alternative to: Twitter/X, Threads


Mastodon is a free, open-source social network server based on ActivityPub, where users can follow friends and discover new ones. Users can publish text, links, pictures, and video, and every Mastodon server is part of a federated network that interoperates with other software implementing ActivityPub, known as the Fediverse. It includes real-time timelines, media attachments, and built-in safety and moderation tools such as private posts, locked accounts, and phrase filtering.

Mastodon Docker Compose example

Self-host Mastodon on your own server, homelab, or VPS starting from this Docker Compose example. It runs Mastodon in Docker containers using the official ghcr.io/mastodon/mastodon:latest, ghcr.io/mastodon/mastodon-streaming:latest, ghcr.io/mastodon/mastodon:latest, ghcr.io/mastodon/mastodon:latest, postgres:17-alpine, redis:7-alpine images, with persistent volumes and automatic restarts preconfigured. Review the environment variables and adjust them to your setup, save the file as compose.yml (or docker-compose.yml), and start the stack with docker compose up -d.

# Shared Mastodon environment, used by the web, streaming, sidekiq and
# migration services (they must all run with the same configuration).
x-mastodon-env: &mastodon-env
  # Domain that identifies your server (the part after @ in user handles). It cannot be changed safely later.
  LOCAL_DOMAIN: "mastodon.example.com"
  # Public base URL of the streaming API (e.g. wss://streaming.mastodon.example.com). Leave empty to use wss://LOCAL_DOMAIN.
  STREAMING_API_BASE_URL: ""
  # Rails secret used to sign sessions and cookies. Generate with: openssl rand -hex 64
  SECRET_KEY_BASE: "changeme"
  # Active Record encryption deterministic key. Generate all three keys with `bin/rails db:encryption:init`; never change them once in use.
  ACTIVE_RECORD_ENCRYPTION_DETERMINISTIC_KEY: "changeme"
  # Active Record encryption key derivation salt (see above).
  ACTIVE_RECORD_ENCRYPTION_KEY_DERIVATION_SALT: "changeme"
  # Active Record encryption primary key (see above).
  ACTIVE_RECORD_ENCRYPTION_PRIMARY_KEY: "changeme"
  # Web Push private key (generate the pair with `bundle exec rails mastodon:webpush:generate_vapid_key`). Leave both empty to disable browser push notifications.
  VAPID_PRIVATE_KEY: ""
  # Web Push public key (see above).
  VAPID_PUBLIC_KEY: ""
  # SMTP server used to send confirmation and notification emails.
  SMTP_SERVER: ""
  # SMTP server port.
  SMTP_PORT: "587"
  # SMTP username.
  SMTP_LOGIN: ""
  # SMTP password.
  SMTP_PASSWORD: ""
  # Sender address for outgoing emails.
  SMTP_FROM_ADDRESS: "notifications@mastodon.example.com"
  REDIS_HOST: "redis"
  REDIS_PORT: "6379"
  DB_HOST: "db"
  DB_PORT: "5432"
  DB_USER: "postgres"
  DB_NAME: "mastodon_production"
  DB_PASS: ""
  ES_ENABLED: "false"
  RAILS_ENV: "production"
  NODE_ENV: "production"

services:
  web:
    image: ghcr.io/mastodon/mastodon:latest
    command: bundle exec puma -C config/puma.rb
    environment:
      <<: *mastodon-env
    volumes:
      - system:/mastodon/public/system
    healthcheck:
      test: ["CMD-SHELL", "curl -s --noproxy localhost localhost:3000/health | grep -q 'OK' || exit 1"]
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_healthy
      migration:
        condition: service_completed_successfully
    restart: unless-stopped

  streaming:
    image: ghcr.io/mastodon/mastodon-streaming:latest
    command: node ./streaming/index.js
    environment:
      <<: *mastodon-env
    healthcheck:
      test: ["CMD-SHELL", "curl -s --noproxy localhost localhost:4000/api/v1/streaming/health | grep -q 'OK' || exit 1"]
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_healthy
      migration:
        condition: service_completed_successfully
    restart: unless-stopped

  sidekiq:
    image: ghcr.io/mastodon/mastodon:latest
    command: bundle exec sidekiq
    environment:
      <<: *mastodon-env
    volumes:
      - system:/mastodon/public/system
    healthcheck:
      test: ["CMD-SHELL", "ps aux | grep '[s]idekiq 8' || false"]
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_healthy
      migration:
        condition: service_completed_successfully
    restart: unless-stopped

  # One-shot job: creates the database on first start and runs pending migrations on upgrades.
  migration:
    image: ghcr.io/mastodon/mastodon:latest
    command: bundle exec rails db:prepare
    environment:
      <<: *mastodon-env
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_healthy

  db:
    image: postgres:17-alpine
    shm_size: 256mb
    environment:
      # The database is only reachable inside the compose network.
      POSTGRES_HOST_AUTH_METHOD: "trust"
    volumes:
      - postgres:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD", "pg_isready", "-U", "postgres"]
    restart: unless-stopped

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

volumes:
  system:
  postgres:
  redis:

Values set to changeme are required — replace them with your own values before starting Mastodon.

Prefer a managed setup? WinterFlow installs, configures, and updates Mastodon for you using this same Docker Compose configuration.