dankhostself-host anything

Mosquitto with Docker Compose

Eclipse Mosquitto is an open source (EPL/EDL licensed) message broker that implements the MQTT protocol versions 5.0, 3.1.1 and 3.1. In the project's words: mosquitto.org front page checked 2026-09-29

Runs on: amd64, arm64, arm/v7, 386, arm/v6, ppc64le, s390x from each image's registry manifest, checked 2026-09-29; a CPU counts only if every image has a build for it. Image by image

Add Mosquitto to the builder to use your own domain and combine it with other apps in one file.

What Mosquitto needs

Only figures Mosquitto's own documentation publishes, each linked to the page it is on. Where the project gives no number, this page does not invent one.

ResourceMinimumRecommended
RAMThe project publishes no figure
CPUThe project publishes no figure
DiskThe project publishes no figure

On top of this, Caddy (the reverse proxy in the file) is a single small container.

Which hardware Mosquitto has images for

Read from each image's manifest list in its registry (anonymous, read-only) when this page was built. An app counts for a CPU only if every one of its images lists it.

  • x86-64 PC or NAS (linux/amd64): every image has a build.
  • Raspberry Pi 4/5, 64-bit OS (linux/arm64): every image has a build.
  • Raspberry Pi 3 or Zero 2 W, 32-bit OS (linux/arm/v7): every image has a build; mosquitto only as linux/arm/v6, which Docker pulls on a arm/v7 CPU when there is no arm/v7 build.
ServiceImagePlatforms in the manifestDigestRegistryChecked
mosquittoeclipse-mosquitto:2.1-alpineamd64, arm/v6, arm64, 386, ppc64le, s390xsha256:38c0da4f2ef84284d47b3b3eeea1cb3bdeabe81ee10caf0cd5c5ff61ee3ea408registry-1.docker.io2026-09-29

A manifest says an image exists for a CPU, not how well Mosquitto runs on it: a build for arm64 or arm/v7 can still be too slow or need more memory than the board has. The RAM and disk Mosquitto publishes are in the table above and, for a whole set of apps against a Raspberry Pi, on Will it run on my machine?. A tag like latest can move to a new digest after the date checked.

docker-compose.yml

This is exactly what the builder writes for Mosquitto alone on example.org: the official definition with its published ports removed except 1883:1883, which cannot go through a web proxy (see below), and Caddy added in front.

# docker-compose.yml built at dankhost.com for: Mosquitto
# Each app block is its official compose definition. Only the names were prefixed with
# the app slug (services, containers, volumes, bind folders, .env variables) so any set of
# apps can share one file; other differences are listed under "Changed:" in each block.
# Caddy publishes 80 and 443. The only other published ports, each labelled where
# it is written: 1883/tcp (Mosquitto: MQTT).
# Secrets live in .env next to this file.

services:
  # ---- Caddy: the one reverse proxy, HTTPS certificates from Let's Encrypt ----
  # Source: https://caddyserver.com/docs/running (checked 2026-09-23)
  # Source: https://github.com/dani-garcia/vaultwarden/wiki/Using-Docker-Compose (checked 2026-09-23)
  # Caddyfile goes in ./caddy/Caddyfile. Fixed address 172.30.0.2 so apps can trust it exactly.
  caddy:
    image: "caddy:2"
    container_name: "caddy"
    restart: "unless-stopped"
    ports:
      - "80:80"
      - "443:443"
      - "443:443/udp"
    volumes:
      - "./caddy:/etc/caddy"
      - "caddy-data:/data"
      - "caddy-config:/config"
    networks:
      proxy:
        ipv4_address: "172.30.0.2"

  # ---- Mosquitto (mqtt.example.org) ----
  # Source: https://hub.docker.com/_/eclipse-mosquitto (checked 2026-09-29)
  # Source: https://mosquitto.org/man/mosquitto-conf-5.html (checked 2026-09-29)
  # Source: https://mosquitto.org/documentation/authentication-methods/ (checked 2026-09-29)
  # Source: https://github.com/eclipse-mosquitto/mosquitto/blob/master/docker/2.1-alpine/Dockerfile (checked 2026-09-29)
  # Changed: Written from the image page's docker run with the three mirrored directories: ./config, ./data and ./log. Only '1883:1883' is published; Caddy reaches mosquitto:9001, a WebSockets listener added in mosquitto.conf, on the proxy network.
  #          The image page lists no compose file. The tag is 2.1-alpine, one of the two it recommends ("prefer latest or a pinned 2.1*-alpine tag").
  #          Added mosquitto.conf (the image page says to mount your own in /mosquitto/config): the listeners, the persistence and logging lines the page gives, and a password file.
  # PORT OUTSIDE THE PROXY 1883/tcp: plain MQTT for the devices on your network, the '-p 1883:1883' of the documented docker run. MQTT is not HTTP, so most devices cannot reach it through the proxy.
  mosquitto:
    image: "eclipse-mosquitto:2.1-alpine"
    container_name: "mosquitto"
    volumes:
      - "./mosquitto/config:/mosquitto/config"
      - "./mosquitto/data:/mosquitto/data"
      - "./mosquitto/log:/mosquitto/log"
    ports:
      - "1883:1883"  # MQTT
    networks:
      proxy: {}

