Running Mosquitto MQTT in a container on Windows 10

October 17, 2018

Nowadays, it is pretty much easy to run Mosquitto MQTT broker on Windows 10 using, for example, Windows Linux Subsystem (WSL). But today, I would like to show you even easier method to run it on your Windows 10 machine.

Pre-requisites

You should have Docker installed on your PC. And because Mosquitto broker image is actually a Linux image you have to switch your Docker to Linux containers.

Run Mosquitto MQTT in a container

To run your container you have to create a Docker volume first. So, open the PowerShell windows and type the command:

docker volume create mosquitto_data

This will create a new volume that Mosquitto container will use to write its data. The new volume will be stored on your local drive on C:\ProgramData\Docker\volumes.

The next thing is to create a new text file that you will use to configure Mosquitto MQTT broker. You can do that by typing the next commands:

mkdir C:\mosquitto; New-Item C:\mosquitto\mosquitto.conf -ItemType file

These commands will create a new folder mosquitto on your drive C: and create a new empty file mosquitto.conf inside this folder.

Type the next command:

docker run -itd --name="mosquitto" --restart on-failure -p 1883:1883 -p 9001:9001 -v C:\mosquitto\mosquitto.conf:/mosquitto/config/mosquitto.conf -v mosquitto_data:/mosquitto/data -v mosquitto_data:/mosquitto/log eclipse-mosquitto

This command will pull the image eclipse-mosquitto to your PC, in case it is not there. The next thing that it will do is to use this image to create a container.

To check that your Mosquitto container is up and running you can type the command:

docker ps -a

It will show you all running containers. Make sure that the status of Mosquitto container is Up.

Configuring Mosquitto

You can stop here and start using your Mosquitto MQTT broker, but I strongly recommend to continue and setup the credentials to restrict an access to the broker. To do that just type the next command:

docker exec -it mosquitto sh

It should run the shell inside the container where you have to type the next command and change to a desired username:

mosquitto_passwd -c /mosquitto/config/pwfile <username>

After that on a new line just type your password and push [Enter] and then retype your password for confirmation and push [Enter] again. And after that push [Ctrl]+Z to return to your PowerShell.

The last thing is to say to Mosquitto that it should use the pwfile to find the password. Type the next command:

notepad C:\mosquitto\mosquitto.conf

It will open Notepad where you should enter:

password_file /mosquitto/config/pwfile

Click File -> Save and close the Notepad. Now, to apply your changes, you have to restart Mosquitto container. You can do that by typing in a PowerShell window:

docker restart mosquitto

Test Mosquitto

The easiest way to test that your Mosquitto broker works correctly is to use Windows Linux Subsystem (WSL) to install any of Linux distributable, for example, Ubuntu, where you can type the next command to install mosquitto-clients to subscribe to or post messages to the topics:

sudo apt install mosquitto-clients

After that you can open two windows of Ubuntu and type the next command in the fist window to subscribe to all topics:

mosquitto_sub -v -h 127.0.0.1 -p 1883 -t "#" -u <username> -P <password>

Then type the next command in the second window:

mosquitto_pub -d -t "test/topic1" -m "First test" -u <username> -P <password>

The last command will send the message "First test" to the topic "test/topic1". This message should be immediately visible in the first window. This simple test just proves that your Mosquitto MQTT broker was successfully installed and works correctly.

Conclusion

Using the Docker containers it is very easy to install any software nowadays. It just requires some basic knowledge of Docker and 5 minutes of your time.

I hope this post helps some of you guys. So, please leave a comment and share your experience.

Recommended content

Comments

Leave your comment