I’m currently running multiple instances of Mosquitto MQTT server
Running as docker container
Easily set up, not polluting guest’s filesystem… Inpired by https://www.laub-home.de/wiki/Eclipse_Mosquitto_Secure_MQTT_Broker_Docker_Installation
Docker file could be like
having MQTT_PORT and MQTT_TLS_PORT being defined in a .env file
In the directory where the docker-compose.yml file is located, create a subdirectory structure :
- mqtt/conf, containing mosquitto.conf
- mqtt/conf/certs, containing the SSL certificates in case you want to activate TLS. You can for example request one from let’s encrypt
the mosquitto.conf file would then look like this:
Configuring passwords for users
docker-compose exec mosquitto mosquitto_passwd -c /mosquitto/config/mosquitto.passwd <username>
docker-compose exec mosquitto mosquitto_passwd -b /mosquitto/config/mosquitto.passwd user1
Testing the setup
Install the mosquitto-clients package
mosquitto_sub -v -t '#' -u <username> -P <password> -h localhost
The -v option prints additionally the topic next to the data
mosquitto_pub -p 1883 -u <username> -P <password> -t 'my/topic/to/publish' -h localhost -m '<put message here'
Where the message can be anything from an integer, float, string, or more complex JSON structures.
Server to server bridge
It is possible to have a mosquitto server subscribe the topics published on another one. This way, all messages are collected on one single place, and they can be fetched centrally.
For this, add this snippet to
/etc/mosquitto/conf.d/remote.conf on the receiving server:
topic syntax is
topic <topic pattern> <direction> <QOS> <local prefix/remote prefix>