Can't start Openhab docker container with systemd

  • Platform information:
    • Hardware: Intel Core i5 / 16GB RAM/ 500GB SSD
    • OS: Ubuntu 18.04.3 LTS
    • Java Runtime Environment: N/A - docker container
    • openHAB container version: openhab/openhab:milestone (i.e. 2.5.0)

I’m having trouble getting systemd to manage my OpenHAB container. If I start the container using the command line below, it works fine.

docker run --name openhab \
	--net=home-lan \
	--ip=192.168.1.63 \
        -v /etc/localtime:/etc/localtime:ro \
        -v /etc/timezone:/etc/timezone:ro \
        -v openhab_addons:/openhab/addons \
        -v openhab_conf:/openhab/conf \
        -v openhab_userdata:/openhab/userdata \
	--device=/dev/ttyACM0 \
	-e CRYPTO_POLICY=unlimited \
        -e "EXTRA_JAVA_OPTS=-Duser.timezone=Australia/Sydney" \
        --restart=always \
	-d openhab/openhab:milestone

However, when I try to start the container using systemd and the openhab.service file below, it fails.

[Unit]
Description=openHAB
Requires=docker.service
Wants=influxdb.service
After=docker.service influxdb.service

[Service]
Restart=always
ExecStart=/usr/bin/docker run --name=%n \
  --net=home-lan \
  --ip=192.168.1.63 \
  -v /etc/localtime:/etc/localtime:ro \
  -v /etc/timezone:/etc/timezone:ro \
  -v openhab_addons:/openhab/addons \
  -v openhab_conf:/openhab/conf \
  -v openhab_userdata:/openhab/userdata \
  --device=/dev/ttyACM0 \
  -e CRYPTO_POLICY=unlimited \
  -e "EXTRA_JAVA_OPTS=-Duser.timezone=Australia/Sydney" \
  openhab/openhab:milestone
ExecStop=/usr/bin/docker stop -t 2 %n ; /usr/bin/docker rm -f %n

[Install]
WantedBy=multi-user.target

The error code doesn’t really give me a whole lot to go on.

jon@master:/etc/systemd/system$ sudo systemctl status openhab
● openhab.service - openHAB
   Loaded: loaded (/etc/systemd/system/openhab.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Sat 2020-01-18 10:54:43 AEDT; 8s ago
  Process: 7019 ExecStart=/usr/bin/docker run --name=openhab.service --net=home-lan --ip=192.168.1.63 -v /etc/localtime:/etc/localtime:ro -v /etc/timezone:/etc/timezone:ro -v openhab_addons:/open
 Main PID: 7019 (code=exited, status=125)

Jan 18 10:54:43 master systemd[1]: openhab.service: Service hold-off time over, scheduling restart.
Jan 18 10:54:43 master systemd[1]: openhab.service: Scheduled restart job, restart counter is at 5.
Jan 18 10:54:43 master systemd[1]: Stopped openHAB.
Jan 18 10:54:43 master systemd[1]: openhab.service: Start request repeated too quickly.
Jan 18 10:54:43 master systemd[1]: openhab.service: Failed with result 'exit-code'.
Jan 18 10:54:43 master systemd[1]: Failed to start openHAB.

Any help would be much appreciated.

Cheers,

Jon

Uuh, that’s a tricky one. And unrelated to OH so strictly speaking you shouldn’t post that here.
But I’ve hit it, too, when I tried using docker for automated testing of openHABian development
(… ping @ThomDietrich) .
Long story short: forget about it. It just doesn’t work. G**gle for systemd and PID 1 if you want to know the details.
After many frustrating attempts I found a solution: replace systemctl with this.

Thanks Markus. I’m reluctant to replace systemd, so I guess I’ll just rely on docker to keep the openhab container running for now.

It’s not a systemd replacement but just for systemctl and it works fine.

1 Like

Thanks again Markus. I’ll look into it.

I had the same issue but was able to see an additional error in /var/log/syslog

docker: Error response from daemon: Conflict. The container name “/openhab.service” is already in use by container “76d75cd2089d587361ac5e2ddc52ed1382454bb48421477e261140e64d041a0c”. You have to remove (or rename) that container to be able to reuse that name.

Adding this line to /etc/systems/system/openhab.service worked like a charm for me:
ExecStartPre=-/usr/bin/docker rm %n

After adding that you need to reload systemd:
sudo systemctl daemon-reload

And finally ask systemd to start OpenHAB:
sudo systemctl start openhab

Just a note of caution that you need to have followed the instructions to setup your openhab docker with external storage as this wipes out your container each time it re/starts.