openHab: How to install as a Docker Container for Home Automation

The openHab project has excellent written guides at openHab: documentation. My work is based on the openHAB 2 inside a Docker Container guide. I’ll be using Docker built on top of Ubuntu Server 16.04.

Preparation:

First, create a new user non-root system account, and group, on the Linux system running the Docker service.

For our purposes we will create the following:

User openhab
Group openhab

This will run the openHab container using the created account called ‘openhab’ and group called ‘openhab’.

sudo useradd -r -s /sbin/nologin openhab
sudo usermod -a -G openhab openhab

Code explanation: TGP: useradd
Code explanation: TGP: usermod

We will need to get the user and group IDs for accounts we just created with the  id <account> command.

javier@docker:~$ id openhab
uid=9001(openhab) gid=9001(openhab) groups=9001(openhab)

The ‘openhab’ user ID ‘uid’ is 9001
The ‘openhab’ group ID ‘gid’ is 9001

Second, lets now create the configuration directories and adjust folder permissions. They will hold the openHab configuration files on the Docker host file system. For my purposes I use the /opt/containters/ parent directory for organizational purposes.

sudo mkdir -p /opt/containers/openhab/{conf,userdata,addons}
sudo chown -R openhab:openhab /opt/containers/openhab/

Code explanation: TGP: mkdir
Code explanation: TGP: chown

Docker Implementation:

I’m going run the openHab container to be managed by the Docker application. It makes sense for me to run it as a container along with the other containers I run on the same Docker application. Alternatively, the system can manage the openHab container via Systemd. To set up in that manner take a look at the openHab docs at: openHAB 2 inside a Docker Container: Running the Container as a Service Controlled by Systemd

docker run -d \
--name openhab \
--net=host \
--tty \
-v /etc/localtime:/etc/localtime:ro \
-v /etc/timezone:/etc/timezone:ro \
-v /opt/containers/openhab/addons:/openhab/addons \
-v /opt/containers/openhab/conf:/openhab/conf \
-v /opt/containers/openhab/userdata:/openhab/userdata \
-e USER_ID=9001 \
-e GROUP_ID=9001 \
-e OPENHAB_HTTP_PORT=8484 \
-e OPENHAB_HTTPS_PORT=8444 \
openhab/openhab:2.2.0-amd64-debian

Some important notes around the docker command:

–net=host forces container to share physical network stack with Docker host. Required for UPnP functionality.
–tty required for Karaf Console.
–device=/dev/ttyUSB0 can be added of physical zwave USB controller

Note: Due to Docker limitations if you use the  --net=host parameter you will not be able to communicate with any other containers on the same Docker host.

you can now access the openHab web interface at:
http://<local IP address docker host>:8484

Important Notes:

After the openHab container is up and running the Docker host file system will like like this:

/opt/containers/openhab/
 ├── addons
 ├── conf
 │   ├── html
 │   ├── icons
 │   ├── items
 │   ├── persistence
 │   ├── rules
 │   ├── scripts
 │   ├── services
 │   ├── sitemaps
 │   ├── sounds
 │   ├── things
 │   └── transform
 └── userdata
     ├── cache
     ├── config
     ├── etc
     ├── jsondb
     ├── kar
     ├── logs
     └── tmp

Many of your specific configuration files will be in the  /opt/containers/openhab/conf directory. The log files will be located in  /opt/containers/openhab/userdata/logs directory.

In addition since the log files are on the Docker file system the running the following command can give a real-time events as you make configuration changes:
tail -F /opt/containers/openhab/userdata/logs/openhab.log /opt/containers/openhab/userdata/logs/events.log

If you need to restart the openHab service the easiest thing is to stop and start the openHab container.

Next Steps:

Now you can start configuring your openHab application using this guide at: openHab: Configuration of your Smart Home

Advanced:

If you are familiar with using Docker compose you can build the container using the following compose yaml file.

version: "3"
services:

  openHab:
    image: openhab/openhab:2.2.0-amd64-debian
    environment:
      - USER_ID=9001
      - GROUP_ID=9001
      - OPENHAB_HTTP_PORT=8484
      - OPENHAB_HTTPS_PORT=8444
    network_mode: "host"
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - /etc/timezone:/etc/timezone:ro
      - /opt/containers/openhab/addons:/openhab/addons
      - /opt/containers/openhab/conf:/openhab/conf
      - /opt/containers/openhab/userdata:/openhab/userdata
    tty: true
    # devices:
    #   - "/dev/ttyUSB0:/dev/ttyUSB0"

About the Author

%d bloggers like this: