Skip to content
Stand with Ukraine flag

Docker Compose

This guide covers setting up TBMQ in cluster mode using Docker Compose.

The scripts run two TBMQ nodes, two Integration Executors, PostgreSQL, Kafka, and Valkey on one host, with HAProxy in front of the TBMQ nodes. Clients connect to HAProxy, which listens on these host ports:

One host · Docker Compose

  • HAProxy
  • TBMQ × 2
  • Integration Executor × 2
  • PostgreSQL
  • Kafka
  • Valkey

HAProxy ports

  • 80 HTTP TBMQ UI and REST API
  • 443 HTTPS TBMQ UI and REST API over HTTPS; see Generate certificate for HTTPS
  • 1883 MQTT MQTT clients
  • 8084 MQTT over WebSockets MQTT clients that connect over WebSockets
  • 8883 MQTTS MQTT clients that connect over TLS, once you enable MQTTS
  • 8085 MQTT over secure WebSockets MQTT clients that connect over secure WebSockets, once you enable the TBMQ WSS listener (LISTENER_WSS_ENABLED)
  • Docker installed.
  • Git installed.

Clone TBMQ Docker Compose repository

Section titled Clone TBMQ Docker Compose repository
Terminal window
git clone -b release-2.4.2 https://github.com/thingsboard/tbmq-pe-docker-compose.git
cd tbmq-pe-docker-compose/cluster

Run every command in this guide from this tbmq-pe-docker-compose/cluster directory.

Before proceeding, ensure you have an active TBMQ license. If you don’t have one yet, visit the Pricing page, choose a pay-as-you-go subscription or a perpetual license, and use the calculator to size your deployment — session and throughput limits, production and development instances, and any add-ons — to obtain your license key.

Configure the license key

Section titled Configure the license key
  1. Open the tbmq.env file.

    Terminal window
    nano tbmq.env
  2. Set your license key.

    Find TBMQ_LICENSE_SECRET and replace YOUR_LICENSE_KEY_HERE with your actual license key:

    tbmq.env
    # License info
    TBMQ_LICENSE_SECRET=YOUR_LICENSE_KEY_HERE
  1. Create the Docker volumes.

    The script creates the volumes and copies the TBMQ, Integration Executor, and HAProxy configuration into them:

    Terminal window
    ./scripts/docker-create-volumes.sh
  2. Install the database.

    Terminal window
    ./scripts/docker-install-tbmq.sh

    The script finishes with Installation finished successfully!.

  1. Start all services.

    Terminal window
    ./scripts/docker-start-services.sh
  2. Open the UI.

    Once all services have started, open http://{your-host-ip} in your browser (e.g., http://localhost). You should see the TBMQ login page. MQTT clients connect on port 1883.

  3. Sign in as System Administrator.

    Use the default System Administrator credentials:

    Username
    sysadmin@thingsboard.org
    Password
    sysadmin
  4. Change the default password.

    On first login, you are prompted to change the default password and re-login with the new credentials.

View TBMQ node logs
docker compose logs -f tbmq1
View the state of all containers
docker compose ps
View the logs of all services
docker compose logs -f

See the docker compose logs reference for more details.

Stop the services
./scripts/docker-stop-services.sh
Stop only the TBMQ nodes and Integration Executors
./scripts/docker-stop-tbmq.sh
Start the TBMQ nodes and Integration Executors
./scripts/docker-start-tbmq.sh

These two scripts leave PostgreSQL, Kafka, Valkey, and HAProxy running. docker-start-tbmq.sh also recreates the TBMQ containers when their image or tbmq.env settings changed.

Stop and remove the containers
./scripts/docker-remove-services.sh

The data stays in the Docker volumes, so ./scripts/docker-start-services.sh brings the cluster back.

Remove the Docker volumes (deletes all data)
./scripts/docker-remove-volumes.sh

To apply changes to the configuration files (e.g., haproxy.cfg or logback.xml), copy them into the volumes:

Copy the configuration into the volumes
./scripts/docker-refresh-config.sh

TBMQ re-reads logback.xml within 10 seconds. To reload the HAProxy configuration without restarting the container, send it a HUP signal:

Reload HAProxy
docker exec -it haproxy-certbot sh -c "kill -HUP 1"
  1. Check the version-specific notes below for any preparation your target version requires.
  2. Back up your database (optional but recommended).
  3. Run the upgrade commands.

For full version history and supported upgrade paths, see the upgrade instructions page. If the documentation does not cover your specific upgrade path, contact us for guidance.

