Subsequently, MQTT has grown to be one of the most widely used IoT connectivity protocols with direct support from service such as AWS. Back in 2010, the first open-source MQTT Broker was Mosquitto. Mosquitto is now part of the Eclipse Foundation, and an iot.eclipse.org project, sponsored by cedalo.com.
Another area that has grown during the interim period is the use of container technology, such as Docker, for both testing and deployment. We have, also, extensively covered Docker in previous blog posts.
For another internal dogfood project, I wanted to run a local MQTT Broker rather than a web-based broker, such as http://mqtt.eclipse.org/. Mosquitto can be installed natively on Windows, Mac and Linux. Still, one of the significant benefits of Docker is not polluting your working machine with lots of different tools.
Running Mosquitto in a Docker container is, therefore, a perfect test environment. Rather than, as in the previous Docker blog articles, build our own Docker image containing Mosquitto, we can use the official Dockerhub image.
eclipse-mosquitto Docker image
Pull the latest image
I’m assuming you have Docker installed and configured for your local working environment.
First, pull the latest image from Dockerhub:
% docker pull eclipse-mosquitto
Note of caution: the instructions on the Dockerhub site are incorrect!
Run the docker image
Run the basic Docker image with default settings:
% docker run -it --name mosquitto -p 1883:1883 eclipse-mosquitto 1582194844: mosquitto version 1.6.8 starting 1582194844: Config loaded from /mosquitto/config/mosquitto.conf. 1582194844: Opening ipv4 listen socket on port 1883. 1582194844: Opening ipv6 listen socket on port 1883.
-p 1883:1883 argument maps the docker container’s default MQTT socket
1883 the localhost (
1883. Alternatively, we could map that onto another localhost port if it clashed with a locally running MQTT broker, e.g.
--name directive also allows the container to be stopped and restarted, using:
% docker stop mosquitto
% docker start mosquitto
Testing the eclipse-mosquitto Docker container
To test the setup of the running Mosquitto container, I used my original software, still available on github. To build this, you’ll need a C compiler (ideally gcc or clang) and CMake.
Alternatively, any MQTT client should work for test purposes.
Next, we must subscribe to a topic. In a command window invoke the subscribe client to a topic, the default for our project being
hello\world on port
% ./mqttsub MQTT SUB Test Code port:1883 Connected to MQTT Server at 127.0.0.1:1883 Subscribed to MQTT Service hello/world with QoS 0
To test publishing, open another command window and invoke the publisher-client. The publisher-client, by default, publishes 10 messages to the topic
hello\world and then closes the connection, e.g.
% ./mqttpub MQTT PUB Test Code port:1883 Connected to MQTT Server at 127.0.0.1:1883 Published to MQTT Service hello/world with QoS0 Sent 1 messages Published to MQTT Service hello/world with QoS0 Sent 2 messages Published to MQTT Service hello/world with QoS0 Sent 3 messages Published to MQTT Service hello/world with QoS0 Sent 4 messages Published to MQTT Service hello/world with QoS0 Sent 5 messages Published to MQTT Service hello/world with QoS0 Sent 6 messages Published to MQTT Service hello/world with QoS0 Sent 7 messages Published to MQTT Service hello/world with QoS0 Sent 8 messages Published to MQTT Service hello/world with QoS0 Sent 9 messages Published to MQTT Service hello/world with QoS0 Sent 10 messages
On returning to the subscriber window, we will see the received message displayed.
Message number 1 Message number 2 Message number 3 Message number 4 Message number 5 Message number 6 Message number 7 Message number 8 Message number 9 Message number 10
Mosquitto window output
Returning the window where the docker image was invoked, various log messages are shown:
1582194844: mosquitto version 1.6.8 starting 1582194844: Config loaded from /mosquitto/config/mosquitto.conf. 1582194844: Opening ipv4 listen socket on port 1883. 1582194844: Opening ipv6 listen socket on port 1883. 1582205221: New connection from 172.17.0.1 on port 1883. 1582205221: New client connected from 172.17.0.1 as default_sub (p1, c1, k30). 1582205225: New connection from 172.17.0.1 on port 1883. 1582205225: New client connected from 172.17.0.1 as default_pub (p1, c1, k30). 1582205235: Client default_pub disconnected.
Setting up persistent files
Mosquitto can be configured, for example, to change logging, password, listener-ports, etc. This is achieved using
To set up
mosquitto.conf, first create a local working directory with a three sub-directories of config, data and log, e.g.
% cd % mkdir docker-mosquitto % cd docker-mosquitto % mkdir mosquitto % mkdir mosquitto/config/ % mkdir mosquitto/data/ % mkdir mosquitto/log/
Create a config file
Next, create a test file called
mosquitto.conf in the newly created subdirectory
% touch mosquitto/config/mosquitto.conf
Edit the config file
Using your favourite editor (okay
vi isn’t my favourite, but it’s convenient):
% vi mosquitto/config/mosquitto.conf
And add the as a minimum set of conf directives.
# following two lines required for > v2.0 allow_anonymous true listener 1883 persistence true persistence_location /mosquitto/data/ log_dest file /mosquitto/log/mosquitto.log
The full list of configuration items can be found [here](https://mosquitto.org/man/mosquitto-conf-5.html].
Run the docker image with a mounted volume
Now, when invoking the docker image we use the
-v flag mapping the local filesystem into the docker container. The running container will now pick up the locally defined
mosquitto.conf. Invoke e.g:
% docker run -it --name mosquitto -p 1883:1883 -v $(pwd)/mosquitto:/mosquitto/ eclipse-mosquitto
I hope this post gave you a useful overview of getting an MQTT Mosquitto Broker up and running using Docker.
Hopefully, in future posts, I will be able to share further details of the dogfood project.
- Disassembling a Cortex-M raw binary file with Ghidra - December 20, 2022
- Using final in C++ to improve performance - November 14, 2022
- Understanding Arm Cortex-M Intel-Hex (ihex) files - October 12, 2022
Co-Founder and Director of Feabhas since 1995.
Niall has been designing and programming embedded systems for over 30 years. He has worked in different sectors, including aerospace, telecomms, government and banking.
His current interest lie in IoT Security and Agile for Embedded Systems.