Multisite configuration with Docker

:warning: While multisite is supported in the Discourse application, this is an advanced sysadmin setup. If you don’t know what you’re doing, do not set up multisite. The Discourse team is unable to provide multisite configuration support.

If you wish to host multiple domains on a singled Docker setup, you’ll need a multisite configuration. Here are the basic building blocks for one.

Understand hooks

Multisite is a fairly advanced topic. Before attempting a multisite build, spend some time to learn about them.

Discourse templates use pups; its rules are simple and powerful.

Each rule you run may define a hook:

run:
  exec:
    cd: some/path
    hook: my_hook
    cmd:
      - echo 1

Later on in your container you can insert rules before or after a hook:

hooks:
  before_my_hook:
    - exec: echo "I ran before"
  after_my_hook:
     - exec: echo "I ran after"

So in the example above you will see output like the following:

I ran before
1
I ran after

You can read through the templates in /var/discourse/templates to see what hooks you have available.

Amend your standalone container to provision the second site tenant

Replace the entire hooks section with:

hooks:
  after_postgres:
     - exec: sudo -u postgres createdb b_discourse || exit 0
     - exec:
          stdin: |
            grant all privileges on database b_discourse to discourse;
          cmd: sudo -u postgres psql b_discourse
          raise_on_fail: false

     - exec: /bin/bash -c 'sudo -u postgres psql b_discourse <<< "alter schema public owner to discourse;"'
     - exec: /bin/bash -c 'sudo -u postgres psql b_discourse <<< "create extension if not exists hstore;"'
     - exec: /bin/bash -c 'sudo -u postgres psql b_discourse <<< "create extension if not exists pg_trgm;"'

  after_code:
    - exec:
        cd: $home/plugins
        cmd:
          - mkdir -p plugins
          - git clone https://github.com/discourse/docker_manager.git
  before_bundle_exec:
    - file:
        path: $home/config/multisite.yml
        contents: |
         secondsite:
           adapter: postgresql
           database: b_discourse
           pool: 25
           timeout: 5000
           db_id: 2
           host_names:
             - b.discourse.example.com

  after_bundle_exec:
    - exec: cd /var/www/discourse && sudo -E -u discourse bundle exec rake multisite:migrate

There are 3 hooks in play:

  1. after_postgres ensures that after postgres is installed an additional db called b_discourse is created with the appropriate permissions.

  2. before_bundle_exec, ensures docker_manager is in place and that the multisite.yml file is in place (which defines where to find the databases)

  3. after_bundle_exec, runs the custom db migration task rake multisite:migrate this ensures all the dbs are up to date.

Note on configuration

The above sample can be split into data container / app container if needed. Just run the after_postgres hook in the data container and the rest in web container.

The above sample can be extended to provision even more DBs. To do so, provision more DBs by duplicating the create db etc calls, and make sure you add additional sites in multisite.yml.

Make sure you amend the host_names node in multisite.yml to match the actual host name you wish to host.

Also, if you plan to run HTTPS, you will need a proxy in front of the site to handle it as the built in letsencrypt functionality will not work in a multisite scenario.

Last edited by @JammyDodger 2024-05-26T20:56:50Z

Check documentPerform check on document:
67 Likes

I was wondering what does the db_id: 2 value in the suggested config above do?

Also, what is the procedure to add a new site to the existing multisite set-up? Is it just:

  • Update yaml
  • ./launcher bootstrap multisite [1]
  • ./launcher start multisite

I was wondering if there was a way to avoid impacting all the other existing sites by doing a full bootstrap or rebuild and somehow just add the extra database needed?


  1. I tried ./launcher rebuild multisite first, but this seems to fail ↩︎

I think that the db id is no longer needed.

You can just edit the multisite file with the new site and do a

sv restart unicorn

And then migrate the database.

1 Like

You are a life-saver!

I was missing this command to restart (I also tried sv reload unicorn but somehow that wasn’t enough).

In terms of creating the database, is there already a way to do this automatically? It would be nice to have rake db:create[mynewdbname]. Otherwise, I can write a short shell script.

At the moment, I create manually (create db and set permissions) and then do a RAILS_DB=newdb rake db:migrate to populate it.

And just to check if I’m doing something stupid:

  • At the moment, I’m editing the multisite file within docker (/var/www/discourse/config/multisite.yml)
  • Creating the db manually
  • Running rake db:migrate
  • sv restart unicorn
  • I then also have to edit the yaml file outside of docker so that the multisite config is in sync in case of any future rebuild or upgrades.

Am I being silly editing the mutisite config in 2 different places when there’s already a way to edit in one place and push the changes through?

Is it possible to clean up this documentation a bit and provide a bit more directions in it? I am extremely new to Docker first off and although I have linux server experience I am not a pro at it. I am an old schooler love my cron jobs and shell scripts and you know weird stuff like that with apache multi site etc. But this is a bit more complex and using a different programming language than I am use to… Maybe even possibly an Demonstration video of some kind? It would be nice if discourse was just made multi site ready and that way all you would have to do is just edit a few files etc.

Just paste in that stanza of text and edit it to suit.

You also then either need a reverse proxy or to follow Set up Let’s Encrypt with multiple domains / redirects to get certs for all of the domains.

See also Setup Multisite Configuration with Let’s Encrypt and no Reverse Proxy. But that needs to get updated to have another line required that you can get from the previous link. I’ve been meaning to update it but haven’t managed

1 Like