[SOLVED] Can not access to GPIO from OpenHab running in docker container

  • Platform information:
    • Hardware: Orange PI Zero 512MB
    • OS: Armbian 5.90 Orangepizero Debian buster next 4.19.57
    • Docker version: 18.09.7
    • Docker image: openhab/openhab:2.4.0-armhf-debian
    • openHAB version: 2.4.0
  • Issue of the topic: Can not access to GPIO from OpenHab running in docker container
  • Configurations:

gpio.items

Switch LED { gpio="pin:10 force:yes" }
Contact sensor "sensor [%s]" { gpio="pin:12 force:yes debounce:10 activelow:yes" }

gpio.sitemap

sitemap gpio label="GPIO"
{
 Frame label=""
 {
   Switch item=LED
   Text   item=sensor
 }
}
  • Services configuration gpio.cfg
sysfs=/sys
debounce=10
  • logs:
/opt/openhab/userdata/logs/openhab.log

2019-07-09 20:35:05.647 [INFO ] [panel.internal.HABPanelDashboardTile] - Started HABPanel at /habpanel
2019-07-09 20:35:23.259 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model ‘home.items’
2019-07-09 20:35:32.426 [INFO ] [thome.model.lsp.internal.ModelServer] - Started Language Server Protocol (LSP) service on port 5007
2019-07-09 20:35:33.548 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model ‘home.sitemap’
2019-07-09 20:35:36.318 [INFO ] [.dashboard.internal.DashboardService] - Started Dashboard at http://172.17.0.2:8080
2019-07-09 20:35:36.325 [INFO ] [.dashboard.internal.DashboardService] - Started Dashboard at https://172.17.0.2:8443
2019-07-09 20:35:39.545 [INFO ] [arthome.ui.paper.internal.PaperUIApp] - Started Paper UI at /paperui
2019-07-09 20:35:39.833 [INFO ] [ui.habmin.internal.servlet.HABminApp] - Started HABmin servlet at /habmin
2019-07-09 20:35:40.236 [ERROR] [ab.binding.gpio.internal.GPIOBinding] - Error occurred while creating backend object for item DoorSensor, exception: /sys/class/gpio/unexport
2019-07-09 20:35:40.242 [ERROR] [ab.binding.gpio.internal.GPIOBinding] - Error occurred while creating backend object for item RaspiLED, exception: /sys/class/gpio/unexport

  • Description:
# useradd -r -s /sbin/nologin openhab
# mkdir /opt/openhab
# mkdir /opt/openhab/conf
# mkdir /opt/openhab/userdata
# mkdir /opt/openhab/addons
# chown -R openhab:openhab /opt/openhab

groupadd gpio && usermod -aG gpio openhab

cat > /etc/udev/rules.d/99-gpio.rules << “EOF”
SUBSYSTEM==“gpio”, ACTION==“add”, RUN="/bin/sh -c ’
chown -R root:gpio /sys/class/gpio && chmod -R 770 /sys/class/gpio;
chown -R root:gpio /sys$DEVPATH && chmod -R 770 /sys$DEVPATH
'"
EOF

Installed docker to use: armbian-config

docker run
–name openhab
–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
-v /sys:/sys
-d
-e USER_ID=uid
-e GROUP_ID=gid
–restart=always
openhab/openhab:version-architecture-distribution

-v /sys:/sys - for access to gpio
symbol # - means that the command runs from the user root.
version - 2.4.0
architecture - armhf
distribution - debian

to get architecture execute:
# uname --m
my: armv7l

Architectures:
amd64 for most desktop computers (e.g. x64, x86-64, x86_64)
armhf for ARMv7 devices 32 Bit (e.g. most RaspberryPi 1/2/3)
arm64 for ARMv8 devices 64 Bit (not RaspberryPi 3)

uid and gid - execute:
id openhab

my:

docker run

docker run --name openhab
–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
-v /sys:/sys
-e USER_ID=999
-e GROUP_ID=998
–restart=always
-d
openhab/openhab:2.4.0-armhf-debian

