Mosquitto is an MQTT message broker which has an official docker image. You probably have devices which can publish messages using MQTT (like your electricity smart meter), and want to subscribe to those messages for graphing it using something like telegraf and grafana. The mosquitto_passwd tool is included in the docker image, but you will need to set the location of the password_file in the mosquitto.conf file. To do that, first create a directory on your local file system to hold the mosquitto files so that you don't lose the settings when re-creating the container. Then copy the example mosquitto.conf into the config directory and edit it to specify the password_file.

$ mkdir -p mosquitto-data/config mosquitto-data/data mosquitto-data/log    # we only really care about config
$ curl -o mosquitto-data/config/mosquitto.conf https://raw.githubusercontent.com/eclipse/mosquitto/master/mosquitto.conf
$ nano mosquitto-data/config/mosquitto.conf

Look for the password_file line, uncomment it, and set it to /mosquitto/config/users

password_file /mosquitto/config/users

Now you can start up the container, mounting your data directory.

$ docker run -d --name mosquitto -p 1883:1883 -p 9001:9001 -v /path/to/mosquitto-data:/mosquitto eclipse-mosquitto:latest

At the moment you have no users file, so to create one exec a shell in the container and create it using the mosquitto_passwd tool. You will need to pass the -c option when adding the first user so that the new file is created.

$ docker exec -it mosquitto /bin/sh
/ # mosquitto_passwd -c /mosquitto/config/users first_username
/ # mosquitto_passwd /mosquitto/config/users another_user
/ # mosquitto_passwd /mosquitto/config/users user3
/ # exit

Now the users file should be inside the mosquitto-data/config directory of your local file system and you can restart the mosquitto container to start using it.

$ docker restart mosquitto

Previous Post Next Post