Running openHAB in Docker

Tags:

I’ve been running openHAB for some time and I recently decided to recreate my setup in docker to run it on a bigger shared server instead of a Raspberry Pi 4. It was fairly easy but I did have to deviate a little for my configuration from what the documentation suggested.

I like using docker-compose to launch my containers and for my setup I rely on MQTT heavily for some things I do so I made sure both are configured. I also use Traefik to expose some services and therefor I had to change some ports around a bit.

The host server runs in Arch Linux and caused a few issues that I’ll get into shortly.

Docker Compose

docker-compose.yml
version: '3.7'
services:

  openhab:
    image: openhab/openhab:latest
    ports:
      - 9085:9085
    depends_on:
      - mqtt
    volumes:
      - ./conf:/openhab/conf
      - ./userdata:/openhab/userdata
      - ./addons:/openhab/addons
      - ./cont-init.d:/etc/cont-init.d
    environment:
      - OPENHAB_HTTP_PORT=9085
      - OPENHAB_HTTPS_PORT=9443
      - CRYPTO_POLICY=unlimited
      - USER_ID=1000
      - GROUP_ID=1000
    devices:
      - "/dev/ttyUSB0:/dev/ttyUSB0"
      - "/dev/ttyUSB1:/dev/ttyUSB1"
#      - "/dev/ttyACM0:/dev/ttyACM0"
    labels:
    - "traefik.enable=true"

    - "traefik.http.routers.openhab-s.middlewares=https_redirect@file"
    - "traefik.http.routers.openhab-s.rule=Host(`openhab.example.com`)"
    - "traefik.http.routers.openhab-s.entrypoints=web"

    # https
    - "traefik.http.routers.openhab.rule=Host(`openhab.example.com`)"
    - "traefik.http.routers.openhab.entrypoints=websecure"
    - "traefik.http.routers.openhab.tls=true"
    - "traefik.http.routers.openhab.tls.certresolver=myresolver"
    - "traefik.http.routers.openhab.service=openhab"
    - "traefik.http.services.openhab.loadbalancer.server.port=9085"

  mqtt:
    image: eclipse-mosquitto:latest
    volumes:
      - ./mqtt/config:/mosquitto/config
      - ./mqtt/data:/mosquitto/data
      - ./mqtt/log:/mosquitto/log
    user: "1000:1000"
    ports:
      - 1883:2883

openHAB

Most of the contents of the docker-compose.yml file are for openHAB and a few are for MQTT so I’ll mostly document those for openHAB.

Volumes

conf

I created a conf folder to hold the following folders: automation, html, icons, items, persistence, rules, scripts, services, sitemaps, sounds, things and transform. This is the main place I’d add/edit files to run my home.

userdata

This folder will get automatically created when I run docker-compose but it will contains openHAB state data and logs, etc. I frequently monitor logs with tail:

tail -F ./userdata/logs/*.log

addons

I dont use this for anything yet.

cont-init.d

This folder contains a few extra scripts that I run during openHAB startup. For my setup, I found ArchLinux doesn’t have the timezone/localtime files configured the way the documentation suggests so my script maps things so they work better at startup.

./cont-init.d/timezone.sh
echo "setting timezone"
rm -f /etc/localtime
rm -f /etc/timezone
ln -s /usr/share/zoneinfo/America/Los_Angeles /etc/localtime
echo "America/Los_Angeles" > /etc/timezone

Arch linux also maps the uucp group to a different ID than what the openHAB container uses so I modified it to fix issues I had with permissions at startup:

./cont-init.d/uucp.sh
sed -e 's/uucp:x:10/uucp:x:987/' /etc/group -i

Environment

I have a user on the host system in the uucp group to access the Z-Wave / ZigBee dongle and I want all the files created by the container to be own by the same UID so I added the USER_ID and GROUP_ID values.

Devices

My dongle wasn’t mapped to ttyACM0 on my system so I just made sure to mount the correct ones.

Labels

These labels are only used for Traefik. If you don’t use it then you won’t need them.

MQTT

The only interesting thing about the MQTT setup is the ports. Initially I tried running everything in the normal port 1883 but I ran into permission problems. Someone online suggested using 8883 but that seemed to cause protocol problems (TLS port) so I ended up using 2883 but I didn’t want to reconfigure everything to use a different port. This resulted in a single file:

./mqtt/config/mosquitto.conf
persistence true
persistence_location /mosquitto/data/
log_dest file /mosquitto/log/mosquitto.log

allow_anonymous true
listener 2883

protocol mqtt