Docker / Home Automation · May 3, 2021

Containerized MQTT Broker

Here we’re going over the deploy and config of a containerized MQTT Broker, all the basics of pub/sub, topics, retain flags, and I’ll even show you how to dig in under the hood of a Docker volume. Check out the video below, and code samples discussed in the video are here below as well. Enjoy!

YouTube player

Here’s your sample docker-compose.yml for Eclipse Mosquitto MQTT broker:

version: '3.7'

services:
  mosquitto:
    image: eclipse-mosquitto:latest
    volumes:
      - mosquitto-data:/mosquitto/data
      - mosquitto-log:/mosquitto/log
      - mosquitto-config:/mosquitto/config

    ports:
      - "1883:1883"

    deploy:
      mode: replicated
      replicas: 1
      placement:
        constraints:
          - node.role != manager
      restart_policy:
        condition: on-failure
        delay: 5s

volumes:
  mosquitto-data:
  mosquitto-log:
  mosquitto-config:

Sample config for your mosquitto.conf file

listener 1883
protocol mqtt

persistence false

log_dest file /mosquitto/log/mosquitto.log
log_timestamp true
allow_anonymous true