I’ve previously written about being stymied by docker so after getting several containers running properly, I decided to try my hand at creating a mosquitto broker. As with everything else you find on the internet, the veracity and integrity of installed binaries is incumbent you, the user. This leads me to believe that in simple cases, it is probably better to create your own microservices.
There are plenty of brokers out there built on mosquitto. I started with the very complete:
pascaldevink/rpi-mosquitto:latest 206.8 MB
Which is a great container EXCEPT, it does not have the mosquitto-clients which are necessary for mosquitto troubleshooting. It does however, have websocket full implementation which is not exactly trivial.
I wanted small, so I started with Alpine Linux. This ends up being around 5MB, which is their reported size. Size reported by docker is actually smaller:
arm32v6/alpine:latest 4 MB
With mosquitto installed via the following Dockerfile, I ended up with a tiny image, comparable to the trafex image that is probably the most popular Pi MQTT broker.
jumblies/mini-mosquitto:latest 4.6 MB
RUN apk add --no-cache mosquitto-clients mosquitto
# unnecessary exposure to since no websocket 9001
ENTRYPOINT ["/bin/sh" "-c", "/usr/sbin/mosquitto -d"]
COPY config /mqtt/config
I left out the maintainer info, since I doubt it will be distributed and it will probably only be built once. This does run as root.
To run it:
docker run -it -d -p 1883:1883 --name mini-mosquitto jumblies/mini-mosquitto:latest
To use clients:
watch all messages:
docker exec -ti mini-mosquitto mosquitto_sub -h 127.0.0.1 -v -t '#'
publish a message:
docker exec -ti mini-mosquitto mosquittpub -h 127.0.0.1 -t "caviar" -m "tangerine speedo" -r