Dockerizing a Matomo (formerly Piwik) web analytics app

Posted by in DevOps, updated on

Introduction

Matomo (formerly known as Piwik) is an open source web analytics application that runs on PHP and a MySQL database, which I've been using instead of Google Analytics.

This is the configuration I use to deploy it as described here.

I'm using the official matomo and mariadb images and don't need to build anything, so all I need to deploy the app is a docker-compose.yml file and a .env file.

The docker-compose.yml file

I'm using the matomo:3.13.5 (also tagged as matomo:3.13.5-apache) which uses an Apache server and serves the app on port 80. Port 8080 is mapped to my host.

version: '3.7'

services:
app:
image: matomo:3.13.5
restart: always
volumes:
- data:/var/www/html
environment:
MATOMO_DATABASE_HOST: db
ports:
- 8080:80
depends_on:
- db

db:
image: mariadb:10.4.12
command: --max-allowed-packet=64MB
restart: always
volumes:
- db:/var/lib/mysql
env_file:
- matomo.env

volumes:
db:
data:

Note that we need to specify the MATOMO_DATABASE_HOST and the name we pass needs to match the service name we've given mariadb, which is db in this case.

We're creating two named volumes, one for Matomo's data (data:/var/www/html) and one for the database data (db:/var/lib/mysql).

Lastly we're passing the db service a .env file which will hold our database credentials which we'll later use to connect the app to the database.

The .env file

In matomo.env file I've specified the following:

MYSQL_ROOT_PASSWORD=your_root_password
MYSQL_DATABASE=your_db_name
MYSQL_USER=your_username
MYSQL_PASSWORD=your_user_password

The above can be any names and passwords you want, and we'll use them in the next section.

Note that the .env file can be named anything you want as long as it corresponds to the name supplied in the docker-compose.yml file.

Note: don't commit this file to your version control system, I usually ignore all *.env files in my .gitignore file.

Running the app

Next run the app locally with:

docker-compose up

Make sure you're in the same folder as your docker-compose.yml when running that command. If you haven't downloaded the images before then docker-compose will first download them and then run them.

Once the services are ready visit http://localhost:8080, you should see the Matomo welcome page. Click next twice until you reach the Database Setup step and fill in the following:

  1. Database Server: leave as db
  2. Login: your MYSQL_USER
  3. Password: your MYSQL_PASSWORD
  4. Database Name: your MYSQL_DATABASE
  5. Leave the rest as defaults
  6. Click NEXT

You should see a "Tables created with success!" message.

After that you can create your login which you'll use to log in to the app in future (make these different to your database credentials) and set up a website and tracking code.

Geolocation data

The default geolocation tracking in Matomo is via the user's language, which isn't very accurate. For example you can live in Australia and set your computer's language as English US, Matomo will then display that user as accessing your site from North America.

To fix this go to settings (accessed via the cog icon on the top bar) and then select Geolocation on the left sidebar. I'm using the free DBIP option, to enable it scroll to the bottom use the inbuilt Matomo feature to download it. You can also set a weekly or monthly frequency for Matomo to automatically update it.

Don't forget to scroll back up and click on the DBIP / GeoIP 2 (Php) option to enable it.

Recent Posts