Can set up environment variables using docker-compose

Summary
I’m deploying MatterMost using docker-compose in my Terra-Master NAS. I installed docker, docker-compose(docker manager) and created docker-compose.yml following the instruction step-by-step(I’m not using HTTPS/SSO so I skipped step 4&5).

After I deployed MM, I found out that all environment variables set up in docker-compose.yml didn’t work. I looked into the config.yml, all arguments were default values. But if I change the config in MM website, the config.yml changes too, so the problem is MM server didn’t get the environment variables correctly. However, when I docker inspect <mattermost> I can see all the environment variables setting in docker-compose.yml. It seems that MM server doesn’t take the environment variables properly.

Hi Alston! It sounds like you’re on the right track with your Docker setup. To ensure that environment variables are properly passed to the Mattermost server, you may want to verify that the MM_ENVIRONMENT settings are correct in your docker-compose.yml file and check out the Mattermost environment variables documentation for additional guidance. Let us know if this helps!

Thanks for your reply. Here’s my docker-compose.yml content:

# https://docs.docker.com/compose/environment-variables/

version: "3.9"

services:
  postgres:
    image: postgres:alpine3.20
    restart: unless-stopped
    security_opt:
      - no-new-privileges:true
    pids_limit: 100
    read_only: true
    tmpfs:
      - /tmp
      - /var/run/postgresql
    volumes:
      - /Volume2/@apps/docker/db/var/lib/postgresql/data:/var/lib/postgresql/data
    environment:
      # timezone inside container
      - TZ=HKT

      # necessary Postgres options/variables
      - POSTGRES_USER=mmuser
      - POSTGRES_PASSWORD=mmuser_password
      - POSTGRES_DB=mattermost

  mattermost:
    depends_on:
      - postgres
    image: mattermost:10.0
    restart: unless-stopped
    security_opt:
      - no-new-privileges:true
    pids_limit: 200
    read_only: false
    tmpfs:
      - /tmp
    volumes:
      - /Volume2/@apps/docker/mattermost/config:/mattermost/config:rw
      - /Volume2/@apps/docker/mattermost/data:/mattermost/data:rw
      - /Volume2/@apps/docker/mattermost/logs:/mattermost/logs:rw
      - /Volume2/@apps/docker/mattermost/plugins:/mattermost/plugins:rw
      - /Volume2/@apps/docker/mattermost/client/plugins:/mattermost/client/plugins:rw
      - /Volume2/@apps/docker/mattermost/bleve-indexes:/mattermost/bleve-indexes:rw
      # When you want to use SSO with GitLab, you have to add the cert pki chain of GitLab inside Alpine
      # to avoid Token request failed: certificate signed by unknown authority 
      # (link: https://github.com/mattermost/mattermost-server/issues/13059 and https://github.com/mattermost/docker/issues/34)
      # - ${GITLAB_PKI_CHAIN_PATH}:/etc/ssl/certs/pki_chain.pem:ro
    environment:
      # timezone inside container
      - TZ=HKT

      # necessary Mattermost options/variables (see env.example)
      - MM_SQLSETTINGS_DRIVERNAME=postgres
      - MM_SQLSETTINGS_DATASOURCE=postgres://mmuser:mmuser_password@postgres:5432/mattermost?sslmode=disable&connect_timeout=10

      # necessary for bleve
      - MM_BLEVESETTINGS_INDEXDIR=/mattermost/bleve-indexes

      # additional settings
      - MM_SERVICESETTINGS_SITEURL=http://www.altson.top:8065
      
    ports:
      - 8065:8065
      - 8443:8443/udp
      - 8443:8443/tcp

      
# If you use rolling image tags and feel lucky watchtower can automatically pull new images and
# instantiate containers from it. https://containrrr.dev/watchtower/
# Please keep in mind watchtower will have access on the docker socket. This can be a security risk.
#
#  watchtower:
#    container_name: watchtower
#    image: containrrr/watchtower:latest
#    restart: unless-stopped
#    volumes:
#      - /var/run/docker.sock:/var/run/docker.sock

I can’t see any problem so it should be working properly.

When I login SSH and looking in environment variables, they are set as mentioned in docker-compose.yml.

No idea how to fix this. Maybe it’s MatterMost Server itself?

Hi Alston! Thanks for sharing your docker-compose.yml file. It looks like the environment variables are correctly set up, so the issue might be related to how Mattermost is reading these variables. You can try adding the MM_CONFIG environment variable to the Mattermost service to explicitly point to your configuration file like this:

environment:
  - MM_CONFIG=/mattermost/config/config.json

Additionally, ensure that the mattermost/config directory and its contents are accessible and have the correct permissions.

It sounds like you’ve set everything up correctly with your docker-compose.yml, but MatterMost isn’t picking up the environment variables as expected. One possibility is that MatterMost may be prioritizing the settings in config.yml over the environment variables from your docker-compose file. You could try setting "Enable configuration via environment variables" to true in your config.json file to ensure that environment variables are being properly recognized.

Also, make sure you’re using the right format for the variables and check the logs for any errors during the startup process. Another potential fix is to restart the container after making changes to ensure the environment variables take effect. Let us know how it goes!

1 Like