based on http://www.steves-internet-guide.com/mosquitto-bridge-configuration/
An MQTT broker can be executed as a Docker container. This fact provides flexibility when deploying these services, especially in edge devices, like Raspberry Pi or similar. Searching for MQTT, you’ll get:
$ docker search mqtt
NAME DESCRIPTION STARS OFFICIAL
eclipse-mosquitto Eclipse Mosquitto is an open source message … 1253 [OK]
emqx The most scalable open-source MQTT broker fo… 83 [OK]
greenbone/mqtt-broker A mosquitto.org MQTT broker for the Greenbon… 2
airbyte/destination-mqtt 0
sensebox/mqtt-osem-integration MQTT Integration for openSenseMap API 0
fraunhoferiosb/frost-server-mqtt The MQTT-Only package of the FROST-Server 0
...
The first one, eclipse-mosquitto, is the official image for the Mosquitto MQTT broker, probably the most widely used MQTT broker.
The third one, efrecon/mqtt-client is also a helpful image that allows having a handy client to test deployments.
The docker-compose.yml
file below indicates how to start up an MQTT broker locally:
version: '3'
networks:
pubsub-net:
driver: bridge
services:
mosquitto1:
image: eclipse-mosquitto
container_name: broker1
ports:
- 1883:1883
volumes:
- ./mosquitto:/mosquitto/
networks:
- pubsub-net
in this case we need a ./mosquitto/config/mosquitto.conf
file as simple as this:
allow_anonymous true
listener 1883
Executing docker compose up
we will get something like:
[+] Running 4/4
⠿ mosquitto1 Pulled 3.7s
⠿ ef5531b6e74e Pull complete 1.2s
⠿ a7ca8a06e1f9 Pull complete 1.3s
⠿ b02910b6532b Pull complete 1.4s
[+] Running 2/2
⠿ Network day2_b_bridgingmqttbrokers_pubsub-net Created 0.0s
⠿ Container broker1 Cr... 0.1s
Attaching to broker1
broker1 | 1676744530: mosquitto version 2.0.15 starting
broker1 | 1676744530: Config loaded from /mosquitto/config/mosquitto.conf.
broker1 | 1676744530: Opening ipv4 listen socket on port 1883.
broker1 | 1676744530: Opening ipv6 listen socket on port 1883.
broker1 | 1676744530: mosquitto version 2.0.15 running
check with docker ps -a
:
We can do a quick test by opening two terminals and executing one of them:
docker run -it --rm **--network=host** efrecon/mqtt-client sub -t 'test/#'
or
docker run -it --rm **--net=container:broker1** efrecon/mqtt-client sub -t 'test/#'
<aside>
🗣 Compare the use of --network=...
</aside>
and, in the other terminal: