Self-hosted Matomo using Docker

Maybe you are also interested in the number of visitors of your website(s). At least, I am. However, my interest is limited to basic values like number of visitors, visited sites and duration of the visit. Google Analytics is never an option for me, that’s why I’m using Matomo, formerly Piwik.

As described in my initial post of this series, my goal is to have multiple websites separately running in Docker. Those websites are running behind Traefik, which is enabling secure HTTPS connections and forwarding client requests to the appropriate Docker containers. One of those Docker containers is Matomo. More precisely, the web server container communicating with the container running Matomo.

If you read my posts about setting up WordPress or Nextcloud, you will see some similarities. I have three well-known containers: One container running Matomo, a database container and a web server container.

1. Database

See my other posts about some details of starting a database container.

services:
  db:
    image: mysql:5.7.20
    restart: always
    volumes:
      - /path/to/my/matomo/mysql/runtime:/var/lib/mysql
    secrets:
      - matomo_db
    networks:
      - internal
    labels:
      - traefik.enable=false
services:
  db:
    image: mysql:5.7.20
    restart: always
    volumes:
      - /path/to/my/matomo/mysql/runtime:/var/lib/mysql
    secrets:
      - matomo_db
    networks:
      - internal
    labels:
      - traefik.enable=false
MYSQL_ROOT_PASSWORD=...
MYSQL_PASSWORD=...
MYSQL_DATABASE=db
MYSQL_USER=db_user

2. Web server

See my other posts about some details of starting a web server container.

volumes:
  matomo_volume:

web:
  image: nginx
  restart: always
  volumes:
    - ./nginx.conf:/etc/nginx/nginx.conf:ro
    - matomo_volume:/var/www/html
  links:
    - app
  networks:
    - internal
    - proxy
  labels:
    - traefik.enable=true
    - traefik.backend=matomo
    - traefik.frontend.rule=Host:matomo.domain.net
    - traefik.docker.network=proxy

3. Matomo

Matomo is providing a Docker image which I’m using. In addition to the default configuration to start a Docker container using docker-compose, the necessary additional configuration is limited to storing files outside the container (remember, when a container crashes, stops or is being deleted, the changed files are also gone).

app:
  image: piwik:fpm
  restart: always
  links:
    - db
  volumes:
    - /path/to/matomo/config:/var/www/html/config
    - matomo_volume:/var/www/html
  networks:
    - internal
  labels:
    - traefik.enable=false

Summary

Setting up Matomo in a Docker container is quite simple. It’s similar to numerous other web services, for example as I described in posts for WordPress and Nextcloud. Starting three connected containers (Matomo, web server, database), adding a password here and there and save the data outside the containers. Now I am able to add Matomo’s tracking code to send data from your homepage to Matomo using an HTTPS connection.

Leave a Reply

Your email address will not be published. Required fields are marked *