HOWTO: Beginner's Guide to Installing openHAB, Mosquitto, etc. with Docker on Debian/Ubuntu + Tips on backup and more

WIP: still being tested and tweaked

openHAB Docker Installation Guide

Source: https://github.com/jimtng/openhab-docker-setup

This is a set of step-by-step copy-pasteable instructions to install openHAB as a Docker container, along with:

  • Mosquitto MQTT Broker
  • Zigbee2MQTT (optional)

This guide works on a clean, minimal install of Ubuntu 24, but it should also work on a working full installation and possibly also on other versions of Debian based Linux.

Contents:

Expected Outcome

  • Docker and its dependencies installed
  • 3 docker containers created
    • openhab
    • mosquitto
    • zigbee2mqtt
  • The containers will run under your UID and GID. This is to make it easier to manage file permissions inside and outside the containers.
  • Directories / Files created:
    • compose.yml
    • openhab/
      • conf/ - the main openHAB’s configuration folder, often referred to as $OPENHAB_CONF
      • userdata/ - this is openHAB’s internal storage for your instance, often referred to as $OPENHAB_USERDATA
      • addons/ - this is where you can put custom add-on jar files to load into openHAB
    • mosquitto/
      • mosquitto.conf
    • zigbee2mqtt/
      • configuration.yaml
  • Your unix user will be included in the docker group
  • Zigbee2MQTT will not be fully configured yet. You’ll need to finish configuring it to connect your Zigbee dongle.

The commands in this guide will not overwrite your existing files, so if something isn’t working, ensure that the file contents are correct.

If you don’t need/use Zigbee, edit compose.yml and remove the zigbee section.

Install Docker

The following instructions were copied from https://docs.docker.com/engine/install/ubuntu/#install-using-the-repository

# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

# Add the repository to Apt sources:
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update

# Install the latest Docker version
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

Docker post install steps

# Create the docker group if it does not exist
getent group docker || sudo groupadd docker
# add the current user to the docker group
sudo usermod -aG docker $USER
# Load the new group
newgrp docker

Then do the next command to revert the main group back.

newgrp
[ $(id -u) == $(id -g) ] || echo "Warning: the current gid doesn't match your uid. Don't proceed. Try rebooting."

Create compose.yml

GID=$(id -g)
[ -f compose.yml ] && echo Warning: compose.yml already exists || cat <<EOF > compose.yml
services:
  openhab:
    image: openhab/openhab:latest
    # image: openhab/openhab:milestone
    # image: openhab/openhab:snapshot
    container_name: openhab
    restart: always
    network_mode: host
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - /etc/timezone:/etc/timezone:ro
      - ./.ssh:/openhab/.ssh
      - ./openhab/.karaf:/openhab/.karaf
      - ./openhab/conf:/openhab/conf
      - ./openhab/userdata:/openhab/userdata
      - ./openhab/addons:/openhab/addons
    environment:
      USER_ID: ${UID}
      GROUP_ID: ${GID}
      # Adjust accordingly
      OPENHAB_HTTP_PORT: 8080
      OPENHAB_HTTPS_PORT: 8443
      JAVA_MIN_MEM: 4g
      JAVA_MAX_MEM: 4g
  
  mosquitto:
    image: eclipse-mosquitto:latest
    container_name: mosquitto
    restart: always
    volumes:
      - ./mosquitto/mosquitto.conf:/mosquitto/config/mosquitto.conf
    user: "${UID}:${GID}"
    ports:
      # These ports should not be changed unless absolutely necessary
      - "1883:1883"
      - "8883:8883"

  zigbee2mqtt:
    image: koenkk/zigbee2mqtt:latest
    container_name: zigbee2mqtt
    restart: always
    # Make it run as the openhab user too
    user: "${UID}:${GID}"
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - /etc/timezone:/etc/timezone:ro
      - ./zigbee2mqtt:/app/data
    ports:
      # Adjust accordingly
      - "8088:8080"    
    # Uncomment the devices section below and map the correct device
    #devices:
      #- /dev/serial/by-id/usb-Silicon_Labs_Sonoff_Zigbee_3.0_USB_Dongle_Plus_0001-if00-port0:/dev/ttyUSB0
    depends_on:
      - mosquitto
EOF
unset GID
[ -f docker-compose.yaml ] && echo Warning: docker-compose.yaml exists. compose.yml will take precedence.

Create Directories and Config Files

mkdir -p openhab mosquitto zigbee2mqtt
[ -f mosquitto/mosquitto.conf ] || cat <<-EOF > mosquitto/mosquitto.conf
per_listener_settings true

