Running openHAB on a QNAP NAS with Docker

September 16, 2018 5 mins read

Running openHAB on a QNAP NAS with Docker

I have recently decided to start building a smart home. A colleague of mine who has lots of experience and even writes a blog about smart home has recommended me to take a look at openHAB. openHAB is an open-source IoT platform that is capable of integrating various standards and devices and therefore seemed like a perfect fit.

I first thought about installing openHAB on a Raspberry PI, but then decided to run it on my QNAP NAS, as the NAS is running anyways. As openHAB provides a Docker image and I already used the QNAP Container Station to run Crashplan online backup with Docker I immediately got excited and had to install openHAB with Docker on my QNAP NAS running QTS 4.3.

openHAB Data Folder Structure

First, set up the folder structure on the QNAP NAS to store the openHAB data. As with all services installed through Docker, I prefer to store the data in a central location: /share/CACHEDEV1_DATA/Containers/<service>. By doing so, you always know where to look for the data. So let’s get started on the shell:

# first, login to your NAS
$ ssh admin@<NAS_IP>

# then create the folders on your mounted storage (usually /share/CACHEDEV1_DATA on QNAP)
mkdir -p /share/CACHEDEV1_DATA/Containers/openhab/conf
mkdir -p /share/CACHEDEV1_DATA/Containers/openhab/userdata
mkdir -p /share/CACHEDEV1_DATA/Containers/openhab/addons

Now that we have the folders we need to setup a user for openHAB and set the correct permissions:

# add openhab user that is not allowed to login:
$ useradd -r -s /sbin/nologin openhab

# set permissions on the openhab data folder
$ chown -R openhab:openhab /share/CACHEDEV1_DATA/Containers/openhab

# and check that it worked
$ ls -al /share/CACHEDEV1_DATA/Containers/openhab
drwxr-xr-x 5 openhab openhab        4096 2018-09-09 23:29 ./
drwxr-xr-x 2 openhab openhab        4096 2018-09-09 23:06 addons/
drwxr-xr-x 2 openhab openhab        4096 2018-09-09 23:06 conf/
drwxr-xr-x 2 openhab openhab        4096 2018-09-09 23:06 userdata/

Perfect! Now all we need is to launch the container.

Configuring the openHAB Container

Using docker we can launch new containers that are then visible in the QNAP Container Station. It is best practice to store the docker run command in a bash script so that you can keep track of how you configured the container. So create a new file in:

/share/CACHEDEV1_DATA/Containers/openhab/docker-run-openhab.sh

and then let’s start to script.

openHAB stores configuration, user data and addons into separate folders on the file system. We can use docker volumes to control where on the host system openHAB will store those folders. First, we want to make sure we have the correct and absolute references to the three data folders:

#!/bin/sh

DIR_CONF=$(readlink -f ./conf)
DIR_USERDATA=$(readlink -f ./userdata)
DIR_ADDONS=$(readlink -f ./addons)

if [ -z $DIR_CONF ]; then
        echo "Config directory does not exist!"
        exit 1
fi
if [ -z $DIR_USERDATA ]; then
        echo "Userdata directory does not exist!"
        exit 1
fi
if [ -z $DIR_ADDONS ]; then
        echo "Addons directory does not exist!"
        exit 1
fi

echo "Conf directory: $DIR_CONF"
echo "Userdata directory: $DIR_USERDATA"
echo "Addons directory: $DIR_ADDONS"

Now that we have the data locations stored in variables, we can easily start the openHAB Docker container by adding the following lines to the script:

docker run \
    --name openhab \
    --net=host \
    --tty \
    -v /etc/localtime:/etc/localtime:ro \
    -v /etc/timezone:/etc/timezone:ro \
    -v $DIR_CONF:/openhab/conf \
    -v $DIR_USERDATA:/openhab/userdata \
    -v $DIR_ADDONS:/openhab/addons \
    -d \
    -e USER_ID=$(id -u openhab) \
    -e GROUP_ID=$(id -g openhab) \
    -e OPENHAB_HTTP_PORT=8190 \
    -e OPENHAB_HTTPS_PORT=8191 \
    --publish 8190:8190 \
    --publish 8191:8191 \
    --restart=always \
    openhab/openhab:2.3.0-amd64-debian

Let’s look at some parts of the above to explain things a little. We want openHAB to store the data outside the Docker container in the folders we prepared above. To do that, we mount our data folders to the locations where openHAB writes it’s data to:

    -v $DIR_CONF:/openhab/conf \
    -v $DIR_USERDATA:/openhab/userdata \
    -v $DIR_ADDONS:/openhab/addons \

openHAB needs to know the user and group IDs of the openHAB user, which we dynamically pass to the container using environment variables.

    -e USER_ID=$(id -u openhab) \
    -e GROUP_ID=$(id -g openhab) \

The openHAB docker container then creates that exact user inside the container, so that folder permissions on the mounted data folders work as expected. Last, but not least, we want to make sure that the openHAB web application is properly exposed on the QNAP host. You need to find two unoccupied ports on your QNAP NAS (what about the 5th Mersenne prime?).

$ netstat -an | grep 8191
# no output means port 8191 is unoccupied

To keep things consistent, the environment variables configure openHAB to use our custom ports, which are then published to the QNAP host by Docker:

    -e OPENHAB_HTTP_PORT=8190 \
    -e OPENHAB_HTTPS_PORT=8191 \
    --publish 8190:8190 \
    --publish 8191:8191 \

To further configure the container see the openHAB2 inside a Docker container installation guide.

Launching openHAB on QNAP

Using our Docker launch script it is super easy to start openHAB:

# start openHAB
cd /share/CACHEDEV1_DATA/Containers/openhab
./docker-run-openhab.sh

# check status
docker ps

# check logs in case of errors
docker logs openhab

If the docker container is running successfully go to http://<QNAP_IP>:8191 and you should see your shiny new openHAB installation. Now proceed with the configuration as described in the official documentation.

Conclusion

openHAB is easy to install and configure on a QNAP NAS using Docker with the Container Station. It does not take up lot’s of system resources and is perfect to start out automating your home. If you have a QNAP NAS there is actually no reason not to start right away :-)

Comments

👋 I'd love to hear your opinion and experiences. Share your thoughts with a comment below!

comments powered by Disqus