Mattermost on Kontena

Mattermost is an open source, self-hosted Slack-alternative. With this tutorial you are able to install Mattermost on Docker with Kontena container platform.

Deploying Mattermost with Kontena has the following benefits compared to the official one-line Docker install option:

  1. it's not using known password strings
  2. it has separated database and application containers
  3. it is stateful, application data is persisted
  4. configuration file is not overridden on re-deploy
  5. support SSL certificates

You can see the complete source code for this tutorial here. There's also option to install Mattermost with MariaDB Galera Cluster.

Getting Started

If you are new to Kontena, please follow the Kontena Quick Start guide. It'll take just 5-10 minutes!

Once you have Kontena installed, you are ready to proceed installing Mattermost!

Install the Application

Before installing the application first step is to write secret passwords to Kontena Vault:

$ kontena vault write MATTERMOST_MYSQL_ROOT_PASSWORD "top_secret"
$ kontena vault write MATTERMOST_MYSQL_PASSWORD "mostest"

After that you can deploy Mattermost with Kontena simply by running command:

$ kontena app deploy

Under the Hood

In the following sections we will go through some fundamentals behind this example.

Separated services

Firstly, we have separated application and database into own services (kontena.yml).

app:  
  image: kontena/mattermost:2.1  
  links:
    - db:db    
  environment:
    - DATABASE_HOST=%{project}-db.kontena.local:3306
  ...

db:  
  image: mariadb:latest
  stateful: true
  environment:
    - MYSQL_USER=mmuser
    - MYSQL_DATABASE=mattermost_production
  ...

Sensitive data

We don't want to leak any sensitive data (passwords, tokens etc), so we won't define any passwords in YAML file. Instead we can store secrets in Kontena Vault

$ kontena vault write MATTERMOST_MYSQL_ROOT_PASSWORD "top_secret"
$ kontena vault write MATTERMOST_MYSQL_PASSWORD "mostest"

and reference them in kontena.yml:

db:  
  image: mariadb:latest
  stateful: true
  secrets:
    - secret: MATTERMOST_MYSQL_ROOT_PASSWORD
      name: MYSQL_ROOT_PASSWORD
      type: env
    - secret: MATTERMOST_MYSQL_PASSWORD
      name: MYSQL_PASSWORD
      type: env
   ...

Stateful services

When re-deploying services we want the data in the database and file uploads will remain between deploys. With Kontena it is very easy to use local file system and persist the data. We just need to define services as stateful and Kontena will take care the rest:

app:  
  image: kontena/mattermost:2.1
  stateful: true
  ...

db:  
  image: mariadb:latest
  stateful: true
  ...

Configuration settings

When deploying the Mattermost container the configuration file is read from Docker image. It means that on every re-deploy all the modifications that have been made via the Mattermost system console are lost. To solve this Kontena has service hooks that enables to run specific operation only once when the service is created the first time:

app:  
  ...
  hooks:
    post_start:
      - cmd: /init-config.sh
        name: init-config
        instances: 1
        oneshot: true

By doing this we can freely modify settings with the Mattermost UI and they will remain between deploys since we have defined app service as stateful.

SSL certificate

On production environment it's recommended to use SSL. With Kontena it's super easy to add certificates to your applications. Recommended way to do this is by using Kontena Vault.

First we add the content of the certificate to Kontena Vault:

$ kontena vault write my_company_cert "$(cat cert.pem)"

And then we define a load balancer service and reference the certificate from Vault:

loadbalancer:  
  image: kontena/lb:latest
  ports:
    - 443:443
  secrets:
    - secret: my_company_cert
      name: SSL_CERTS
      type: env

app:  
  ...
  links:  
    - loadbalancer
  environment:
    - KONTENA_LB_INTERNAL_PORT=80
    - KONTENA_LB_VIRTUAL_PATH=/
  ...

If you want to use Domain name instead of host machine's ip address you can replace KONTENA_LB_VIRTUAL_PATH with KONTENA_LB_VIRTUAL_HOST=mattermost.yourdomain.com

Build your own application

The purpose of this example is to give some ideas how you can deploy your own Mattermost on Docker with Kontena and avoid some common pitfalls. This may not to be the most complete and error-free example, instead you can freely extend this example and build your own Mattermost image and deploy it with Kontena. You can use Kontena's built-in private registry and VPN access for your private image builds.

Building the Mattermost application is very easy. You can refer kontena-devel.yml and run build and deploy commands.

$ kontena app build -f kontena-devel.yml
$ kontena app deploy -f kontena-devel.yml

About Kontena

Kontena is a new open source Docker platform including orchestration, service discovery, overlay networking and all the tools required to run your containerized workloads. Kontena is built to maximize developer happiness. It works on any cloud, it's easy to setup and super simple to use. Give it a try! If you like it, please star it on Github and follow us on Twitter. We hope to see you again!