Mosquitto MQTT Broker with Docker Compose

From wiki.N4VX.net
Jump to navigation Jump to search

How to setup standalone mosquitto MQTT broker using docker-compose

create docker-compose.yml

version: "3"
  services:
    mosquitto:
      image: eclipse-mosquitto
      network_mode: host
      volumes:
        - ./conf:/mosquitto/conf
        - ./data:/mosquitto/data
        - ./log:/mosquitto/log

Now create ./conf/mosquitto.conf

persistence true
persistence_location /mosquitto/data/
log_dest file /mosquitto/log/mosquitto.log
listener 1883
## Authentication ##
# allow_anonymous false
password_file /mosquitto/conf/mosquitto.passwd

Now create the first user by running the command

docker-compose exec mosquitto mosquitto_passwd -c /mosquitto/conf/mosquitto.passwd mosquitto SuperSecretPa55wordText

You can optionally create more users using the -b (batch) flag instead of -c , supplying the password on the command line:

docker-compose exec mosquitto mosquitto_passwd -b /mosquitto/conf/mosquitto.passwd seconduser AnotherSuperSecretPa55wordText

Now start mosquitto using

docker-compose up