Automatically restarting Open Balena after reboot

There is a thread on GitHub around the automatic restart behaviour of Open Balena (https://github.com/balena-io/open-balena/issues/83), but it seems for the executing the startup script (’/scripts/compose up’) an init system will be used. I was surprised not to see a systemd service script for this.

Here is what I am looking to use, would be good to clarify whether there is an alternative already provided somewhere, or any comments on the below:

[Unit]
Description=Start OpenBalena
After=docker.service
BindsTo=docker.service
ReloadPropagatedFrom=docker.service

[Service]
Type=simple
User=balena
ExecStart=/home/balena/open-balena/scripts/compose up
ExecStop=/home/balena/open-balena/scripts/compose stop
Restart=on-failure

[Install]
WantedBy=multi-user.target

Hello @maggie0002 OpenBalena is designed to be a flexible solution for users and as a result we do not tie ourselves to a particular init system. This allows users to choose a start-up method that works best for their needs. In this case you are welcome to either add restart: always to the docker compose file or use a systemd script as you wish. We also find that not restarting OpenBalena by default is very useful during development and debugging as the logs are preserved.

For anyone drawing on this, suggest including a timeout not included in the initial post:

[Unit]
Description=Start OpenBalena
After=docker.service
BindsTo=docker.service
ReloadPropagatedFrom=docker.service

[Service]
Type=simple
User=balena
ExecStart=/home/balena/open-balena/scripts/compose up
ExecStop=/home/balena/open-balena/scripts/compose stop
TimeoutStopSec=120
Restart=on-failure

[Install]
WantedBy=multi-user.target

Hello

In our environment we just add restart:always to the specification of services in docker-compose

You can render the open-balena docker-compose via
./scripts/compose config > docker-compose.yml’

then in this docker-compose add restart:always to every service, and then use
docker-compose up -d

From now it will be handled via docker itself

1 Like

Indeed, although I have tried to stick as close to the original OpenBalena design to avoid any potential issues during updates in the future (i.e. changes to the docker-compose files that require this to be redone without me noticing).

Adding in the GitHub thread for others reference: https://github.com/balena-io/open-balena/issues/83

1 Like

I have been visiting this again today, because my production implementation with the system file just seemed really clunky, and layering two init systems not really ok. Especially when it came to losing all the abilities of docker-compose to merge the logs.

Fortunately, docker to the rescue! I came across the multiple compose files docs: Share Compose configurations between files and projects | Docker Documentation

In short, flatten the docker compose file as before. Then add a docker-compose.override.yml into the root directory of open-balena with the following:

services:
  api:
    restart: unless-stopped

  cert-provider:
    restart: unless-stopped

  db:
    restart: unless-stopped

  haproxy:
    restart: unless-stopped

  redis:
    restart: unless-stopped

  registry:
    restart: unless-stopped

  s3:
    restart: unless-stopped

  vpn:
    restart: unless-stopped

Docker merges the two, adding in the restart policies when you run docker-compose up -d. And this way when an Open Balena update is released, you only have to flatten the docker file again and not need to manually add in all the restart policies (one exception being if Balena adds in more containers, but let’s call that unlikely).

1 Like