networks:
  # Pinned so the trusted-proxy address given to each app (172.30.0.2) is exact.
  # If "docker compose up" reports "Pool overlaps", change 172.30.0 here, in Caddy's
  # ipv4_address and in every trusted-proxy value to a free 172.x.0 range.
  proxy:
    ipam:
      config:
        - subnet: "172.30.0.0/24"
          ip_range: "172.30.0.128/25"
          gateway: "172.30.0.1"

volumes:
  caddy-data:
  caddy-config:

Caddyfile save as caddy/Caddyfile

Caddy gets and renews the HTTPS certificate for mqtt.example.org by itself once the DNS record points at your server and ports 80 and 443 reach it.

# Caddyfile built at dankhost.com. Save it as ./caddy/Caddyfile next to docker-compose.yml.
# Caddy gets and renews a Let's Encrypt certificate for each name below by itself,
# once the DNS records point at this server and ports 80 and 443 reach it.

# Mosquitto
mqtt.example.org {
	reverse_proxy mosquitto:9001
}

Already running nginx, Nginx Proxy Manager, Caddy or Traefik? The reverse-proxy settings Mosquitto needs, for all four, each cited to Mosquitto's own docs.

.env

Mosquitto needs no secret here; the builder adds nothing you have to fill in by hand.

# .env for Mosquitto, from dankhost.com/app/mosquitto/. Nothing here is secret.
# Keep this file private (chmod 600).
# None of the chosen apps needs a variable here.

The file Mosquitto needs next to the compose file

The builder writes it with your domain and fills every secret with random letters and digits made in your browser (crypto.getRandomValues); nothing is sent anywhere. Below, each secret is a FILL-IN-… placeholder instead, because a secret printed on a public page is the same for everyone who copies it.

./mosquitto/config/mosquitto.conf

# mosquitto.conf for Mosquitto, built at dankhost.com. Save it as ./mosquitto/config/mosquitto.conf
# Every line is an option from the mosquitto.conf man page.
# Plain MQTT for devices on your network (published on the host as 1883).
listener 1883
# MQTT over WebSockets, which the proxy serves as wss://mqtt.example.org.
listener 9001
protocol websockets
# The Docker image page: persist data to /mosquitto/data and log to /mosquitto/log.
persistence true
persistence_location /mosquitto/data/
log_dest file /mosquitto/log/mosquitto.log
# allow_anonymous defaults to false: only users in this file can connect.
# Create it before the first start (see the notes).
password_file /mosquitto/config/passwd

What Mosquitto needs beyond the proxy

Every other app in the builder is reached only through the proxy on ports 80 and 443. Mosquitto also needs this port on the server itself, because it carries traffic that is not HTTP. The compose file labels it with a # PORT OUTSIDE THE PROXY comment.

  • 1883:1883 (MQTT): plain MQTT for the devices on your network, the '-p 1883:1883' of the documented docker run. MQTT is not HTTP, so most devices cannot reach it through the proxy.
  • Port 1883 (plain MQTT, not encrypted) is published on every interface of this server for the devices on your network. Do not forward it from your router; clients outside your home use MQTT over WebSockets through the proxy on port 443 instead.

