docker-compose.yml - example

version: '3'

networks:
  traefik_network:
    external:
      name: homelabos_traefik
  mastodon:


services:

  db:
    restart: always
    image: postgres:9.6-alpine
#    healthcheck:
#     test: ["CMD", "pg_isready", "-U", "postgres"]
    environment:
      - POSTGRES_PASSWORD=<SET_YOUR_PASSWORD>
    volumes:
      - ./postgres:/var/lib/postgresql/data
    networks:
      - mastodon

  redis:
    restart: always
    image: redis:5.0-alpine
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
    volumes:
      - ./redis:/data
    networks:
      - mastodon

  web:
    image: tootsuite/mastodon:v3.1.4
    restart: always
    env_file: .env.production
    command: bash -c "rm -f /mastodon/tmp/pids/server.pid; bundle exec rails s -p 3000"
#    healthcheck:
#      test: ["CMD-SHELL", "wget -q --spider --proxy=off localhost:3000/health || exit 1"]
    expose:
      - "3000"
    depends_on:
      - db
      - redis
#      - traefik
    labels:

       - "traefik.enable=true"
       - "traefik.docker.network=homelabos_traefik"
       - "traefik.http.services.mastodon.loadbalancer.server.scheme=http"
       - "traefik.http.services.mastodon.loadbalancer.server.port=3000"
       - "traefik.http.routers.mastodon-http.rule=Host(`mastodon.<YOUR_DOMAIN>`)"
       - "traefik.http.routers.mastodon-http.entrypoints=http"
       - "traefik.http.middlewares.redirect-to-https.redirectscheme.scheme=https"
       - "traefik.http.routers.mastodon-http.middlewares=redirect-to-https"
       - "traefik.http.routers.mastodon.rule=Host(`mastodon.<YOUR_DOMAIN>`)"
       - "traefik.http.routers.mastodon.entrypoints=https"
       - "traefik.http.routers.mastodon.middlewares=customFrameHomelab@file"
       - "traefik.http.routers.mastodon.tls=true"
       - "traefik.http.routers.mastodon.tls.certresolver=http"




    volumes:
      - ./public/system:/mastodon/public/system
    networks:
      - traefik_network
      - mastodon

  streaming:
    image: tootsuite/mastodon:v3.1.4
    restart: always
    env_file: .env.production
    command: node ./streaming
#    healthcheck:
#      test: ["CMD-SHELL", "wget -q --spider --proxy=off localhost:4000/api/v1/streaming/health || exit 1"]
    expose:
      - "4000"
    depends_on:
      - db
      - redis
#      - traefik
    labels:

        - "traefik.enable=true"
        - "traefik.docker.network=homelabos_traefik"
        - "traefik.http.services.mastodon-streaming.loadbalancer.server.scheme=http"
        - "traefik.http.services.mastodon-streaming.loadbalancer.server.port=4000"
        - "traefik.http.routers.mastodon-streaming-http.rule=Host(`mastodon-streaming.<YOUR_DOMAIN>`)"
        - "traefik.http.routers.mastodon-streaming-http.entrypoints=http"
        - "traefik.http.middlewares.redirect-to-https.redirectscheme.scheme=https"
        - "traefik.http.routers.mastodon-streaming-http.middlewares=redirect-to-https"
        - "traefik.http.routers.mastodon-streaming.rule=Host(`mastodon-streaming.<YOUR_DOMAIN>`)"
        - "traefik.http.routers.mastodon-streaming.entrypoints=https"
        - "traefik.http.routers.mastodon-streaming.middlewares=customFrameHomelab@file"
        - "traefik.http.routers.mastodon-streaming.tls=true"
        - "traefik.http.routers.mastodon-streaming.tls.certresolver=http"


    networks:
      - traefik_network
      - mastodon

  sidekiq:
    image: tootsuite/mastodon:v3.1.4
    restart: always
    env_file: .env.production
    command: bundle exec sidekiq
    depends_on:
      - db
      - redis
#      - traefik
    volumes:
      - ./public/system:/mastodon/public/system
    networks:
      - traefik_network
      - mastodon

This seems to work quite nicely.

In the same directory run:
touch .env.production
On linux machines: sudo chown 991:991 .env.production

Also: I found I needed to set 991:991 on the 'public' folder to save locally.

sudo chown 991:991 public

docker-compose run --rm -v $(pwd)/.env.production:/opt/mastodon/.env.production web bundle exec rake mastodon:setup
This will guide you through some steps setting up things like Users, Secrets, etc. don’t worry.

You can set up an admin user but may want to make sure you have a working web server. 

If you are using a mailgun relay, you'll need to edit the .env.production file after you have run the command above.

Pay attention to domain though.  Mine is mastodon.lazysquirrel.co.uk for example. After teh above is run, it will spit out a password on the command line. Use this to login with your admin account.

docker-compose up -d

It's based on the work here: https://www.innoq.com/en/blog/traefik-v2-and-mastodon/

Editing .env.production for use with mailgun relay:

SMTP_SERVER=smtp.eu.mailgun.org
SMTP_PORT=587
SMTP_LOGIN=postmaster@mg.<YOUR_DOMAIN>
SMTP_PASSWORD=<MAILGUN PASSWD>
#SMTP_AUTH_METHOD=none
#SMTP_OPENSSL_VERIFY_MODE=none
SMTP_FROM_ADDRESS=<YOU>@<YOUR_DOMAIN>

There are some other options on the Mastodon documentation.