Programster's Blog

Tutorials focusing on Linux, programming, and open-source

Deploy Zabbix Through Docker

Introduction

Zabbix is an open-source monitoring platform that might be thought of as an alternative to Nagios. I'm sure there are many online discussions comparing the two tools (such as this one), but that's beyond the scope of this tutorial.

Server Requirements

I would recommend deploying a server with at least 2 GB of RAM, preferably more.

Steps

You can run the following bash script to deploy Zabbix on your server. Just make sure to update the settings at the top of the file.

Create Docker Compose File

services:
  db:
    container_name: zabbix-mysql-database
    image: mariadb:10.11-jammy
    restart: unless-stopped
    volumes:
      - ./volumes/mysql:/var/lib/mysql
    environment:
      - MYSQL_DATABASE=zabbix
      - MYSQL_USER=zabbix
      - MYSQL_PASSWORD=${MYSQL_PASSWORD}
      - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}

  zabbix-server:
    image: zabbix/zabbix-server-mysql:ubuntu-6.2-latest
    container_name: db
    restart: unless-stopped
    ports:
      - "10050:10050"
      - "10051:10051"
    volumes:
      - ./volumes/alertscripts:/usr/lib/zabbix/alertscripts
      - ./volumes/externalscripts:/usr/lib/zabbix/externalscripts
      - ./volumes/modules:/var/lib/zabbix/modules
      - ./volumes/enc:/var/lib/zabbix/enc
      - ./volumes/ssh_keys:/var/lib/zabbix/ssh_keys
      - ./volumes/ssl/certs:/var/lib/zabbix/ssl/certs
      - ./volumes/ssl/keys:/var/lib/zabbix/ssl/keys
      - ./volumes/ssl_ca:/var/lib/zabbix/ssl/ssl_ca
      - ./volumes/snmptraps:/var/lib/zabbix/snmptraps
      - ./volumes/mibs:/var/lib/zabbix/mibs
    environment:
      - DB_SERVER_HOST=db
      - MYSQL_USER=zabbix
      - MYSQL_PASSWORD=${MYSQL_PASSWORD}
    depends_on:
      - db

  zabbix-nginx:
    image: zabbix/zabbix-web-nginx-mysql:ubuntu-6.2-latest
    container_name: zabbix-web-nginx
    restart: unless-stopped
    ports:
      - "80:8080"
      - "443:8443"
    depends_on:
      - zabbix-server
    environment:
      - DB_SERVER_HOST=db
      - MYSQL_USER=zabbix
      - MYSQL_PASSWORD=${MYSQL_PASSWORD}
      - PHP_TZ=UTC
      - ZBX_SERVER_HOST=zabbix-server

If you use the MySQL 8.x Docker image instead of MariaDB, then you get an error to do with collation.

Web Login

After having run the script, wait rough 30 seconds to a minute before visiting your server's IP address or hostname if your browser. If you waited long enough, you will see a login screen, rather than an error message. If you see an error message, just wait longer.

The default credentials to log in are:

  • username: Admin
  • password: zabbix

Optional - Configure SSL/TLS

In order to support HTTPS web connections to the Zabbix server, one could deploy a reverse proxy, or one could perform the steps below:

Create SSL Folder Of Certificates

Create a folder for the TLS certificates to go in:

sudo mkdir -p ./volumes/nginx/ssl
sudo chown $USER:$USER ./volumes/nginx/ssl