Afrer Installed openHab I installed GPIO Binding:
http://ip:8080/paperui/index.html#/extensions?tab=binding
GPIO Binding
binding-gpio1 - 1.13.0

gpio didn’t work from openHAB

I tried SSH into a Container:
docker exec -it openhab /bin/bash

out gpio

echo "7" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio7/direction
echo "1" > /sys/class/gpio/gpio7/value

in gpio

echo "6" > /sys/class/gpio/export
echo "in" > /sys/class/gpio/gpio6/direction
cat /sys/class/gpio/gpio6/value

has access to GPIO

I had stopped docker

docker stop openhab

and installed openHab to use armbian-config.
OpenHab installed and has access to GPIO.

I have deployed docker container with openhab into raspbery pi 3B
OpenHab from docker into raspbery pi 3B has access to GPIO.

First of all thank you so much for read this question.
How to get access from openHab running in Docker Armbian access to GPIO?
Thank you very much for your responce.

References

  1. https://www.openhab.org/docs/installation/docker.html
  2. https://hub.docker.com/r/openhab/openhab/
  3. https://www.openhab.org/addons/bindings/gpio1/
  4. https://forum.armbian.com/topic/1851-rfc-make-armbian-more-iot-friendly/page/2/?tab=comments#comment-52949
  5. http://phase2.github.io/devtools/common-tasks/ssh-into-a-container/

Before Markus jumps in, why are you running OH in Docker on an SBC? It doesn’t really buy you all that much and it certainly comes with it’s drawbacks.

As you are hopefully aware, the whole point of a container is to isolate some service from the host and from everything else running on the same host. That isolation from the host means it can’t access the hardware, at least not by default.The container also contains just enough to work meaning bindings like Exec will be pretty useless to you as almost nothing you’d want to run with it (e.g. Python) does not exist in the container.

A quick search of Google shows several ways to give the container access to the GPIO pins.

Pay attention to

Which of these three approaches fits your needs will also depend on the libraries you are using when accessing GPIO. Not all libraries support all three of these options.

I’d highly recommend that if you need to access the GPIO, you should run it installed, not in a container. Running in a container is going to cause more problems than it solves on an SBC.

That is not* ssh into a container. That is opening a shell in the container’s context. There is a very big difference.

I’m not sure how or why it works on the RPi. It shouldn’t work there either without explicit giving the container access to them, which does not appear to be the case given your docker run command.

Thank you for your answer and for reading my question.

used for access to gpio:
-v /sys:/sys

Orange Pi Zero:
Installed openhab from armbian-config has access to GPIO;

Raspbery PI:
openhab installed from repository has access to GPIO
the docker container of openhab has access to GPIO

-v /sys:/sys - for access to gpio

References



sudo groupadd gpio && sudo usermod -aG gpio openhab

sudo cat > /etc/udev/rules.d/99-gpio.rules <<"EOF"
SUBSYSTEM=="gpio", ACTION=="add", RUN="/bin/sh -c '\
        chown -R root:gpio /sys/class/gpio && chmod -R 770 /sys/class/gpio; \
        chown -R root:gpio /sys$DEVPATH && chmod -R 770 /sys$DEVPATH \
        chown -R root:gpio /sys/devices/platform/gpio*.* \
'"
EOF

sudo  chmod +x /etc/udev/rules.d/99-gpio.rules
sudo /etc/udev/rules.d/99-gpio.rules

sudo id openhab
uid=998(openhab) gid=997(openhab) groups=997(openhab),1001(gpio)

sudo docker stop openhab && sudo docker rm openhab

sudo docker run --name openhab \
--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 \
-v /sys:/sys \
-e USER_ID=998 \
-e GROUP_ID=1001 \
--restart=always \
-d \
openhab/openhab:2.4.0-armhf-debian

-v /sys:/sys - for access to gpio
-e USER_ID - openhab user id
-e GROUP_ID - gpio group id

Thank you for corrected me.
I had accessed to GPIO, when I runned a shell in the container’s.
docker exec -it openhab /bin/bash

I have solved this question, answer above.