This tutorial is about installing MQTT and Openhab 3 in a docker container. It will use docker-compose to run yaml file with MQTT and Openhab 3 configuration.
First, we need to install Docker on Rasbperry Pi.
Install Docker Compose on Raspberry Pi 4
curl -sSL https://get.docker.com | sh
Add pi user to docker group.
sudo usermod -aG docker pi
To install docker compose, we need to install Python and Pip first
sudo apt-get install libffi-dev libssl-dev python3-dev python3 python3-pip -y
Then install Compose using Pip
sudo pip3 install docker-compose
To avoid problems, we need to reboot.
sudo reboot
Install Mosquitto on a Docker
To install Eclipse Mosquitto on Raspberry Pi 4, we need to create configuration file first.
We will create a docker folder first. We recommend installing samba and share this folder. Because It’s easy to create and modify yaml file using Microsoft Code.
cd
mkdir docker
Create folders for MQTT and Openhab inside docker folder.
cd docker
mkdir smarthome
mkdir smarthome/mqtt
mkdir smarthome/mqtt/config
Write the MQTT configuration file.
nano smarthome/mqtt/config/mosquitto.conf
Paste this code:
# Config file for mosquitto
listener 1883
persistence true
persistence_location /mosquitto/data/
log_dest file /mosquitto/log/mosquitto.log
allow_anonymous false
Save it and exit.
Now let write docker-compose yaml file inside the smarthome folder:
cd smarthome
nano docker-compose.yaml
Paste this code:
version: '3'
services:
#mqtt
mosquitto:
container_name: mqtt
#hostname: mosquitto
restart: always
image: eclipse-mosquitto
ports:
- "1883:1883"
- "9001:9001"
volumes:
- ./mqtt/config/mosquitto.conf:/mosquitto/config/mosquitto.conf
- ./mqtt/data:/mosquitto/data
- ./mqtt/log:/mosquitto/log
networks:
- default
networks:
default:
Save and exit
Run docker compose
docker-compose up -d
To check if it is running:
docker ps
Run a new command in the running MQTT container
docker exec -it mqtt sh
Now that we are the MQTT container command, we can change the username and password of MQTT.
- Username: mymqtt
- Password: mypassword
mosquitto_passwd -c /mosquitto/data/pwfile mymqtt
You will be asked to enter a password twice.
Let us exit the command:
exit
To configure Mosquitto, we have to stop the container first
docker stop mqtt
Edit Mosquitto config file:
sudo nano ~/docker/smarthome/mqtt/config/mosquitto.conf
Paste this at the bottom
password_file /mosquitto/data/pwfile
Start the container
docker start mqtt
docker ps
Let us test if it works.
Now we will need two ssh terminal. First terminal for subscription and second terminal to issue publish command.
On the first terminal, subscribe to topic home/bedroom
docker exec -it mqtt sh
mosquitto_sub -u mymqtt -P mypassword -d -t home/bedroom
On the second terminal, publish a hello message
docker exec -it mqtt sh
mosquitto_pub -u mymqtt -P mypassword -d -t home/bedroom -m "Hello from Terminal 2!"


