8 Steps to Instal Mosquitto, InfluxDB and Grafana via Docker

Now that you have Home Assistant installed, you will need to install MQTT broker to allow interaction between your IoT devices with Home Assistant. In this post, we are going to install the Mosquitto MQTT broker.

step 1. To start you can execute the following command

docker pull eclipse-mosquitto

This will download the latest image.

Step 2. Sharing info between host and Docker container

Before starting the container we need to make sure that the information are being shared between the host system and the Docker container. In order to do this you will need to create three directory: config, data and log under the mosquitto default directory.

Let us assume that your mosquitto default directory will be under the folder called mosquitto. So you can create them using the following command:

mkdir mosquitto
mkdir mosquitto\config
mkdir mosquitto\data
mkdir mosquitto\log

Step 3. Create the mosquitto config

Now that you have created the above folder structure it is time to create the configuration in the config folder using the following command

nano mosquitto/config/mosquitto.conf

Copy and paste the following context:

pid_file /var/run/mosquitto.pid

persistence true
persistence_location /mosquitto/data/

log_dest file /mosquitto/log/mosquitto.log
log_dest stdout

password_file /mosquitto/config/mosquitto.passwd
allow_anonymous false

You can saved the file using ^O and close nano using ^X.

Step 4. Change permission of the mosquitto configuration folder

To ensure that the Docker container get the permission to access these folder, you can change the permission using the chown command. Use the following command to give Mosquitto access to the mosquitto folder via port 1883.

chown -R 1883:1883 ~/mosquitto

Step 5. Create the Mosquitto container

Now you can create the Mosquitto container using the following command:

docker run -it -p 1883:1883 --name mosquitto -v ~/mosquitoconfig -v ~/mosquitto/data:/mosquitto/data -v ~/mosquitto/log:/mosquitto/log eclipse-mosquitto

The Mosquitto container should be running now, you can check this using the following command

sudo docker container ls -a

You can also see the container ID for Mosquitto, my container id is ca115f8382cf , yours will be differed. You will need this for the next step to setup the username and password for Mosquitto.

Step 6. Setup username and password for Mosquitto

It is important to setup the username and password for the Mosquitto to ensure that you have security in place right from the beginning.

Access the shell of the container using the following command:

docker exec -it ca115f8382cf sh

type the following command to set your username and password. In this case I am using effendy as my username.

mosquitto_passwd -c /mosquitto/config/mosquitto.passwd effendy

You will be prompted for the password twice, make sure you noted this as you will need the username and password for the IoT devices to be able to post messages to Mosquitto.

Step 7. Install InfluxDB via Docker

Now we can install InfluxDB using the following command

docker pull influxdb

We also need to share host and container for InfluxDB. So we create a folder called influxdb using the following command

mkdir influxdb

To start Influxdb you can call the following:

docker run --name influxdb -p 8086:8086 -v influxdb:/var/lib/influxdb influxdb

Step 8. Install Grafana via Docker

Pull the grafana image using the following command

docker pull grafana/grafana

To start Grafana you can execute the following command:

docker run -d --name=grafana -p 3000:3000 grafana/grafana

Now you should see the Home Assistant, Mosquitto, Influxdb and Grafana all running:

Now that all the are setup we are ready to push MQTT message to Home Assistant and being able to save it into InfluxDB and subsequently display it using Grafana.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.