↓ Skip to main content

Deploying Zabbix with Docker

··17 mins·
Daniel Lima
Author
Daniel Lima

In this article, we will explore how to deploy Zabbix using Docker and Docker Compose, from preparing environment variables to configuring the services responsible for running the platform.

The goal is to build an organized environment in which each Zabbix component runs in its own container. This approach allows us to manage services individually, simplify future updates, and keep data persistent regardless of the containers’ lifecycle.

All files used in this deployment are available on GitHub: netns/zbx-docker. The repository contains the Docker Compose configuration files and other resources required to reproduce the environment presented in this article.

A brief introduction to how Docker works
#

Docker is a containerization platform that allows applications and their dependencies to be packaged into isolated units called containers. Unlike traditional virtual machines, containers share the host operating system’s kernel, reducing resource consumption and simplifying application distribution.

Docker Compose complements this functionality by allowing us to define and manage applications composed of multiple containers through a single YAML file. Within this file, we can declare services, images, environment variables, volumes, networks, and restart policies.

In the case of Zabbix, this approach allows us to separate components such as the server, web interface, and database while maintaining communication between them through an internal Docker network.

For more information, refer to the official Docker documentation and the Docker Compose documentation.

Docker Compose Services
#

Before defining the services, let’s organize the variables used throughout the deployment.

Docker Compose allows us to use a file named .env to centralize values that will be referenced in the YAML configuration file. This avoids repeating information and makes the environment easier to maintain.

An example .env file:

POSTGRES_PASSWORD=strong-password-here

ZBX_SERVER_NAME="Zabbix Docker"
PHP_TZ=America/Sao_Paulo

# HA (uncomment only in multi-node deployments)
# ZBX_HANODENAME=zabbix-node-1
# ZBX_NODEADDRESS=zabbix-server

ZBX_STARTPOLLERS=10
ZBX_STARTPOLLERSUNREACHABLE=3
ZBX_STARTPINGERS=3
ZBX_STARTPREPROCESSORS=5
ZBX_STARTDISCOVERERS=2

ZBX_CACHESIZE=32M
ZBX_HISTORYCACHESIZE=64M
ZBX_HISTORYINDEXCACHESIZE=16M
ZBX_TRENDCACHESIZE=16M
ZBX_VALUECACHESIZE=32M

ZBX_CACHEUPDATEFREQUENCY=10
ZBX_HOUSEKEEPINGFREQUENCY=1
ZBX_MAXHOUSEKEEPERDELETE=5000
ZBX_LOGSLOWQUERIES=3000

The variables above represent different configurations for the components. Some are used by Docker Compose to substitute values in the YAML file, while others correspond directly to Zabbix configuration parameters.

It is important to note that the Docker Compose .env file does not automatically make all its variables available inside containers. To achieve this, they must be explicitly referenced through the environment attribute or made available using env_file in the service definition.

Warning

The snippets presented in this article show only the service definitions. The root-level volume and network declarations, along with the remaining parameters, are included in the complete file available in the repository: zbx_srv_postgres.yml. To reproduce the environment, use the repository file rather than assembling the YAML configuration using only the fragments shown below.

Tip

The ZBX_HANODENAME and ZBX_NODEADDRESS variables are related to Zabbix Server High Availability (HA) configuration. In a single-node deployment, these variables are not required. Refer to the official Zabbix HA documentation for further information.

Caution

Do not commit .env files containing credentials or sensitive information to public repositories. Add the file to .gitignore and, in production environments, consider using dedicated secrets management mechanisms.

Database
#

The database is one of the most important components of the Zabbix architecture. It stores information about hosts, monitored items, events, history, trends, and platform configurations.

For smaller environments, the database can run in a container on the same machine as the other components. However, in larger environments or those with stricter availability and performance requirements, it is worth considering running the database on dedicated infrastructure.

In this example, we will use PostgreSQL 18:

