Openhab: Install and run openhab from a docker image

Hi,

this port describes how to quickly install an openhab instance from a docker image. In this example Openhab runs under user/group openhab and the network of the docker container is connected to the host interface.

Before starting checkout the install docker post to get the system ready for hosting openhab.


Create the user and group openhab

root@debdev ~ # useradd -r -s /sbin/nologin openhab
root@debdev ~ # usermod -a -G openhab openhab

Create folders for holding the config on the host system ant not inside the container.

root@debdev ~ # mkdir -p /opt/openhab/conf
root@debdev ~ # mkdir -p /opt/openhab/userdata
root@debdev ~ # mkdir -p /opt/openhab/addons
root@debdev ~ # chown -R openhab:openhab /opt/openhab

Get UID/GID

root@debdev ~ # export USER_ID=$(egrep '^openhab' /etc/passwd|awk -F: '{print $3}')
root@debdev ~ # export GROUP_ID=$(egrep '^openhab' /etc/group|awk -F: '{print $3}')

If you are behind a proxy and have no direct connection to the Internet, configure the proxy server.

Create a config directory for the docker service

root@debdev ~ # mkdir /etc/systemd/system/docker.service.d

Create a file /etc/systemd/system/docker.service.d/http-proxy.conf

[Service]
Environment="HTTP_PROXY=http://192.168.254.254:888"
Environment="HTTPS_PROXY=http://192.168.254.254:888"
Environment="NO_PROXY=localhost,127.0.0.0/8"

and ~/.docker/config.json

{
 "proxies":
 {
   "default":
   {
     "httpProxy": "http://192.168.254.254:888",
     "httpsProxy": "http://192.168.254.254:888",
     "noProxy": "localhost"
   }
 }
}

Reload config

root@debdev ~ # systemctl daemon-reload
root@debdev ~ # systemctl show --property Environment docker
root@debdev ~ # systemctl restart docker

To select the version you want to start look at docker hub for all startable images and tags

Run openhab. This one is for version 2.5.0-M3 on Standard PC Hardware amd64. -d means openhab detaches from the console and runs in background. For testing purposes omit the -d switch then you see all output messages at the console.

root@debdev ~ # docker run \
        --name openhab-2.5.0-M3 \
        --net=host \
        -v /etc/localtime:/etc/localtime:ro \
        -v /etc/timezone:/etc/timezone:ro \
        -v /opt/openhab/conf:/openhab/conf \
        -v /opt/openhab/userdata:/openhab/userdata \
        -v /opt/openhab/addons:/openhab/addons\
        -d \
        -e OPENHAB_HTTP_PORT=8080 \
        -e OPENHAB_HTTPS_PORT=8443 \
        -e USER_ID=$USER_ID \
        -e GROUP_ID=$GROUP_ID \
        --restart=always \
         openhab/openhab:2.5.0.M3-amd64-debian 

If you are behind a proxy, pass the proxy in the run command by the EXTRA_JAVA_OPTS environment variable to the docker container

...
-e OPENHAB_HTTP_PORT=8080 \
-e OPENHAB_HTTPS_PORT=8443 \
-e EXTRA_JAVA_OPTS="-Dhttp.proxySet=true -Dhttp.proxyHost=192.168.254.254 -Dhttp.proxyPort=888 -Dhttps.proxySet=true -Dhttps.proxyHost=192.168.254.254 -Dhttps.proxyPort=888" \
-e USER_ID=$USER_ID \
...

Get a shell within the container

root@debdev ~ # docker exec -it openhab-2.5.0-M3 sh -c "/bin/bash"

Start the openhab command line (Karaf console) within the container

root@debdev ~ # docker exec -it openhab-2.5.0-M3 sh -c "/openhab/runtime/bin/client"
Logging in as openhab

                          __  _____    ____
  ____  ____  ___  ____  / / / /   |  / __ )
 / __ \/ __ \/ _ \/ __ \/ /_/ / /| | / __  |
/ /_/ / /_/ /  __/ / / / __  / ___ |/ /_/ /
\____/ .___/\___/_/ /_/_/ /_/_/  |_/_____/
    /_/                        2.5.0.M3
                               Milestone Build

Open a browser and connect to port 8080

http://yourIPAddress:8080

Review logs by

root@debdev ~ # docker logs openhab-2.5.0-M3

Stopping and starting the container

root@debdev ~ # docker stop openhab-2.5.0-M3
root@debdev ~ # docker start openhab-2.5.0-M3

Have fun 🙂

Michael

Reference: OpenHAB

Leave a Reply