What each upgrade needs before you run the upgrade — select a version for its full notes:

Upgrade to Before running the upgrade
2.4.2 Nothing. Check SCRAM-only authentication and Kafka integrations
2.4.1 Nothing. If you set MQTT_TOPIC_MAX_SEGMENTS_COUNT, raise it by 1
2.4.0 Nothing. If you scrape metrics, update queries for the renamed Prometheus metrics
2.3.0 Nothing
TBMQ to TBMQ PE Move to the PE repository and set the license key; this path has its own commands

Backing up your PostgreSQL database before upgrading is highly recommended but optional. For guidance, follow the backup and restore instructions.

This is a standard upgrade from v2.4.1. No third-party component changes are required, and the upgrade only records the new schema version — no table is created or altered and no data is migrated.

Proceed with the upgrade.

This is a standard upgrade from v2.4.0.

No third-party component changes are required, and the upgrade only records the new schema version — no table is created or altered and no data is migrated.

Proceed with the upgrade.

This is a standard upgrade from v2.3.0.

No third-party component changes are required. The upgrade creates the dropped_msg table used by dropped message analytics; no existing table is altered and no data is migrated.

Proceed with the upgrade.

This is a standard upgrade from v2.2.0.

No third-party component changes are required — the official images are already in use since v2.2.0.

Proceed with the upgrade.

Upgrade from TBMQ to TBMQ PE

Section titled Upgrade from TBMQ to TBMQ PE

CE-to-PE migration is supported for the same version only. If you are on an earlier CE version, upgrade TBMQ CE to the latest version first. For all supported paths, see the upgrade instructions.

TBMQ PE uses its own repository, so the migration moves your cluster from the TBMQ tbmq/docker directory to the PE tbmq-pe-docker-compose/cluster directory. The Docker volumes, and the data in them, stay the same.

  1. Clone the TBMQ PE repository.

    Clone it next to your TBMQ directory, as described in Clone TBMQ Docker Compose repository.

  2. Set the license key and carry over your settings.

    In the PE tbmq.env, set TBMQ_LICENSE_SECRET to your license key from the license step, and keep TBMQ_LICENSE_INSTANCE_DATA_FILE as it is. Then copy your own changes from the TBMQ directory into the matching PE files, such as tbmq.env, .env, docker-compose.yml, and haproxy/config/haproxy.cfg.

  3. Remove the TBMQ containers.

    In the TBMQ tbmq/docker directory, run:

    Terminal window
    ./scripts/docker-remove-services.sh

    The PE services use the same container names, so the TBMQ containers must be removed first. The data stays in the Docker volumes.

  4. Migrate the database and start TBMQ PE.

    In the PE tbmq-pe-docker-compose/cluster directory, run:

    Terminal window
    ./scripts/docker-upgrade-tbmq.sh --fromVersion=ce
    ./scripts/docker-start-services.sh

    The upgrade script also creates the tbmq1-data and tbmq2-data volumes that TBMQ PE adds.

  1. Switch to the new release branch.

    Terminal window
    git fetch origin release-2.4.2
    git checkout -m release-2.4.2

    git checkout -m switches to the release branch and keeps your own edits to files such as .env, tbmq.env, and haproxy.cfg. Make sure TBMQ_VERSION in the .env file is set to the target version (e.g., 2.4.2).

  2. Run the upgrade.

    Terminal window
    ./scripts/docker-stop-tbmq.sh
    ./scripts/docker-upgrade-tbmq.sh
    ./scripts/docker-start-tbmq.sh

    The first command stops only the TBMQ nodes and Integration Executors; PostgreSQL, Kafka, Valkey, and HAProxy keep running. The upgrade script pulls the new TBMQ image and upgrades the database. The last command starts the TBMQ nodes and Integration Executors on the new version.

    If the notes for your target version change third-party services, run ./scripts/docker-stop-services.sh and ./scripts/docker-start-services.sh instead of the first and last commands, so that every service is restarted.

HAProxy handles traffic proxying and serves the web UI on ports 80 and 443 by default. To enable HTTPS with a valid certificate, run:

Terminal window
docker exec haproxy-certbot certbot-certonly --domain your_domain --email your_email
docker exec haproxy-certbot haproxy-refresh

MQTTS allows clients to connect over an encrypted TLS/SSL channel. There are two common approaches:

  • Two-way MQTTS (Mutual TLS) — TBMQ terminates TLS; clients must present valid certificates.
  • One-way MQTTS (TLS termination at load balancer) — HAProxy handles TLS termination and forwards plain MQTT to TBMQ over the internal network.