zabbix-db:
    image: postgres:18
    container_name: zabbix-db
    environment:
      POSTGRES_DB: zabbix
      POSTGRES_USER: zabbix
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
    volumes:
      - db-data:/var/lib/postgresql
    restart: unless-stopped
    networks:
      - zabbix-net
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}"]
      interval: 30s
      timeout: 5s
      retries: 5

Let’s take a look at the main parameters used.

  • image: defines the image used to create the container. In this case, we use the official PostgreSQL image, version 18.

  • container_name: assigns a name to the container, making it easier to identify and manage.

  • environment: provides environment variables used during PostgreSQL initialization. Here, we define the database name, username, and password.

  • volumes: attaches a persistent Docker volume to the PostgreSQL data directory.

  • restart: defines the restart policy. With unless-stopped, the container will restart after a failure or Docker restart, unless it has been manually stopped.

  • networks: connects the service to the internal network used by the other Zabbix components.

Data Persistence with Docker Volumes
#

One aspect that deserves attention in a containerized deployment is data persistence.

By default, data stored in a container’s writable layer is tied to its lifecycle. If the container is removed, this data may also be lost.

To prevent this issue, Docker provides volumes, which allow data to be stored outside the containers’ writable layers.

In the previous example, we used a named volume called db-data, mounted at the /var/lib/postgresql directory inside the container.

This way, even if the container is removed and recreated, the database data remains stored in the volume, provided that the volume itself is not explicitly removed.

For more information, refer to the official Docker Volumes documentation.

Zabbix Server
#

The Zabbix Server is the core component of the platform. It is responsible for data collection and processing, executing checks, evaluating triggers, managing events, and sending notifications.

In this deployment, we will use the official zabbix/zabbix-server-pgsql image, version 8.0, which is the latest Long-term Support (LTS) release based on Ubuntu.

zabbix-server:
    image: zabbix/zabbix-server-pgsql:ubuntu-8.0-latest
    container_name: zabbix-server
    depends_on:
          zabbix-db:
            condition: service_healthy
    environment:
      # Database Config
      DB_SERVER_HOST: zabbix-db
      POSTGRES_DB: zabbix
      POSTGRES_USER: zabbix
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}

      # HA Config
      ZBX_HANODENAME: ${ZBX_HANODENAME}
      ZBX_NODEADDRESS: ${ZBX_NODEADDRESS}

      # Pollers Tuning
      ZBX_STARTPOLLERS: ${ZBX_STARTPOLLERS}
      ZBX_STARTPOLLERSUNREACHABLE: ${ZBX_STARTPOLLERSUNREACHABLE}
      ZBX_STARTPINGERS: ${ZBX_STARTPINGERS}
      ZBX_STARTPREPROCESSORS: ${ZBX_STARTPREPROCESSORS}
      ZBX_STARTDISCOVERERS: ${ZBX_STARTDISCOVERERS}

      # Mem and Cache Tuning
      ZBX_CACHESIZE: ${ZBX_CACHESIZE}
      ZBX_HISTORYCACHESIZE: ${ZBX_HISTORYCACHESIZE}
      ZBX_HISTORYINDEXCACHESIZE: ${ZBX_HISTORYINDEXCACHESIZE}
      ZBX_TRENDCACHESIZE: ${ZBX_TRENDCACHESIZE}
      ZBX_VALUECACHESIZE: ${ZBX_VALUECACHESIZE}

      # Housekeeper
      ZBX_CACHEUPDATEFREQUENCY: ${ZBX_CACHEUPDATEFREQUENCY}
      ZBX_HOUSEKEEPINGFREQUENCY: ${ZBX_HOUSEKEEPINGFREQUENCY}
      ZBX_MAXHOUSEKEEPERDELETE: ${ZBX_MAXHOUSEKEEPERDELETE}
      ZBX_LOGSLOWQUERIES: ${ZBX_LOGSLOWQUERIES}

      # Other
      ZBX_ENABLE_SNMP_TRAPS: true
    ports:
      - "10051:10051"
    volumes:
      - zabbix-alertscripts:/usr/lib/zabbix/alertscripts
      - zabbix-externalscripts:/usr/lib/zabbix/externalscripts
      - zabbix-modules:/var/lib/zabbix/modules
      - zabbix-enc:/var/lib/zabbix/enc
      - zabbix-ssh-keys:/var/lib/zabbix/ssh_keys
      - zabbix-ssl-certs:/var/lib/zabbix/ssl/certs
      - zabbix-ssl-keys:/var/lib/zabbix/ssl/keys
      - zabbix-ssl-ca:/var/lib/zabbix/ssl/ssl_ca:ro
      - zabbix-snmptraps-data:/var/lib/zabbix/snmptraps
      - zabbix-export-data:/var/lib/zabbix/export
      - zabbix-mibs:/var/lib/zabbix/mibs
    restart: unless-stopped
    networks:
      - zabbix-net

