Enabling heterogeneous devices to communicate, especially in IoT and home automation, is an essential requirement. Mosquitto is the ideal companion for anyone who wants to integrate MQTT into their project. Let’s look in detail at what it is and how to install and use it.
MQTT is an M2M (machine-to-machine) communication protocol that is widely used to connect multiple devices through a publish/subscribe system, using extremely little bandwidth.
In a previous article I described how to install it natively on a Raspberry Pi; in this article we will install it as a container in Docker.
So, once Docker is installed, start by creating a folder on the Raspberry Pi, which will be useful both for configuring the MQTT broker and for backing up those settings, and then a script file needed to create and configure the container. Type:
mkdir docker-mosquittonano startMQTT.sh
and enter the following:
docker run --name=mosquitto --restart=always -p 1883:1883 -p 9001:9001 -v ~/docker-mosquitto/:/mosquitto/ -d eclipse-mosquitto
- the
--name=mosquittooption gives the container its name; - the
--restart=alwaysoption tells Docker to start the container automatically and restart it if there are problems; - the
-p 1883:1883and-p 9001:9001options tell Docker to map the network ports from the Raspberry Pi to the container (if you need to create two Mosquitto containers on the same machine, you will need to change the ports published outside the Raspberry Pi, for example with a configuration like this:-p 1884:1883 -p 9002:9001); - the
-v ~/docker-mosquitto/:/mosquitto/option tells Docker to map themosquittofolder inside the container to thedocker-mosquittofolder on the Raspberry Pi (this setting is handy if you ever need to reinstall the MQTT container, so that you don’t lose the log, data and config folders); - the
-doption tells Docker to run the container in the background - while the last item is simply the name of the image to download and install from Docker Hub.
With that done, press CTRL+O to save the file, press ENTER to confirm and press CTRL+X to exit the nano editor.
Now all that’s left is to run the script you just created to install the MQTT broker with the chosen settings, by typing:
bash startMQTT.sh
Once the script has finished, you can check the contents of the docker-mosquitto folder and you will see the log and data folders; however, for the MQTT broker to store logs and configuration persistently, one more small step is needed.
Create a folder called config inside docker-mosquitto with the following commands:
cd ~/docker-mosquitto
sudo mkdir config
then create a new file called mosquitto.conf in the folder you just created with the following command:
sudo nano config/mosquitto.conf
and enter the following:
persistence truepersistence_location /mosquitto/data/log_dest file /mosquitto/log/mosquitto.loglog_dest stdout
listener 1883
allow_anonymous true
Press CTRL+O to save the file, press ENTER to confirm and press CTRL+X to exit the nano editor.
Once this file has been created, you can restart the container with the command:
docker restart mosquitto
and to check that the container is running, you can run the command:
docker container ls -a
Enjoy!
Enjoy!