Both approaches encrypt external connections. Two-way MQTTS adds client certificate verification for higher security; one-way MQTTS simplifies broker configuration and can reuse existing HTTPS certificates.

In this configuration, TBMQ handles TLS termination and (optionally) client certificate authentication directly. This approach is suitable when you want the broker to fully control SSL/TLS and mutual authentication without relying on a load balancer for security. The default haproxy.cfg already forwards port 8883 to port 8883 of the TBMQ nodes without terminating TLS.

For supported certificate formats and options, see the MQTT over SSL guide.

  1. Provide SSL certificates.

    Obtain a valid SSL certificate and private key:

    • mqttserver.pem — public certificate (may include the full chain)
    • mqttserver_key.pem — private key
  2. Mount the certificates into both TBMQ nodes.

    In docker-compose.yml, uncomment the certificates volume mount in both the tbmq1 and tbmq2 services and replace PATH_TO_CERTS with the path to your certificate files:

    docker-compose.yml
    services:
    tbmq1:
    volumes:
    - tbmq-config:/config
    - tbmq1-logs:/var/log/thingsboard-mqtt-broker
    - tbmq1-data:/data
    - PATH_TO_CERTS:/config/certificates
    tbmq2:
    volumes:
    - tbmq-config:/config
    - tbmq2-logs:/var/log/thingsboard-mqtt-broker
    - tbmq2-data:/data
    - PATH_TO_CERTS:/config/certificates

    Keep the other volume lines of each service as they are.

  3. Let TBMQ read the certificate files.

    TBMQ runs as the thingsboard user (UID 799) inside the container and must be able to read both files. Private keys are usually readable only by their owner, so give that user ownership of the files:

    Terminal window
    sudo chown 799:799 PATH_TO_CERTS/mqttserver.pem PATH_TO_CERTS/mqttserver_key.pem

    If TBMQ cannot read a file, it fails to start and logs Permission denied for that file.

  4. Configure environment variables.

    Edit the tbmq.env file and uncomment/configure the following lines:

    tbmq.env
    LISTENER_SSL_ENABLED=true
    LISTENER_SSL_PEM_CERT=/config/certificates/mqttserver.pem
    LISTENER_SSL_PEM_KEY=/config/certificates/mqttserver_key.pem
    LISTENER_SSL_PEM_KEY_PASSWORD=server_key_password

    Leave LISTENER_SSL_PEM_KEY_PASSWORD empty if your private key has no password.

  5. Restart services.

    Terminal window
    ./scripts/docker-start-services.sh

    Once restarted, MQTT clients can connect securely on port 8883.

TLS is terminated at HAProxy. Clients connect to HAProxy on port 8883, which forwards plain MQTT to TBMQ on port 1883. You can reuse the same certificate used for HTTPS.

  1. Provide the certificate bundle.

    HAProxy needs one PEM file with the full certificate chain followed by the private key.

    • If you reuse the certificate from Generate certificate for HTTPS, haproxy-refresh already saved this bundle as /usr/local/etc/haproxy/certs.d/your_domain.pem.

    • Otherwise, copy your bundle into the HAProxy container:

      Terminal window
      docker cp fullchain-and-key.pem haproxy-certbot:/usr/local/etc/haproxy/certs.d/
  2. Point the MQTTS listener at the bundle.

    In haproxy/config/haproxy.cfg, replace the mqtts-in section with:

    haproxy.cfg
    listen mqtts-in
    bind *:${MQTTS_PORT} ssl crt /usr/local/etc/haproxy/certs.d/fullchain-and-key.pem
    mode tcp
    option clitcpka # For TCP keep-alive
    timeout client 3h
    timeout server 3h
    option tcplog
    balance leastconn
    server tbMqtt1 tbmq1:1883 check inter 5s resolvers docker_resolver resolve-prefer ipv4
    server tbMqtt2 tbmq2:1883 check inter 5s resolvers docker_resolver resolve-prefer ipv4

    Replace fullchain-and-key.pem with the actual filename of your certificate bundle.

  3. Apply the configuration.

    Terminal window
    ./scripts/docker-refresh-config.sh
    docker exec -it haproxy-certbot sh -c "kill -HUP 1"

    MQTT clients can now connect securely on port 8883.

Connect your first client

Set up client credentials, then publish and subscribe with an MQTT client. See Getting started.

Secure the broker

Choose the authentication provider that fits your clients. See Security overview.

Watch the broker

Track sessions, throughput, and resource usage as traffic grows. See Monitoring.