Database Connection
#

The DB_SERVER_HOST, POSTGRES_DB, POSTGRES_USER, and POSTGRES_PASSWORD variables are used to establish the connection between Zabbix Server and PostgreSQL.

Notice that we use zabbix-db as the database hostname. This is possible because both services share the same Docker network, allowing them to communicate using their respective service names as internal DNS addresses.

Process and Cache Management
#

Zabbix Server uses different processes to perform monitoring tasks. Through variables such as ZBX_STARTPOLLERS, ZBX_STARTPINGERS, ZBX_STARTPREPROCESSORS, and others, we can adjust the number of processes available for each task.

We can also define the amount of memory allocated to the server’s internal caches using parameters such as ZBX_CACHESIZE, ZBX_HISTORYCACHESIZE, and ZBX_TRENDCACHESIZE.

These parameters directly affect resource consumption and platform performance. Their values should be defined based on the number of monitored hosts, collected items, update intervals, and available processing capacity.

SNMP Traps and MIB Files
#

The ZBX_ENABLE_SNMP_TRAPS variable enables SNMP trap processing on Zabbix Server.

For this feature to work, the trap file generated by the zabbix-snmptraps service must be made available through a shared volume between the two containers.

The /var/lib/zabbix/mibs volume allows additional MIB files to be made available for interpreting OIDs during network device monitoring.

The /var/lib/zabbix/export volume is used to store real-time export files containing events, history, and trends when this feature is enabled.

Ports and Persistence
#

TCP port 10051 is used by Zabbix Server to receive connections from active agents and proxies. Publishing this port on the host allows components outside the Docker network to reach the service.

Finally, the volumes ensure that files used by additional features remain persistent, regardless of container recreation.

Zabbix SNMP Traps
#

The SNMP Trapper is responsible for receiving SNMP notifications sent by monitored devices, such as switches, routers, firewalls, and other network equipment.

Unlike traditional SNMP polling, in which Zabbix periodically queries devices, traps are sent asynchronously by the devices themselves when specific events occur.

In a Docker deployment, we can use a separate container to run the service responsible for receiving these notifications.

To enable this functionality, we need to set the ZBX_ENABLE_SNMP_TRAPS variable to true in Zabbix Server and share the /var/lib/zabbix/snmptraps volume between the two containers.

Example configuration:

zabbix-snmp-traps:
  image: zabbix/zabbix-snmptraps:ubuntu-8.0-latest
  container_name: zabbix-snmp-traps
  depends_on:
    zabbix-server:
      condition: service_started
  ports:
    - "162:1162/udp"
  volumes:
    - zabbix-snmptraps-data:/var/lib/zabbix/snmptraps
    - zabbix-mibs:/var/lib/zabbix/mibs
  restart: unless-stopped
  networks:
    - zabbix-net

In this example, we publish UDP port 162 on the host and map it to UDP port 1162 inside the container, which is used by the trap reception service in the official image.