Changes from the official file

Besides prefixing every service, container, volume, folder and variable with mosquitto (so any set of apps can share one file), these are all the differences from what the project documents:

  • Written from the image page's docker run with the three mirrored directories: ./config, ./data and ./log. Only '1883:1883' is published; Caddy reaches mosquitto:9001, a WebSockets listener added in mosquitto.conf, on the proxy network.
  • The image page lists no compose file. The tag is 2.1-alpine, one of the two it recommends ("prefer latest or a pinned 2.1*-alpine tag").
  • Added mosquitto.conf (the image page says to mount your own in /mosquitto/config): the listeners, the persistence and logging lines the page gives, and a password file.

Notes for Mosquitto behind the proxy

  • Before the first start, create a user: docker compose run --rm mosquitto mosquitto_passwd -c /mosquitto/config/passwd <user> (it asks for the password). The authentication page: the password file must be readable by the user Mosquitto runs as, which in this image is uid 1883, so then run: sudo chown 1883:1883 ./mosquitto/config/passwd
  • Without that file Mosquitto does not start: the configuration names it, and since allow_anonymous defaults to false no client could connect anyway. Add more users with the same command without -c.
  • Devices on your network connect to the server's LAN address on port 1883 with that user. Clients that speak MQTT over WebSockets connect to wss://mqtt.example.org (port 443, through the proxy).
  • Other apps in the same compose file reach the broker as mosquitto:1883 on the proxy network, for example Zigbee2MQTT with mqtt://mosquitto:1883. Home Assistant (host network) reaches it at this server's LAN address, port 1883.
  • The documented docker run sets no restart policy, so Mosquitto does not come back by itself after a reboot. Run "docker compose up -d" again, or add restart: unless-stopped.

How to run it

  1. At your DNS provider, add an A record named mqtt on your domain pointing at the server's public IP address, and forward TCP 80, TCP 443 and UDP 443 to it. For Mosquitto, port 1883 (plain MQTT, not encrypted) is published on every interface of this server for the devices on your network. Do not forward it from your router; clients outside your home use MQTT over WebSockets through the proxy on port 443 instead.
  2. Save ./mosquitto/config/mosquitto.conf from the files above (or from the builder, with the secrets filled in).
  3. Save the three files in one folder: docker-compose.yml and .env side by side, the Caddyfile at ./caddy/Caddyfile. Replace example.org with your domain in the Caddyfile, the compose file and the .env, mosquitto.conf, or let the builder do it.
  4. Nothing in .env needs filling in; chmod 600 .env all the same.
  5. Start it:
    docker compose up -d
    docker compose logs -f caddy   # watch the certificate arrive
  6. Open the new address (mqtt. plus your domain) in a browser and follow the notes above.

With Home Assistant, Mosquitto and Zigbee2MQTT in the same file

Tick all three in the builder and they land in one docker-compose.yml, but they do not reach the broker at the same address. Home Assistant keeps network_mode: "host" as its install page documents, so it is on none of the file's networks; Mosquitto and Zigbee2MQTT are both on the proxy network. Every step below is quoted from the project's own docs, with the date the page was fetched.

Where each one finds the broker in this file
AppIn the fileBroker address
Home Assistantnetwork_mode: "host"<server LAN IP>, port 1883
Zigbee2MQTTthe proxy networkmqtt://mosquitto:1883
Mosquittothe proxy network, publishes 1883:1883listens on 1883 inside its container
  • Zigbee2MQTT uses the service name. It shares the proxy network with Mosquitto, and Compose gives every service on a network a name the others can look up, so mosquitto resolves to the broker's container from inside Zigbee2MQTT's: “Each service registers its name with an internal DNS server, so containers can reach each other using the service name directly.” “The proxy service is isolated from the db service, because they do not share a network in common.” Docker docs: Networking in Compose (service discovery by name, custom networks) checked 2026-10-09
  • Home Assistant uses the server's own address. With host networking it is not on the proxy network at all, so the name mosquitto means nothing to it. It reaches Mosquitto the way the devices on your network do, through the port the file publishes on the server (1883:1883): “If you use the host network mode for a container, that container's network stack isn't isolated from the Docker host (the container shares the host's networking namespace), and the container doesn't get its own IP-address allocated.” Docker docs: Host network driver checked 2026-10-09 “-p 8080:80 Map port 8080 on the Docker host to TCP port 80 in the container.” Docker docs: Port publishing and mapping checked 2026-10-09

