How to install MQTT with docker

The official dockerhub link.

  • Create config file mosquitto.conf under /data/mosquitto/config/
# allow_anonymous true
listener 1883
persistence true
persistence_location /mosquitto/data/
log_dest file /mosquitto/log/mosquitto.log
password_file /mosquitto/pwd*
  • Create file pwd under /mosquitto/ in docker
touch pwd
  • Add user for pwd
mosquitto_passwd -c /mosquitto/pwd root
  • Execute following line to start mqtt docker container
docker run -it -d --name mosquitto -v /data/mosquitto/config/mosquitto.conf:/mosquitto/config/mosquitto.conf -v /data/mosquitto/data:/mosquitto/data -v /data/mosquitto/log:/mosquitto/log --restart always -p 1883:1883 -p 9001:9001 eclipse-mosquitto

Now you can use cli to test your mqtt

  • Subscribe data
docker run --rm hivemq/mqtt-cli sub -t '#' -h ourdark.org -u root -pw Forhuman1999@
  • publish message
docker run --rm -it hivemq/mqtt-cli pub -t test -h ourdark.org -m "aaa"

or

docker run --rm -it hivemq/mqtt-cli shell
con -i test -h ourdark.org
pub -t test -m "aaa"

Enable Websocket

Add flollowing lines to config file:

listener 8080
protocol websockets

Then restart docker and expose port 8080

docker run -it -d --name mosquitto -v /data/mosquitto/config/mosquitto.conf:/mosquitto/config/mosquitto.conf -v /data/mosquitto/data:/mosquitto/data -v /data/mosquitto/log:/mosquitto/log --restart always -p 1883:1883 -p 9001:9001 -p 8080:8080 eclipse-mosquitto

Well, you can use your single node mqqt server, it support mqtt and websocket protocol now.