The shared volume allows the records received by the zabbix-snmptraps container to be made available to Zabbix Server for processing.

It is important to remember that configuring the container alone does not guarantee that SNMP Traps will work correctly. The network devices must also be configured to send notifications to the Docker host’s IP address, UDP traffic must be allowed, and the corresponding items must be configured in Zabbix.

Zabbix Frontend
#

The Zabbix Frontend is the web interface used to administer the platform. Through it, we can register hosts, configure templates, create monitoring items, track problems, view graphs, and manage users.

For this deployment, we will use the official zabbix/zabbix-web-nginx-pgsql image, which combines the Nginx web server with the Zabbix PHP frontend, using PostgreSQL as its database.

Running the Frontend in a separate container allows us to manage the interface independently of Zabbix Server.

zabbix-web:
  image: zabbix/zabbix-web-nginx-pgsql:ubuntu-8.0-latest
  container_name: zabbix-web
  depends_on:
    zabbix-db:
      condition: service_healthy
    zabbix-server:
      condition: service_started
  environment:
    # Database Config
    DB_SERVER_HOST: zabbix-db
    POSTGRES_DB: zabbix
    POSTGRES_USER: zabbix
    POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}

    # Zabbix Server Config
    ZBX_SERVER_HOST: zabbix-server
    ZBX_SERVER_PORT: 10051
    ZBX_SERVER_NAME: ${ZBX_SERVER_NAME}

    # PHP Config
    PHP_TZ: ${PHP_TZ}
  ports:
    - "8080:8080"
  restart: unless-stopped
  networks:
    - zabbix-net

Some parameters deserve special attention:

  • DB_SERVER_HOST: address of the container responsible for the database.
  • POSTGRES_DB, POSTGRES_USER, and POSTGRES_PASSWORD: credentials used to access PostgreSQL.
  • ZBX_SERVER_HOST: address of Zabbix Server. Since both services are on the same Docker network, we use the service name.
  • ZBX_SERVER_PORT: port used for communication with Zabbix Server.
  • ZBX_SERVER_NAME: installation identifier displayed in the web interface.
  • PHP_TZ: defines the timezone used by PHP, preventing discrepancies in the display of dates and times.

Host port 8080 will be mapped to port 8080 inside the container, allowing access to the interface through a web browser at http://SERVER_IP:8080.

In production environments, I recommend using a reverse proxy with HTTPS, avoiding direct exposure of the web interface without encryption.

Zabbix Agent
#

The Zabbix Agent is a component responsible for collecting information about the operating system and applications running on the monitored host, such as CPU usage, memory utilization, disk space, network interfaces, and processes.

Zabbix provides two implementations: the traditional Agent and Agent 2. The second version was developed with a plugin-based architecture, offering greater flexibility for integration with different technologies.

In this example, we will use Agent 2:

  zabbix-agent:
    image: zabbix/zabbix-agent2:ubuntu-8.0-latest
    container_name: zabbix-agent
    depends_on:
     zabbix-server:
       condition: service_started
    environment:
      ZBX_SERVER_HOST: zabbix-server
      ZBX_SERVER_PORT: 10051
      ZBX_HOSTNAME: "Zabbix server"
    restart: unless-stopped
    networks:
      - zabbix-net

The ZBX_SERVER_HOST variable defines the address of Zabbix Server, while ZBX_HOSTNAME specifies the identification name of the monitored host. The latter must exactly match the name configured in the Zabbix frontend.

Since the containers share the zabbix-net network, we can use the zabbix-server service name to establish communication between them, taking advantage of Docker’s built-in internal DNS resolution.

This way, Agent 2 becomes available to monitor the containerized environment itself and collect host information, depending on the permissions and resources made available to the container.

Important

It is important to distinguish between container monitoring and host operating system monitoring.

By default, Docker containers run in isolated environments and do not have access to most host resources. Therefore, Zabbix Agent 2 will be able to collect information about the container itself, but it will not have complete visibility into the resources of the host machine.