1. Create the broker user first

Mosquitto in this file lets no client in without a user from its password file. Create the first one with the command in the notes above before you start it. To give Home Assistant and Zigbee2MQTT a user each, add the second the way Mosquitto's docs say, without the create flag: “To add more users to an existing password file, or to change the password for an existing user, leave out the -c argument” Mosquitto docs: Authentication methods (mosquitto_passwd, password_file) checked 2026-10-09

A user added while Mosquitto runs is not read until the broker reloads the file, so restart it afterwards: “If you make changes to the password file you must trigger the broker to reload the file by sending a SIGHUP message” Mosquitto docs: Authentication methods (mosquitto_passwd, password_file) checked 2026-10-09 “Restarts all stopped and running services, or the specified services only.” Docker docs: docker compose restart checked 2026-10-09

docker compose restart mosquitto

2. Home Assistant: add the MQTT integration

  1. “Go to Settings > Devices & services.”
  2. “In the bottom right corner, select the Add Integration button.”
  3. “From the list, select MQTT.” Home Assistant docs: MQTT integration (adding it, broker settings, MQTT protocol version 5) checked 2026-10-09
  4. As broker give <server LAN IP> (the address you open Home Assistant on, not mosquitto), as port 1883, and the user and password from the first step. “Add the MQTT integration, then provide your broker’s hostname (or IP address) and port and (if required) the username and password that Home Assistant should use.” Home Assistant docs: MQTT integration (adding it, broker settings, MQTT protocol version 5) checked 2026-10-09

Mosquitto meets Home Assistant's one requirement on the broker: “Home Assistant requires that your broker supports MQTT protocol version 5.” Home Assistant docs: MQTT integration (adding it, broker settings, MQTT protocol version 5) checked 2026-10-09 “implements the MQTT protocol versions 5.0, 3.1.1 and 3.1” mosquitto.org front page checked 2026-10-09

3. Zigbee2MQTT: the broker address

Zigbee2MQTT keeps its settings in configuration.yaml in its data directory, which this file mounts from ./zigbee2mqtt/data. The file is written by the onboarding page on the first start: “The file have to be located in the data directory within your installation.” Zigbee2MQTT docs: Configuration (the data directory, ZIGBEE2MQTT_CONFIG_ environment variables) checked 2026-10-09 “Zigbee2MQTT offers an onboarding process on first run (when no configuration.yaml exists).” Zigbee2MQTT docs: Getting started (onboarding on first run) checked 2026-10-09

Its mqtt section, with the docs' example address changed to the broker's service name and port, and the user you created for Zigbee2MQTT: “Zigbee2MQTT requires a MQTT-Server connection to operate. # Required: MQTT settings mqtt:” “server: 'mqtt://localhost:1883'” “user: my_user” “password: my_password” Zigbee2MQTT docs: Configuration, MQTT (server, user, password) checked 2026-10-09

mqtt:
  server: 'mqtt://mosquitto:1883'
  user: <user>
  password: <password>

4. Zigbee2MQTT: make its devices appear in Home Assistant

Zigbee2MQTT announces its devices to Home Assistant over MQTT, but only when told to: “The easiest way to integrate Zigbee2MQTT with Home Assistant is by using MQTT discovery. This allows Zigbee2MQTT to automatically add devices to Home Assistant.” Zigbee2MQTT docs: Home Assistant integration (MQTT discovery) checked 2026-10-09 “# Enable the integration (default: false)” Zigbee2MQTT docs: Configuration, Home Assistant integration (homeassistant.enabled) checked 2026-10-09

Add these two lines to the same configuration.yaml: “In your Zigbee2MQTT configuration.yaml set: homeassistant: enabled: true” Zigbee2MQTT docs: Home Assistant integration (MQTT discovery) checked 2026-10-09

