Running Exec binding scripts in Docker using custom image

I recently migrated my install to use a docker container and I have a bunch of Python & Shell scripts running via the Exec binding. Since one of the limitation of the official openHAB docker image renders the Exec binding pretty much useless and I didn’t want to make each script an external service, I have created a custom docker image based of the openHAB official one to include the necessary scripting tools. I am using docker compose to build/start up the custom image.

This tutorial should be useful for anyone wanting to run Python scripts or other scripting languages from the container. You can easily add, update or remove the packages to match your needs. I would recommend to only keep the packages you will be using.

The configuration includes a requirements.txt file which should list all the Python modules necessary to run your scripts and be placed in the same directory than the Dockerfile configuration file. It also includes jq as I have some shell scripts parsing JSON output. Some of the settings in the docker compose configuration file will need to be customized to your needs.

Dockerfile

# openhab custom image
#
ARG VERSION
ARG ARCH
ARG BASE
FROM openhab/openhab:${VERSION}-${ARCH}-${BASE}

# Include openhab scripts folder in path
ENV PATH=${APPDIR}/conf/scripts:${PATH}

# Install additional packages
RUN apt-get update && \
    DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y \
    jq \
    python \
    python-pip \
    python-setuptools \
    python-wheel

# Install python required packages
COPY ./requirements.txt ${APPDIR}
RUN pip install -r ${APPDIR}/requirements.txt

docker-compose.yml

version: '2.2'

services:
  openhab:
    build:
      context: .
      args:
        VERSION: 2.4.0-snapshot
        ARCH: armhf
        BASE: debian
    image: jeshab/openhab:2.4.0-snapshot-armhf-debian
    container_name: openhab
    restart: always
    network_mode: host
    tty: true
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - /etc/timezone:/etc/timezone:ro
      - /opt/openhab/addons:/openhab/addons
      - /opt/openhab/conf:/openhab/conf
      - /opt/openhab/userdata:/openhab/userdata
    environment:
      OPENHAB_HTTP_PORT: 8080
      OPENHAB_HTTPS_PORT: 8443
      USER_ID: 9001
      GROUP_ID: 9001

Commands

To build/update the docker image: (pull option ensures that the openHAB base image is up to date)

docker-compose build --pull

To create/start the container:

docker-compose up --detach

To delete/stop the container:

docker-compose down
3 Likes

With the container init scripts support, a custom image is no longer needed for this use case. Here is the updated setup I am now using:

cont-init.d/50-exec-scripts

#!/usr/bin/with-contenv bash

# Include openhab scripts folder in path
export PATH=${OPENHAB_CONF}/scripts:${PATH}

# Install additional packages
apt-get -qq update && \
  DEBIAN_FRONTEND=noninteractive apt-get -qq install --no-install-recommends -y \
  curl \
  jq \
  python \
  python-pip \
  python-setuptools \
  python-wheel

# Install python required packages
pip install -q --no-cache-dir \
  <required_pkg_1> \
  <required_pkg_2> \
  ...

docker-compose.yml

version: '3'

services:
  openhab:
    image: openhab/openhab:snapshot
    container_name: openhab
    restart: always
    network_mode: host
    tty: true
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - /etc/timezone:/etc/timezone:ro
      - ./addons:/openhab/addons
      - ./conf:/openhab/conf
      - ./cont-init.d:/etc/cont-init.d
      - ./userdata:/openhab/userdata
    environment:
      OPENHAB_HTTP_PORT: 8080
      OPENHAB_HTTPS_PORT: 8443
      USER_ID: 9001
      GROUP_ID: 9001

Commands

To pull latest openHAB snapshot

docker-compose pull

To create/start the container:

docker-compose up --detach

To delete/stop the container:

docker-compose down
5 Likes

Excellent! I’m currently trying this out.

docker-compose is totally new for me but shouldn’t the docker-compose.yml file in the previous post start with version?

Good catch! I updated my original post.

Thanks!

I added procps and vim to packages.

procps (pgrep) is needed for the openHAB restore script to work.

vim (vi) is an editor I can’t do without :blush:

Things are working very well including openhab-scripters/openhab-helper-libraries. openHAB 2.5.0-SNAPSHOT Build #1629 was pulled down. The only thing that I’ve stumbled upon is that the paper UI stops to work sometimes. There’s a message about that “Jersey” or similar isn’t ready. That’s annoying.

Anyway, thank you very much for your work! :slight_smile:

That’s my favorite editor as well. Why would you want to edit file inside the container? Usually with a docker setup, you expose the volume with all your configuration and edit them from the host level.

Nice I should check this out.

Do you mean jetty error by any chance? This might happen when you first start a container after pulling a new snapshot image. Usually it stops then starts again. I think this something to do with building the initial cache. Anyway, I usually restart the container a couple times after a new pull and this isn’t related to the additional packages you are installing but how the current snapshot is setup.

No worries. I am just sharing my actual setup so not much of extra work from my end :smile:

The exact error message is “503 jersey is not ready yet” Initially I had a lot of them and Paper UI was “empty” (no items, no things etc …). However the error message disappeared and it all started to work by itself after some time time but it reappeared when I removed a binding in Paper UI. I restarted the OH container and I haven’t seen it since.

That helped me a lot!

Cheers!

You’re right about that. I had to edit a python file after downloading a python library. I’ll try to add it into a init.d script to be done automatically.

Check this thread. It looks like you might need to clear your browser cache to resolve this issue.