Run Basic Mosquitto Broker in Docker

Steps to run a mosquitto broker in a docker:

1- Create two files:

First one mosquitto.conf where you have the basic configuration for your broker. We explain the mosquitto passwd file later.

[dalmine@rosas mosquitto]$ cat mosquitto.conf
listener 9001
protocol websockets
allow_anonymous false
password_file /mosquitto/config/passwd
[dalmine@rosas mosquitto]$

Then create an empty passwd file.

[dalmine@rosas mosquitto]$touch passwd

 

The first line of mosquitto.conf is about listener, 9001 is the default for websocket, I choose it because you can build or code other client with a lot of languages using webscoket, also you have mqtt 1883, mitts 8883 or random port as you wish , but you need to choose the protocol.

2- Set password to login

/ # mosquitto_passwd passwd alliot
Error: Empty password.
Password: / #

/ # cat passwd
batman:$6$3qbtsdsdfsdfsdfsfdsdfshdflsjdfjlksdfhskdjfh==
/ # ls

We have the command mosquitto_passwd it is part of mosquitto packages.

 

3- Run Docker

Then we need to run docker command with a lot parameter we want to avoid configuration with any provisioning tools like compose,k8s,etc,etc,etc…

 

[dalmine@rosas mosquitto]$ sudo docker run -it -p 9001:9001 -v $(pwd)/mosquitto.conf:/mosquitto/config/mosquitto.conf -v $(pwd)/passwd:/mosquitto/config/passwd -d –name mosquitto eclipse-mosquitto
91652c904c86220f43ad6d7de2402f54c30f438bd3f59b217ef6fb7e4c61127e

The command is (ONE LINE) –> docker run -it -p 9001:9001 -v $(pwd)/mosquitto.conf:/mosquitto/config/mosquitto.conf -v $(pwd)/passwd:/mosquitto/config/passwd -d –name mosquitto eclipse-mosquitto

 

4-Check logs:

[dalmine@rosas mosquitto]$ docker logs -f mosquitto
1528666018: mosquitto version 1.4.12 (build date 2017-06-01 13:03:46+0000) starting
1528666018: Config loaded from /mosquitto/config/mosquitto.conf.
1528666018: Opening websockets listen socket on port 9001.

 

We are open to receive new connections. Now we can use the mosquitto client command  line or any web/app client.

1528667142: New client connected from 45.232.92.159 as lens_HJ9OowTbqNS11toqxXxt4eFnFew (c1, k120, u’alliot’).1528667142: New client connected from 45.232.92.159 as lens_HJ9OowTbqNS11toqxXxt4eFnFew (c1, k120, u’batman’).1528667142: Sending CONNACK to lens_HJ9OowTbqNS11toqxXxt4eFnFew (0, 0)

1528667150: Received PUBLISH from lens_HJ9OowTbqNS11toqxXxt4eFnFew (d0, q0, r0, m0, ‘dc/batman/test’, … (4 bytes))1528667155: Received SUBSCRIBE from lens_HJ9OowTbqNS11toqxXxt4eFnFew1528667155: dc/batman/test (QoS 0)1528667155: lens_HJ9OowTbqNS11toqxXxt4eFnFew 0 dc/batman/test1528667155: Sending SUBACK to lens_HJ9OowTbqNS11toqxXxt4eFnFew1528667159: Received PUBLISH from lens_HJ9OowTbqNS11toqxXxt4eFnFew (d0, q0, r0, m0, ‘dc/batman/test’, … (4 bytes))1528667159: Sending PUBLISH to lens_HJ9OowTbqNS11toqxXxt4eFnFew (d0, q0, r0, m0, ‘dc/batman/test’, … (4 bytes))1528667160: New client connected from 45.232.92.159 as baticueva (c1, k30, u’alliot’).1528667160: Sending CONNACK to baticueva (0, 0)1528667160: Received SUBSCRIBE from baticueva1528667160: dc/dc/batman/baticueva/# (QoS 1)1528667160: baticueva 1 dc/dc/batman/baticueva/#1528667160: Sending SUBACK to baticueva

1528667160: Sending SUBACK to baticueva
1528667190: Received PINGREQ from baticueva
1528667190: Sending PINGRESP to baticueva
1528667220: Received PINGREQ from baticueva
1528667220: Sending PINGRESP to baticueva

The logs depends in what kind of action are you doing with broker, you hace two main action, PUBLISH  where you send a message to the broker, and the other is SUBSCRIBE.

With any client you can set be a publisher or a subscriber. Keep in mind you have QoS , quality of service where determine how you can confirm your messages.

You can now connect to your mosquitto broker with your favorite client.

 

 

 

Links

WS Mosquito

Mosquitto ECLIPSE DOCS

Mosquitto docker file

MOsquitto Password Config

 

 

 

Leave a Reply