homeassistant:
  enabled: true

Then restart Zigbee2MQTT: “Some options will require a restart before being effective.” Zigbee2MQTT docs: Configuration (the data directory, ZIGBEE2MQTT_CONFIG_ environment variables) checked 2026-10-09

docker compose restart zigbee2mqtt

Its devices then appear in Home Assistant as devices of the MQTT integration from step 2: “When using Home Assistant MQTT discovery, Zigbee2MQTT integrates with the Home Assistant device registry. This allows you to change the Home Assistant entity_id and friendly_name from the Home Assistant web interface without having to restart Home Assistant.” Zigbee2MQTT docs: Home Assistant integration (MQTT discovery) checked 2026-10-09

Back it up

Mosquitto publishes no backup procedure. With persistence on, its data file (retained messages, subscriptions, queued messages for persistent clients) is in /mosquitto/data; the configuration and password file are in /mosquitto/config.

Below is the BACKUP.md the builder writes next to the compose file above: which of its folders and volumes hold data, what to copy with which container stopped, and a restore check. Pick more apps in the builder and it covers them all in one file.

# BACKUP.md built at dankhost.com for: Mosquitto

What to copy from the docker-compose.yml built with this file, the database dump each project documents (rewritten for the service names in that file), what to copy with which container stopped, and how to check the result. Run every command in the folder that holds docker-compose.yml.

The folders are owned by the containers' users: put sudo in front of tar if it says "Permission denied". Named volumes live under /var/lib/docker/volumes, not next to the compose file, so they are copied through a throwaway ubuntu container (Docker's documented --volumes-from method). tar makes a full copy each run; for large folders an incremental tool (restic, borg, rsync) saves time and space.

Never run "docker compose down -v" on this file: it deletes the named volumes of every app in it.

## Before you start

    BACKUP=/mnt/backup/selfhost   # change this: a disk or machine other than this server's system disk
    mkdir -p "$BACKUP"

## Every time: the three stack files

    tar czf "$BACKUP/stack-files.tar.gz" docker-compose.yml .env caddy

.env holds the database passwords and secret keys the apps were set up with; keep this archive as private as the data.

## Caddy (the reverse proxy)

Caddy's docs: 'The data directory must not be treated as a cache.' It holds the TLS certificates and their private keys; without it Caddy has to ask Let's Encrypt for every certificate again.

- caddy-data (named volume, /data in caddy)
  BACK UP: TLS certificates, private keys, OCSP staples.
- caddy-config (named volume, /config in caddy)
  SKIP: the last active configuration, kept for 'caddy run --resume', which this setup does not use: it starts from the Caddyfile.
- ./caddy (/etc/caddy in caddy)
  IN stack-files.tar.gz: the Caddyfile, copied with the stack files above.

Copy (Caddy keeps serving while you copy):

    docker run --rm --volumes-from caddy -v "$BACKUP":/backup ubuntu tar cvf /backup/caddy-data.tar /data

Restore check:

    tar tf "$BACKUP/caddy-data.tar" > /dev/null && echo OK
    docker run --rm --volumes-from caddy -v "$BACKUP":/backup ubuntu bash -c "cd /data && tar xvf /backup/caddy-data.tar --strip 1"

Sources:
- Caddy docs: Conventions, Data directory: https://caddyserver.com/docs/conventions#data-directory (checked 2026-09-24)
- Docker docs: Volumes, Back up, restore, or migrate data volumes: https://docs.docker.com/engine/storage/volumes/#back-up-restore-or-migrate-data-volumes (checked 2026-09-24)

## Mosquitto

Mosquitto publishes no backup procedure. With persistence on, its data file (retained messages, subscriptions, queued messages for persistent clients) is in /mosquitto/data; the configuration and password file are in /mosquitto/config.

What this compose mounts:
- ./mosquitto/config (/mosquitto/config in mosquitto)
  BACK UP: mosquitto.conf and the password file.
- ./mosquitto/data (/mosquitto/data in mosquitto)
  BACK UP: mosquitto.db: retained messages and persistent sessions.
