Skip to content

MQTT (mosquitto) swarm template

Description

This template runs mqtt for use with home automation

State Considerations for SWARM

  • Mosquitto 2 needs a config before it will listen on anything but localhost.
  • The config is mosquitto.conf in git. It deploys as a swarm config, which is available to the whole swarm, with a version in its name, see stack conventions.

Network Considerations

none, this publishes the default port of 1883 (if you need port 9001 then you already know why and how to modify this example :-) )

Placement Considerations

  • None, this runs a single replica.
  • This is for home network so no additional scale or redundancy needed in my usecase.
compose.yml, 22 lines, 1 note

each in the code opens a note on that line. download compose.yml

version: "3.8"

services:
  mosquitto:
    image: eclipse-mosquitto:2.1.2-alpine
    environment:
      - TZ=America/Los_Angeles
    configs:
      - source: mqtt_config
        target: /mosquitto/config/mosquitto.conf

    ports:
      - 1883:1883

    deploy:
       mode: replicated
       replicas: 1

configs:
  mqtt_config:  # (1)!
    file: ./mosquitto.conf
    name: mqtt_config_v2
  1. the config is mosquitto.conf, below, deployed as a swarm config. swarm refuses a deploy that changes a config's content under the same name, so each change to the file bumps the _v2 suffix.

The config looks like this :-)

mosquitto.conf: the broker's config, 2 lines

download mosquitto.conf

listener 1883
allow_anonymous true

This page started as a gist: the original, with its comments