If the goal is to monitor the host, the container must be run with additional privileges (privileged) or provided with the system volumes and resources required to collect metrics.

Refer to the official image documentation to check the required volumes and permissions: Zabbix Agent 2 – Docker Hub.

Starting the Services
#

With the services configured, we can start the deployment using Docker Compose.

Run the following command in the directory where the Docker Compose configuration file is stored. In this example, we will use the filename zbx_srv_postgres.yml, but you can choose another name according to your organizational preferences:

docker compose -f zbx_srv_postgres.yml up -d

If an .env file is being used, it must also be available in the working directory or explicitly referenced in the configuration.

The -f parameter specifies the Compose file to be used, while -d runs the containers in the background (detached mode).

After execution, Docker Compose will create the network, volumes, and containers defined in the configuration file.

To check the status of the services, run:

docker compose -f zbx_srv_postgres.yml ps

If any service fails to start correctly, check its logs using:

docker compose -f zbx_srv_postgres.yml logs -f

Once all components are running, the Zabbix web interface will be available at:

http://SERVER_IP:8080.

From this point onward, we can access the Frontend and begin configuring the monitoring environment.

Self-Monitoring
#

By default, Zabbix Server self-monitoring may use the address 127.0.0.1 to perform queries to the Agent. However, inside a container, this loopback address always refers to the container itself, rather than another service in the infrastructure.

Since we are running Zabbix Server and Agent 2 in separate containers, we need to modify the interface configuration of the host responsible for self-monitoring.

In the web interface, navigate to the configuration of the Zabbix server host and change the Agent interface address to use DNS, specifying zabbix-agent as the address.

Since both containers share the zabbix-net network, Docker can automatically resolve the zabbix-agent service name, directing queries to the correct address.

After this change, Zabbix Server will be able to query Agent 2 normally, allowing us to monitor the resources and operation of the environment.

Database Backup Procedure
#

One of the simplest ways to back up a database is to use a tool such as pg_dump for PostgreSQL or mysqldump for MySQL/MariaDB. These tools are generally more suitable for smaller environments, as they do not provide features such as Point-in-Time Recovery (PITR) or take advantage of Write-Ahead Logging (WAL) for continuous archiving. At the end of this section, I have included some articles that explore these tools in greater detail.

The procedure for creating a database dump in Docker is shown in the example below. In this case, we use pg_dump, but for MySQL/MariaDB, the command structure is similar, using mysqldump instead.

docker exec -T zabbix-db pg_dump -U zabbix -d zabbix \
  > dump_$(date +%Y-%m-%d_%H_%M_%S).sql

It is also possible to create compressed dumps. In this example, I use pigz, a multithreaded implementation of gzip, but you can use whichever tool you prefer.

docker exec -T zabbix-db pg_dump -U zabbix -d zabbix \
  | pigz --best > dump_$(date +%Y-%m-%d_%H_%M_%S).sql.gz

To restore an uncompressed dump, simply run:

docker exec -iT zabbix-db psql -U zabbix -d zabbix < dump.sql

To restore a gzip-compressed file, use:

gunzip -c dump.sql.gz | docker exec -iT zabbix-db psql -U zabbix -d zabbix

Another alternative is to use PostgreSQL’s Custom format, generated with the -Fc parameter in pg_dump. This format allows you to use pg_restore for restoration, providing additional features such as object selection and parallel processing.

To create the backup:

docker exec -T zabbix-db pg_dump -U zabbix -d zabbix -Fc \
  > dump_$(date +%Y-%m-%d_%H_%M_%S).dump

To restore it, run:

docker exec -iT zabbix-db pg_restore -U zabbix -d zabbix \
  < backup.dump

Unlike psql, pg_restore supports parallel restoration using the -j parameter. For this, the backup file must be directly accessible inside the container:

