28/07/2020

8 Steps to Instal Mosquitto, InfluxDB and Grafana via Docker

By snorlaxprime

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

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:

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

Copy and paste the following context:

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.

Step 5. Create the Mosquitto container

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

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

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:

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

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.

Restart the mosquitto container

Test the installation by using the following command

Assuming you have setup your home assistant to monitor the home/livingroom/temperature, you should see the sensor value being updated

NB:Reference https://github.com/sukesh-ak/setup-mosquitto-with-docker?tab=readme-ov-file

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.