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
procps (pgrep) is needed for the openHAB restore script to work.
vim (vi) is an editor I can’t do without
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.
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
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.
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.