Multisite configuration with Docker

You may wish to host multiple domains on a singled Docker setup. To do so follow these instructions:

We strongly recommend you run separate web and data containers

The standalone container is extremely easy to configure, however has a couple of drawbacks

  1. You will need to stop your site when bootstrapping a new image
  2. You run through the db bootstrap only when needed

Multisite is a fairly advanced topic, learn about hooks before attempting this.

Understand hooks

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 site and talk to it

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.

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 have 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.

53 Likes
How does multisite.yml look?
How to create 1000 subcategories in a scalable way?
Multiple Forums in a Single Installation
Pros and cons of multiple standalone containers vs one for multiple Discourse installs?
Multiple Discourses, multiple containers, one server
Success - New Multisite Install on Dedicated server using ServerPilot, Nginx and Apache
Multisite Installation
Will I need to start over with multisite configuration from standalone version?
Installation wizard not appearing in multisite installs
Disabling/restricting user search
Starting a second Discourse forum on the same VPS
Sandbox and test discourse on host?
Multiple Discourse Installs - Single Server
How do you set up multiple discourse forums on the same server?
How to move from standalone container to separate web and data containers
Multi-site Installation
Discourse multisite installation help with digitalocean
Need proper documentation for multisite discourse with docker
Pros and cons of a multisite installation
Install Discourse on Plesk / Ubuntu 14 without Docker
Multisite viability - 2 read-only and 1 active
Multisite installation with seperated smtp emails
Three sites in one setup
Discourse for 3 different Countrys
Totally Walled-Off Groups
I want to host hundreds of instances of Discourse
Second discourse instance on the same server
Can I log into multiple instances of discourse simultaneously?
Multiple discourse instances in a single server
How to move from standalone container to separate web and data containers
Help me setup many websites on one VPS
How can Admin login while Read-only mode is enabled on multisite?
Installing on Kubernetes
How to set redis DB ID in Docker container?
Recover to one site from multisite
Running other websites on the same machine as Discourse
Multiple container setup problems
Feature proposal: Customizations based on primary group membership, a.k.a. tenancy 'lite'
Stable branch assets won't compile
Stable branch assets won't compile
Pull request for Wikis?
PAID Require Discourse expert for extreme customization
Multitenat Dockerize Discourse
Add path to cookie
App.yml shared volumes for a two website setup
Contabo experience with Plesk, Webmin or other server-admin setup?
How to move from standalone container to separate web and data containers
Using a launcher built docker image in docker-compose
How to move from standalone container to separate web and data containers
Two standalone instances on one server?
Best approach for test and production instance of discourse
Multisite Config
Are there other workarounds to not having category moderation such as multiple instances of the board?
ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column categories.search_priority does not exist
Multisite installation ... in subdirectories?
Multiple discourse instances in a single server
How do I set category tracking level defaults historically
Can we change lets encrypt script?
Integration with .NET MVC application for a SaaS platform
Multisite for small collection of sites for a family?
Need help with multisite configuration
Hosting Plan Suggestion for 500 Concurrent Online Users
Avatar, Site Logos, and Cert Errors
Secure cookie configuration
How to move from standalone container to separate web and data containers
Can I have two domains pointing towards the same IP address without redirect?

@jomaxro can a tech advocate confirm the guide is accurate?

5 Likes

If you are having issues with this guide, open a separate support topic, this is closed pending verification

4 Likes