Skip to content
Mattias Martikainen edited this page Feb 5, 2018 · 8 revisions

Using the image toke/mosquitto from Docker Hub

Create the following directories and files

sudo mkdir /Docker/mqtt/config/
sudo mkdir /Docker/mqtt/logs/
sudo mkdir /Docker/mqtt/data/
sudo mkdir /Docker/mqtt/config/mosquitto.passwd
sudo mkdir /Docker/mqtt/config/mosquitto.conf

Add the following to /Docker/mqtt/config/mosquitto.conf

persistence true
persistence_location /mqtt/data/
log_dest file /mqtt/log/mosquitto.log
log_dest stdout
allow_anonymous false
password_file /mqtt/config/mosquitto.passwd

Create the container

docker create --name mqtt --restart=always -p 1883:1883 -e PGID=1000 -e PUID=1000 -v /Docker/mqtt/config:/mqtt/config -v /Docker/mqtt/log:/mqtt/log -v /Docker/mqtt/data/:/mqtt/data/  toke/mosquitto

Start the container

docker start mqtt

Login to the container

docker exec -it mqtt bash

Use the mosquitto_passwd utility to create a user for the mqtt broker

mosquitto_passwd -c /mqtt/config/mosquitto.passwd mqtt

enter the password twice then leave the container

exit

restart it and test that it require password to login

docker restart mqtt

To try it without password, should give you a connection refused

mosquitto_sub -h 192.168.1.5 -v -t '#'

Try with password

mosquitto_sub -h 192.168.1.5 -u mqtt -P PASSWORD -v -t '#'

Command to publish something

mosquitto_pub -h 127.0.0.1 -u mqtt -P PASSWORD -t "homeassistant/cover/1/config" -m "20"
Clone this wiki locally