Docker services-file not working

I’ve been neglecting my old OH2.5-installation for a few years since it was simply working. And I was busy with other hobbies. Now the laptop crashed and is gone. Bad timing since my central heating is schedueled through OH.
In the mean time OH3 is out. So I have to learn OH3 and get my old installation working again. I thought I better do this by using docker and install both. But that is new for me too.
I run debian 11 on a laptop, with only terminal. I have ssh-access from windows-laptop.
I have docker installed and using the run-command I can start an OH3-image and access from the windows laptop to http://myserver:8080

Now I like to start two instances: OH2.5.12, and OH3.3, by a services file and point them to ports 8082 and 8083.
The service-file ‘openhab2.service’ does not work for some reason. This is without the port-definition because I’m not sure how the port definition should be in the service-file (with -e in front? or not?)

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

[Service]
Restart=always
ExecStart=/usr/bin/docker run --name=%n --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 /opt/openhab/.java:/openhab/.java \
  --device=/dev/ttyUSB0 \
  --devices=/dev/ttyACM0 \
  -e USER_ID=998 \
  -e GROUP_ID=997 \
  -e CRYPTO_POLICY=unlimited \
openhab/openhab:2.5.12-debian ./start_debug.sh
ExecStop=/usr/bin/docker stop -t 2 %n ; /usr/bin/docker rm -f %n

[Install]
WantedBy=multi-user.target

The file openhab3.service is working (that is: there is a docker image started) but I cannot get to the installation web page. This is also without the port-difinition, because when I put the ports in, the file does not work anymore. Must have wrong syntax I think.

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

[Service]
Restart=always
ExecStart=/usr/bin/docker run --name=openhab3 --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 /opt/openhab/.java:/openhab/.java \
  --device=/dev/ttyUSB0 \
  --device=/dev/ttyACM0 \
  -e USER_ID=998 \
  -e GROUP_ID=997 \
  -e CRYPTO_POLICY=unlimited \
  openhab/openhab:3.3.0
ExecStop=/usr/bin/docker stop -t 2 %n ; /usr/bin/docker rm -f %n

[Install]
WantedBy=multi-user.target

For the ports I tried these lines:

OPENHAB_HTTP_PORT=8080
OPENHAB_HTTPS_PORT=8443

or:

-e OPENHAB_HTTP_PORT=8080 \
-e OPENHAB_HTTPS_PORT=8443 \

I also wonder why the docker image does not get the name openhab3 as indicated in the services file. Instead it only gets a image-tag:

 sudo docker images
REPOSITORY        TAG       IMAGE ID       CREATED        SIZE
openhab/openhab   3.3.0     46ec3c47adc2   5 months ago   717MB

I hope you can help.

That’s not how it typically works with Docker. Run the containers with the --restart always option and Docker itself will start the containers on a reboot. No need to mess with systemd and it’s probably best to not have two things managing your containers at the same time anyway.

The ExecStart will run the command after the = exactly as it’s typed. So what ever you do when running the container from the command line is exactly what you would put here. But again, why make things complicated? Just add --restart=always to the options to docker run and you are done.

Note that the official docs and the readme found under dockerhub provides lots of examples and explanations of all the parameters and why you should use them. These should be your first place to look as many of these questions are answered there.

As for the ports issue, there are more than just two ports opened and used by OH. You can only change two of them through the environment variables (that’s what the -e option does and the full list of supported environment variables at both of the links above). There’s the LSP port and an autodiscovery port and some bindings open their own port.

You will have to not use --net=host for one of these instances of OH and either:

  • use the -p option to port map to a different port (note this can break some things that depend on the port being at a well known location like the autodiscovery port)
  • use periods of processing where you only run one version of OH at a time until such time that the OH 3 instance can fully take over
  • run OH 3 on a different machine for now (could be a VM on the same laptop) until OH 3 is ready to take over.

Thank you Rich, for your explanation.
I did read both the doc and the readme (they are practically the same). They both give three options (the run command, compose file, or systemd) and I thought the latter would be the most automated. Theay also both point to the official docker-site which is where it’s difficult to know from where to return :wink: and easy to get lost. The problem I face as an eternal beginner, is that the examples given in the docs are often a bit minimalistic. The extra arguments to expand are then explained but for a beginner it is not always that clear how exactly they should fit in.
Thanks to you I now understand I can let Docker do the automated start on reboot using the run-command by use of --restart=always.
I’ll also just use the standard ports but run either OH2 or 3 at a time. Thanks for that tip too. I thought they could run along side.