You should see a hello message received on Terminal 1
Install Openhab 3 using Docker Compose
We need to edit the docker-compose.yaml file we created earlier and add Openhab service.
But to avoid confusion, we will delete the docker-compose.yaml and create a new one:
cd ~/docker/smarthome
rm docker-compose.yaml
Create an new yaml file
nano docker-compose.yaml
Paste this code:
version: '3'
services:
#mqtt
mosquitto:
container_name: mqtt
#hostname: mosquitto
restart: always
image: eclipse-mosquitto
ports:
- "1883:1883"
- "9001:9001"
volumes:
- ./mqtt/config/mosquitto.conf:/mosquitto/config/mosquitto.conf
- ./mqtt/data:/mosquitto/data
- ./mqtt/log:/mosquitto/log
networks:
- default
#OPENHAB
openhab:
container_name: openhab
image: "openhab/openhab:3.2.0"
restart: always
volumes:
- ./openhab/addons:/openhab/addons
- ./openhab/conf:/openhab/conf
- ./openhab/userdata:/openhab/userdata
environment:
OPENHAB_HTTP_PORT: "8080"
OPENHAB_HTTPS_PORT: "8443"
EXTRA_JAVA_OPTS: "-Duser.timezone=Asia/Dubai"
network_mode: host
networks:
default:
Save it, exit and run it.
docker-compose up -d
Check the container if it is running
docker ps
Please wait for a minute for openhab service completely up and running.
Open your browser and type in the URL the IP address of your Raspberry Pi following with port 8080. Something like this http://192.168.1.20:8080

Enter your desired username and password
You are done, you just installed MQTT and Openhab 3.
Easier way to Install MQTT and Openhab 3
Using Docker Compose, we can install MQTT and Openhab 3 in a much easier way. We will write a yaml file and edit the configuration of MQTT.
Please remember that this is a repeat of the tutorial above.
To get started, we need to have a clean everything we did. So, we have to delete the container and delete all the files inside the docker directory smarthome.
docker stop mqtt
docker stop openhab
docker rm mqtt
docker rm openhab
#please be careful. Make sure you are deleting in the right location.
cd ~/docker/smarthome
sudo rm * -R
Now that we had a clean directory, let’s get started.
Create a compose yaml file:
nano docker-compose.yaml
Paste this code:
version: '3'
services:
#mqtt
mosquitto:
container_name: mqtt
#hostname: mosquitto
restart: always
image: eclipse-mosquitto
ports:
- "1883:1883"
- "9001:9001"
volumes:
- ./mqtt/config/mosquitto.conf:/mosquitto/config/mosquitto.conf
- ./mqtt/data:/mosquitto/data
- ./mqtt/log:/mosquitto/log
networks:
- default
#OPENHAB
openhab:
container_name: openhab
image: "openhab/openhab:3.2.0"
restart: always
volumes:
- ./openhab/addons:/openhab/addons
- ./openhab/conf:/openhab/conf
- ./openhab/userdata:/openhab/userdata
environment:
OPENHAB_HTTP_PORT: "8080"
OPENHAB_HTTPS_PORT: "8443"
EXTRA_JAVA_OPTS: "-Duser.timezone=Asia/Dubai"
network_mode: host
networks:
default:
Save, exit, and run docker compose.
docker-compose up -d
You will receive an error: Cannot start service Mosquitto OCI runtime create failed.

Go to mqtt configuration file:
cd mqtt
cd config
ls
You will see a folder name mosquitto.conf was created. Delete this folder.
sudo rm mosquitto.conf -R
Now create a new file called mosquitto.conf
sudo nano mosquitto.conf
Paste this configuration:
# Config file for mosquitto
listener 1883
persistence true
persistence_location /mosquitto/data/
log_dest file /mosquitto/log/mosquitto.log
allow_anonymous false
Save, exit, and run docker compose again, or start the docker container
#go back to directory first
cd ~/docker/smarthome
docker-compose up -d
It should work.
Set username and password for Mqtt
Run a new command in the running mqtt container
docker exec -it mqtt sh
Now that we are the mqtt container command, we can change the username and password of mqtt.
- Username: mymqtt
- Password: mypassword
mosquitto_passwd -c /mosquitto/data/pwfile mymqtt
You will be asked to enter a password twice.
Let’s exit the command:
exit
To configure Mosquitto, we have to stop the container
docker stop mqtt
Edit Mosquitto config file:
sudo nano ~/docker/smarthome/mqtt/config/mosquitto.conf
Paste this at the bottom
password_file /mosquitto/data/pwfile
Start the container
docker start mqtt
docker ps
Let us test if it works.
Follow the next tutorial: