MQTT broker via Docker

MQTT is a great and simple way to read statusses of your IOT devices. But how do you set up a MQTT broker? Via Docker Compose it’s surprisingly easy, as you’ll find out if you read on.

If you are interested in home automation, you inevitably will come across the acronym ‘MQTT’ which stands for ‘MQ Telemetry Transport’. It allows for easy publishing messages to topics which other devices can subsribe to, so they know the status of whatever the device was publishing about. And for us, that’s basically all there is to it.

To start, create a docker-compose.yml file. It should look something like this

version: '3'

  image: toke/mosquitto:latest
  container_name: mqtt
  restart: always
    - 1883:1883/tcp
    - bridge
    - ./config:/mqtt/config:ro
    - ./data:/mqtt/data


The mapping for data is to make sure you retain your messages when the MQTT container gets recreated. As you can see there’s also a mapping to a config directory. In this directory you’ll need at least one file called mosquitto.conf. You can read more about this file over here: Mine looks partially like this, which is enough to get started:

# Place your local configuration in /mqtt/config/conf.d/

pid_file /var/run/

persistence true
persistence_location /mqtt/data/

user mosquitto

# Port to use for the default listener.
port 1883

log_dest file /mqtt/log/mosquitto.log
log_dest stdout

Now all that’s left is to start the container with docker-compose. I like to use MQTT.fx as a client for Windows to connect to the broker and subscribe to topics when I’m testing new devices or troubleshooting. It also allows for easy publishing of messages to new or existing topics.

For more information about Docker Compose, see my blogpost about it: Docker Compose

About: lolgast