Deploying Mosquitto message broker with Docker

Mosquitto is an open-source message broker based on lightweight MQTT (Message Queuing Telemetry Transport) protocol, designed for Internet of Things (IoT) applications and low-bandwidth communication.

I will deploy Mosquitto using the official docker image. This docker image contains 3 directories which are used for configuring Mosquitto when the container runs, these 3 directories are as follows.
a. /mosquitto/config
b. /mosquitto/data
c. /mosquitto/log

As shown below, here I have created three directories to save the configuration related to mosquitto

mkdir -p mosquitto/{mq_config,mq_data,mq_log}

Goto mq_config, create mosquitto.conf file & add the following configuration in it.

allow_anonymous true
listener 1883 0.0.0.0
persistence true
persistence_location /mosquitto/data/
log_dest file /mosquitto/log/mosquitto.log

Run the following command to create docker container:

docker run -d --name mosquitto-server -p 1883:1883 -v "$HOME/mosquitto/mq_config:/mosquitto/config" -v /mosquitto/data -v /mosquitto/log eclipse-mosquitto

Verify the config file is mounted inside the running container

docker exec -it mosquitto-server sh

After entering in the container, goto mosquitto/config directory and verify the mosquitto.conf file and check its content

As I am still inside the container, Lets try to send a MQTT message directly from command line.

mosquitto_pub -h localhost -t test -m "test"

If terminal move to next-line without any error then the command executes successfully & Mosquitto message broker successfully deployed