- ./mosquitto/log (/mosquitto/log in mosquitto)
  SKIP: the log file.

1. Stop mosquitto. Mosquitto writes its persistence file while it runs; stopping it first writes out a complete copy.

    docker compose stop mosquitto

2. Copy while mosquitto is stopped:

    tar czf "$BACKUP/mosquitto-config.tar.gz" ./mosquitto/config
    tar czf "$BACKUP/mosquitto-data.tar.gz" ./mosquitto/data

3. Start it again:

    docker compose start mosquitto

Restore check:

    tar tzf "$BACKUP/mosquitto-config.tar.gz" > /dev/null && echo OK
    tar tzf "$BACKUP/mosquitto-data.tar.gz" > /dev/null && echo OK

Put a copy back (with mosquitto stopped):

    tar xzf "$BACKUP/mosquitto-config.tar.gz"   # recreates ./mosquitto/config; move the old folder aside first
    tar xzf "$BACKUP/mosquitto-data.tar.gz"   # recreates ./mosquitto/data; move the old folder aside first

Stop it, put ./mosquitto/config and ./mosquitto/data back, start it.

Sources:
- Mosquitto docs: mosquitto.conf man page (listener, protocol websockets, allow_anonymous, password_file, persistence): https://mosquitto.org/man/mosquitto-conf-5.html (checked 2026-09-29)
- Docker docs: Volumes, Back up, restore, or migrate data volumes: https://docs.docker.com/engine/storage/volumes/#back-up-restore-or-migrate-data-volumes (checked 2026-09-29)

Update it

What docker compose pull does depends only on how each image line in the compose file above is tagged. For Mosquitto as this page writes it:

ServiceImageHow it is pinnedWhat docker compose pull does
caddycaddy:2Major version only (tag 2)Takes the newest image tagged 2: minor and patch releases of 2, never 3. A new major version needs a tag edit.
mosquittoeclipse-mosquitto:2.1-alpineMajor and minor version (tag 2.1-alpine)Takes the newest image tagged 2.1-alpine: patch releases and rebuilds of 2.1 only. A newer minor or major version needs a tag edit.

What Mosquitto's docs say about updating

Mosquitto publishes no update procedure for this setup; the steps below are Docker's generic method.

The steps

  1. Back up first, as in Back it up above. An update can migrate Mosquitto's data on its first start; the copy is the way back.
  2. Download the new images. Running containers are not touched yet:
    docker compose pull
  3. Recreate the containers whose image changed. Volumes and bind-mounted folders stay as they are:
    docker compose up -d
  4. Watch Mosquitto start (Ctrl+C stops following the log, not the app):
    docker compose logs -f mosquitto
  5. Optional: remove the old images the pull left without a tag. This covers every stack on the machine, not only this one:
    docker image prune

Docker's reference for these commands: Docker docs: docker compose pull checked 2026-09-30; Docker docs: docker compose up (recreates changed containers, keeps volumes) checked 2026-09-30; Docker docs: docker image prune checked 2026-09-30

Locked out? Reset the admin password

Mosquitto has no web login; its passwords are the MQTT users in the password file /mosquitto/config/passwd. mosquitto_passwd updates one user's password there.

  1. Set the new password (it asks for it): Source: Mosquitto man page: mosquitto_passwd (update the password for a user) read 2026-10-03
    docker compose exec mosquitto mosquitto_passwd /mosquitto/config/passwd <user>
  2. The broker must reload the file to use it; restarting it does that. The authentication page also says the file must be readable by the user Mosquitto runs as (uid 1883 in this image), so if it fails to start, check the owner of ./mosquitto/config/passwd. Source: Mosquitto docs: Authentication methods (password file, reload) read 2026-10-03; Docker docs: docker compose restart read 2026-10-03
    docker compose restart mosquitto

Run the commands in the folder that holds docker-compose.yml. Words in angle brackets are placeholders: replace them, brackets included. docker compose exec runs the command in the container that is already running: Docker docs: docker compose exec read 2026-10-03

Sources

Every page below was fetched on the date shown.

Add Mosquitto to the builder All one hundred apps

Made with AI tools. · Privacy