Then generate and copy your TLS certificates into that folder (./volumes/nginx/ssl) with the following names:

  • ssl.crt - your full site certificate file for nginx (e.g. sometimes referred to as fullchain.pem by Let's Encrypt)
  • ssl.key - your private key file.

Diffie-Hellman Parameters File

We also need a file called dhparam.pem file in there. If you already have it then just put it in there. If you are like me and don't then you can just create one with the following command:

openssl dhparam -out dhparam.pem 2048

Update Docker Compose File

Now we need to add that folder as a volume to the webserver container. This can easily be done by replacing the zabbix-nginx section with the following:

  zabbix-nginx:
    image: zabbix/zabbix-web-nginx-mysql:ubuntu-6.2-latest
    container_name: zabbix-web-nginx
    restart: unless-stopped
    ports:
      - "80:8080"
      - "443:8443"
    depends_on:
      - zabbix-server
    volumes:
      - ./volumes/nginx/ssl:/etc/ssl/nginx:ro
    environment:
      - DB_SERVER_HOST=db
      - MYSQL_USER=zabbix
      - MYSQL_PASSWORD=${MYSQL_PASSWORD}
      - PHP_TZ=UTC
      - ZBX_SERVER_HOST=zabbix-server

The only important bit we are adding is the volumes: section. If your docker-compose file looks different, just copy those lines as appropriate into your YAML config file.

Restart

Once you have all three of those files in place with the correct names, and the docker compose configuration has been updated to mount that within the webserver, we can restart the web container for it to now support TLS connections.

The easiest way to do this is to just run:

docker compose restart

Appendix

Docker Run Commands

When I originally created this tutorial, it was with a BASH script that started the relevant containers/images through docker run commands rather than making use of docker compose. That script is below for reference:

#!/bin/bash

# Change these settings
MYSQL_ROOT_PASSWORD="changeme123"
ZABBIX_DB_USER_PASSWORD="changeMeAgain123"
SERVER_IP="192.168.1.54"


# Deploy a mysql container for zabbix to use.
docker run -d \
  --name="zabbix-mysql-database" \
  --restart=always \
  -p 3306:3306 \
  -e MYSQL_DATABASE=zabbix \
  -e MYSQL_USER=zabbix \
  -e MYSQL_PASSWORD=$ZABBIX_DB_USER_PASSWORD \
  -e MYSQL_ROOT_PASSWORD=$MYSQL_ROOT_PASSWORD \
  -v $HOME/volumes/mysql:/var/lib/mysql \
  mariadb:10.11-jammy

# Deploy the zabbix-server application container
docker run -d \
  --name zabbix-server \
  --restart=always \
  -e DB_SERVER_HOST=$SERVER_IP \
  -e MYSQL_USER="zabbix" \
  -e MYSQL_PASSWORD=$ZABBIX_DB_USER_PASSWORD \
  -v $HOME/volumes/zabbix/alertscripts:/usr/lib/zabbix/alertscripts \
  -v $HOME/volumes/zabbix/externalscripts:/usr/lib/zabbix/externalscripts \
  -v $HOME/volumes/zabbix/modules:/var/lib/zabbix/modules \
  -v $HOME/volumes/zabbix/enc:/var/lib/zabbix/enc \
  -v $HOME/volumes/zabbix/ssh_keys:/var/lib/zabbix/ssh_keys \
  -v $HOME/volumes/zabbix/ssl/certs:/var/lib/zabbix/ssl/certs \
  -v $HOME/volumes/zabbix/ssl/keys:/var/lib/zabbix/ssl/keys \
  -v $HOME/volumes/zabbix/ssl_ca:/var/lib/zabbix/ssl/ssl_ca \
  -v $HOME/volumes/zabbix/snmptraps:/var/lib/zabbix/snmptraps \
  -v $HOME/volumes/zabbix/mibs:/var/lib/zabbix/mibs \
  -p 10050:10050 \
  -p 10051:10051 \
  zabbix/zabbix-server-mysql:ubuntu-6.2-latest

# Deploy the webserver frontend.
docker run -d \
  --name zabbix-web-nginx \
  --restart=always \
  -e DB_SERVER_HOST="$SERVER_IP" \
  -e MYSQL_USER="zabbix" \
  -e MYSQL_PASSWORD=$ZABBIX_DB_USER_PASSWORD \
  -e ZBX_SERVER_HOST=$SERVER_IP \
  -e PHP_TZ="UTC" \
  -p 80:80 \
  -p 443:443 \
  -p 8080:8080 \
  -p 8443:8443 \
  zabbix/zabbix-web-nginx-mysql:ubuntu-6.2-latest

If you use the MySQL 8.x Docker image instead of MariaDB, then you get an error to do with collation. It is easier just to use the MariaDB image.

References

Last updated: 16th July 2024
First published: 16th August 2018