port 1883
protocol mqtt
allow_anonymous true
allow_zero_length_clientid true
connection_messages true    
EOF

Start up the Docker Containers

docker compose up -d

Check to see if the containers are running

docker compose ps

Set the mqtt host in Zigbee2mqtt config:

sed -i 's#mqtt://localhost:#mqtt://mosquitto:#' zigbee2mqtt/configuration.yaml
docker compose restart zigbee2mqtt

Congratulations! You’ve now got openHAB up and running!

Access openHAB’s Web Interface

You should now be able to access openHAB’s web interface on http://your.server.ip:8080/

Configure Zigbee2MQTT

At this point Zigbee2MQTT isn’t fuly configured.
You need to set up the dongle/serial port mapping.

Read up on how to configure Zigbee2MQTT from https://www.zigbee2mqtt.io/guide/installation/02_docker.html,
especially about configuring the serial port to communicate with yout zigbee dongle.

The Zigbee2MQTT config file is located in zigbee2mqtt/configuration.yaml

More Tips

7 Likes

Working with Docker

Showing Container Status

docker compose ps

This command is similar to the unix ps command.

Seeing container logs

If your container doesn’t start up properly, e.g. keeps rebooting, you can see its log to find out what’s wrong.

docker compose logs zigbee2mqtt

For more options:

docker compose logs --help

Stopping/Starting a Container

docker compose [start|stop|restart] <containername>

# for example
docker compose stop openhab

# To stop all containers listed in your compose.yml file;
docker compose stop

# to restart
docker compose restart openhab

Updating Container Version

Because in the example, all the images are set to :latest, you can upgrade them by issuing the following command:

# Upgrade openhab to the latest stable version
docker compose pull openhab
# After upgrading, reload must be done.
docker compose up -d openhab

The same can be done for mosquitto / other containers.

Making Changes to compose.yml

If you modified the docker-compose.yaml file, run docker compose up -d to reload the changes. Simply restarting the docker container won’t read the new changes.

Downgrading To an Older / Specific Version

To downgrade openHAB to version 4.3.1 instead of the latest stable version, edit compose.yml, comment the :latest line and add:

# image: openhab/openhab:latest
image: openhab/openhab:4.3.1
# other options:
# image: openhab/openhab:snapshot
# image: openhab/openhab:milestone

Then do:

docker compose up -d openhab

To upgrade back to the latest (or any other) version, simply edit the image: line accordingly and run the docker compose up -d openhab again.

1 Like

Working with Mosquitto / MQTT

List the number of MQTT Clients connected to the broker

docker compose exec mosquitto mosquitto_sub -v -C 1 -t '$SYS/broker/clients/total'

Monitor MQTT Messages

All zigbee2mqtt’s traffic:

docker compose exec mosquitto mosquitto_sub -v -t 'zigbee2mqtt/#'

Or a specific device

docker compose exec mosquitto mosquitto_sub -v -t 'zigbee2mqtt/livingroom-light/#'

Publish an MQTT Message

docker compose exec mosquitto mosquitto_pub -t 'some/topic/to/publish/to' -m 'data'
1 Like

Backup/Restore

To have a complete backup, include the following files/directories:

  • Backup compose.yml
  • openhab/conf
  • openhab/userdata
  • mosquitto/mosquitto.conf
  • zigbee2mqtt/configuration.yaml

A backup can be performed while openhab is still running.

Backup with Tar

Create a tarball backup

DEST_DIR=~/backups
mkdir -p $DEST_DIR
cd
tar --verbose -czf $DEST_DIR/openhab_backup-$(date +'%Y-%m-%d').tgz \
--exclude=.git \
--exclude=openhab/conf/automation/ruby/.gem \
--exclude=openhab/userdata/backup \
--exclude=openhab/userdata/piper \
--exclude=openhab/userdata/cache \
--exclude=openhab/userdata/tmp \
compose.yml openhab/conf openhab/userdata mosquitto/mosquitto.conf zigbee2mqtt/configuration.yaml
unset DEST_DIR

Note you can create and send this tarball backup over ssh to another host too, e.g.:

tar -czf - .... | ssh user@host "cat > /path/to/target"

Restore a tarball backup

Beware not to overwrite existing installation. Make a copy of conf and userdata folder first, if you’re unsure.

tar -C $HOME -xf ~/backups/<tarballname>

Rsync

Create / update a backup copy

For example, we keep a backup copy in ~/backup. Ideally this is located on a different disk, or over the network onto another machine.