docker exec -T zabbix-db pg_restore -U zabbix -d zabbix -j 4 \
  /backup/backup.dump
Tip

A file can be made available inside the container using the following command:

docker cp backup.dump zabbix-db:/backup/backup.dump

In this example, four simultaneous processes are used to perform the restoration, potentially reducing the time required for larger databases.

Note

This section was only a brief introduction to backing up a database running in a container. However, I recommend exploring the following articles and tools to deepen your knowledge:

Upgrade Procedure for New Versions
#

Keeping Zabbix up to date is important to receive security fixes, performance improvements, and new features. However, before updating the containers, some precautions are necessary to avoid data loss or compatibility issues.

The first step is to perform a full database backup. Since PostgreSQL stores the platform’s configurations, events, history, and other information, preserving this data is essential to allow recovery in case of a failure during the procedure.

Before starting, review the upgrade notes and check the requirements for the target version:

Preparation and Backup
#

First, I recommend stopping the application components to prevent new operations during the backup and upgrade process. The database container must remain running.

Considering the architecture presented in this article, we can execute:

docker compose -f zbx_srv_postgres.yml stop \
  zabbix-web zabbix-server

Next, back up PostgreSQL. In this example, we will use pg_dump:

docker exec -T zabbix-db \
  pg_dump -U zabbix -d zabbix -Fc \
  > dump_$(date +%Y-%m-%d_%H_%M_%S).dump

The -Fc parameter uses PostgreSQL’s custom format, allowing the database to be restored using pg_restore.

In addition to the database, preserve the .env and zbx_srv_postgres.yml files, as they contain the configuration required to recreate the services.

Image Updates
#

With the backup completed, we can download the updated images:

docker compose -f zbx_srv_postgres.yml pull

This command checks and downloads the images corresponding to the references defined in the Compose file.

Next, recreate the containers:

docker compose -f zbx_srv_postgres.yml up -d

Docker Compose will identify changes in the images and recreate the necessary services.

Automatic Database Upgrade
#

An important detail is that the Zabbix Server automatically upgrades the database schema during startup when it detects that the existing structure needs to be updated.

During this process, the server checks the database version and performs the necessary changes to make it compatible with the new version.

zbx auto upgrade
Automatic Zabbix database upgrade

Depending on the database size and the amount of stored information, this procedure may take a few minutes or even longer.

Do not interrupt the database upgrade process. Monitor the Zabbix Server logs to check its progress:

docker compose -f zbx_srv_postgres.yml logs -f zabbix-server

After completion, verify that the server has started successfully and that data collection has resumed.

High Availability (HA) Considerations
#

In environments using Zabbix’s native High Availability mechanism, the upgrade procedure requires additional precautions.

When upgrading between major versions, all nodes must be stopped and a full database backup must be performed. Next, one of the nodes must be selected to carry out the database upgrade, temporarily operating in standalone mode, without the ZBX_HANODENAME configuration.

After the upgrade is complete and the server has been confirmed to be working correctly, the node can be returned to its HA configuration. The remaining nodes can then be upgraded and restarted.

For minor version upgrades, the procedure is simpler: it is generally sufficient to upgrade one node first, verify that it is working correctly, and then upgrade the remaining nodes.

Final Considerations
#

Using Docker Compose allows us to organize the Zabbix deployment into independent services, simplifying the management, maintenance, and updating of the platform components.

Throughout this article, we explored how to structure an environment using PostgreSQL, Zabbix Server, Frontend, Agent, and SNMP Traps, covering the main configurations of each service and the persistence mechanisms provided by Docker.

Although containerization simplifies several deployment tasks, it is important to remember that it does not eliminate the need for proper planning. Resource sizing, data persistence, credential security, backups, and upgrade procedures remain important responsibilities when managing the environment.

The configuration presented here serves as a starting point for lab environments and can be adapted to meet the requirements of different infrastructures. For production environments, also consider availability requirements, container monitoring, storage, backup policies, and secure communication between services.