cd
rsync -av --delete \
--exclude=.git \
--exclude=conf/automation/ruby/.gem \
--exclude=userdata/backup \
--exclude=userdata/piper \
--exclude=userdata/cache \
--exclude=userdata/tmp \
--include=mosquitto/mosquitto.conf \
--exclude=mosquitto/* \
--include=zigbee2mqtt/configuration.yaml \
--exclude=zigbee2mqtt/* \
compose.yml openhab mosquitto zigbee2mqtt ~/backup
2 Likes

reserved

reserved

Will this work a a unique entry or this has to be written in cli as two different entries. I did it in only one entry.

I did not see the message:
useradd warning: openhab’s uid 9001 is greater than SYS_UID_MAX 999

I entered
cd @enter-key@
mkdir -p mosquitto @enter-key@
[ -f mosquitto/mosquitto.conf ] || cat <<-EOF > mosquitto/mosquitto.conf
per_listener_settings true @enter-key@

I entered all at once:
cd
[ -f docker-compose.yaml ] || cat < docker-compose.yaml
services:
openhab:
image: openhab/openhab:latest
container_name: openhab
restart: always
network_mode: host
volumes:
- /etc/localtime:/etc/localtime:ro
- /etc/timezone:/etc/timezone:ro
# This only works if your real user running the docker is also openhab
# - ./.ssh:/openhab/.ssh
- ./.karaf:/openhab/.karaf
- ./conf:/openhab/conf
- ./userdata:/openhab/userdata
- ./addons:/openhab/addons
environment:
# Adjust accordingly
OPENHAB_HTTP_PORT: 8080
OPENHAB_HTTPS_PORT: 8443
JAVA_MIN_MEM: 4g
JAVA_MAX_MEM: 4g

mosquitto:
image: eclipse-mosquitto:latest
container_name: mosquitto
restart: always
volumes:
- ./mosquitto/mosquitto.conf:/mosquitto/config/mosquitto.conf
ports:
# These ports should not be changed unless absolutely necessary
- “1883:1883”
- “8883:8883”

zigbee2mqtt:
image: koenkk/zigbee2mqtt:latest
container_name: zigbee2mqtt
restart: always
volumes:
- /etc/localtime:/etc/localtime:ro
- /etc/timezone:/etc/timezone:ro
- ./zigbee2mqtt:/app/data
ports:
# Adjust accordingly
- “8088:8080”
# Uncomment the devices section below and map the correct device
#devices:
#- /dev/serial/by-id/usb-Silicon_Labs_Sonoff_Zigbee_3.0_USB_Dongle_Plus_0001-if00-port0:/dev/ttyUSB0
depends_on:
- mosquitto
EOF@enter-key@

The result is:
fl@Satellite-Z930:~$ docker compose up -d
[+] Running 23/23
:check_mark: openhab Pulled 106.7s
:check_mark: zigbee2mqtt Pulled 31.8s
:check_mark: mosquitto Pulled 14.6s
[+] Running 4/4
:check_mark: Network fl_default Created 0.1s
:check_mark: Container mosquitto Started 0.5s
:check_mark: Container openhab Started 0.3s
:check_mark: Container zigbee2mqtt Started 0.6s
fl@Satellite-Z930:~$

The next step is:
Configure Zigbee2MQTT

I am expecting to get the sonoff zigbee 3 dongle on the 1rst of may.

Is there anything to do to confirm the installation process been fine?

l@Satellite-Z930:~$ docker compose ps
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
mosquitto eclipse-mosquitto:latest “/docker-entrypoint.
” mosquitto About an hour ago Up About an hour 0.0.0.0:1883->1883/tcp, [::]:1883->1883/tcp, 0.0.0.0:8883->8883/tcp, [::]:8883->8883/tcp
openhab openhab/openhab:latest “/entrypoint gosu op
” openhab About an hour ago Up About an hour (healthy)
zigbee2mqtt koenkk/zigbee2mqtt:latest “docker-entrypoint.s
” zigbee2mqtt About an hour ago Up About an hour 0.0.0.0:8088->8080/tcp, [::]:8088->8080/tcp
fl@Satellite-Z930:

Is everything working now?

I think I’m going to alter the setup a bit by having and running the docker containers inside the openhab user’s home directory instead of your normal user directory.

You should be able to move your existing setup across.

More details to come.

Here is what I get:
sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
14ae684a46b1 koenkk/zigbee2mqtt:latest “docker-entrypoint.s
” 8 hours ago Restarting (1) 45 seconds ago zigbee2mqtt
4304b4f228ed openhab/openhab:latest “/entrypoint gosu op
” 8 hours ago Up 10 minutes (healthy) openhab
0842d0066664 eclipse-mosquitto:latest “/docker-entrypoint.
” 8 hours ago Up 10 minutes 0.0.0.0:1883->1883/tcp, [::]:1883->1883/tcp, 0.0.0.0:8883->8883/tcp, [::]:8883->8883/tcp mosquitto

Stop this container for now until you have your zigbee dongle and able to configure the port. It is currently continually restarting.

docker compose stop zigbee2mqtt

@francoislavoie I’ve updated the instructions.

The main change is that it no longer needing the user openhab. Instead, all containers are running under your username. This simplifies file permissions

If you want to adjust your current installation to it, here’s what you can do:

docker compose stop
GID=$(id -g)
[ -f compose.yml ] && echo Warning: compose.yml already exists || cat <<EOF > compose.yml
services:
  openhab:
    image: openhab/openhab:latest
    # image: openhab/openhab:milestone
    # image: openhab/openhab:snapshot
    container_name: openhab
    restart: always
    network_mode: host
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - /etc/timezone:/etc/timezone:ro
      - ./.ssh:/openhab/.ssh
      - ./openhab/.karaf:/openhab/.karaf
      - ./openhab/conf:/openhab/conf
      - ./openhab/userdata:/openhab/userdata
      - ./openhab/addons:/openhab/addons
    environment:
      USER_ID: ${UID}
      GROUP_ID: ${GID}
      # Adjust accordingly
      OPENHAB_HTTP_PORT: 8080
      OPENHAB_HTTPS_PORT: 8443
      JAVA_MIN_MEM: 4g
      JAVA_MAX_MEM: 4g
  
  mosquitto:
    image: eclipse-mosquitto:latest
    container_name: mosquitto
    restart: always
    volumes:
      - ./mosquitto/mosquitto.conf:/mosquitto/config/mosquitto.conf
    user: "${UID}:${GID}"
    ports:
      # These ports should not be changed unless absolutely necessary
      - "1883:1883"
      - "8883:8883"

  zigbee2mqtt:
    image: koenkk/zigbee2mqtt:latest
    container_name: zigbee2mqtt
    restart: always
    # Make it run as the openhab user too
    user: "${UID}:${GID}"
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - /etc/timezone:/etc/timezone:ro
      - ./zigbee2mqtt:/app/data
    ports:
      # Adjust accordingly
      - "8088:8080"    
    # Uncomment the devices section below and map the correct device
    #devices:
      #- /dev/serial/by-id/usb-Silicon_Labs_Sonoff_Zigbee_3.0_USB_Dongle_Plus_0001-if00-port0:/dev/ttyUSB0
    depends_on:
      - mosquitto
EOF
unset GID
[ -f docker-compose.yaml ] && echo Warning: docker-compose.yaml exists. compose.yml will take precedence.

sudo chown -R $USER:$USER .ssh .karaf conf userdata addons mosquitto zigbee2mqtt
sudo userdel openhab
mkdir openhab
mv .karaf conf userdata addons openhab
sed -i 's#mqtt://localhost:#mqtt://mosquitto:#' zigbee2mqtt/configuration.yaml

docker compose up -d

You can delete your old docker-compose.yaml - I’ve used compose.yml in the new version.
Also you might want to stop zigbee2mqtt again until you’ve got it configured.

I did follow your last post instructions. The only problem was near the end.

fl@Satellite-Z930:~$ sudo chown -R $USER:$USER .ssh .karaf conf userdata addons mosquitto zigbee2mqtt
[sudo] password for fl:
chown: cannot access ‘.ssh’: No such file or directory
fl@Satellite-Z930:~$ mkdir -p ~/.ssh
fl@Satellite-Z930:~$ sudo chown -R $USER:$USER .ssh .karaf conf userdata addons mosquitto zigbee2mqtt
fl@Satellite-Z930:~$ sudo userdel openhab
userdel: group openhab not removed because it has other members.
fl@Satellite-Z930:~$

I have read on docker and begun some tutorials. Its seems that I have begin to have a cognitive grip on the technology.

Thanks.

Find out the group members:

grep openhab /etc/group

then clean up from there. Use userdel / groupdel. I think you can also edit /etc/group directly if you wish, just be careful not to change unrelated lines.

fl@Satellite-Z930:~$ grep openhab /etc/group
openhab:x:9001:fl

I presume fl is your username.
you can just type

groupdel openhab

Also if you previously installed openhab using apt, you might want to uninstall it and clean up other packages you installed related to it.