Home Assistant docker container as service

Hello,

user@vmware-box:~$ sudo systemctl start docker-homeassistant.service
user@vmware-box:~$ sudo docker container logs homeassistant
[s6-init] making user provided files available at /var/run/s6/etc...exited 0.
[s6-init] ensuring user provided files have correct perms...[cont-finish.d] executing container finish scripts...
[cont-finish.d] done.
[s6-finish] waiting for services.
exited 0.
[fix-attrs.d] applying ownership & permissions fixes...
[fix-attrs.d] done.
[cont-init.d] executing container initialization scripts...
[cont-init.d] udev.sh: executing... 
[07:21:52] INFO: Setup udev backend inside container
starting version 3.2.9
[07:21:52] INFO: Update udev information
[cont-init.d] udev.sh: exited 0.
[cont-init.d] done.
[services.d] starting services
s6-svscanctl: fatal: unable to control /var/run/s6/services: supervisor not listening
s6-svscanctl: fatal: unable to control /var/run/s6/services: supervisor not listening
[s6-finish] sending all processes the TERM signal.

but same time

user@vmware-box:~$ sudo sh -c 'truncate -s 0 /var/lib/docker/containers/*/*-json.log'
user@vmware-box:~$ sudo /usr/bin/docker start homeassistant
homeassistant
user@vmware-box:~$ sudo docker container logs homeassistant
[s6-init] making user provided files available at /var/run/s6/etc...exited 0.
[s6-init] ensuring user provided files have correct perms...exited 0.
[fix-attrs.d] applying ownership & permissions fixes...
[fix-attrs.d] done.
[cont-init.d] executing container initialization scripts...
[cont-init.d] udev.sh: executing... 
[07:33:50] INFO: Setup udev backend inside container
starting version 3.2.9
[07:33:50] INFO: Update udev information
[cont-init.d] udev.sh: exited 0.
[cont-init.d] done.
[services.d] starting services
[services.d] done.
2021-03-13 07:33:51 WARNING (Recorder) [homeassistant.components.recorder.util] The system could not validate that the sqlite3 database at //config/home-assistant_v2.db was shutdown cleanly.
2021-03-13 07:33:56 ERROR (MainThread) [homeassistant.components.mqtt] Failed to connect to MQTT server due to exception: [Errno -3] Try again

( Do not mind MQTT error - mqtt container not running )
Docker version is 20.10.5, home assistant container image is latest homeassistant/home-assistant:stable

Any ideas how to solve this…issue?

So, this is about Home Assistant Supervised or Home Assistant OS?

Home Assistant Container (Home Assistant Core on Docker) runs fine …

It’s a pure Home Assistant Core on Docker and do not runs fine when you try to start container as service via systemctl. That’s is the main problem here! For exapmle

docker run --init -d -p 8123:8123 --name homeassistant --restart=unless-stopped -v /etc/localtime:/etc/localtime:ro -v /dockerfs/home-assistant:/config --net vnet --ip 172.16.8.3 --hostname hass homeassistant/home-assistant:stable

work just fine, but when container is created without --restart=unless-stopped and service (systemctl) is created to start and stop hass container it fails as shown in first code window

But you must be running something else, otherwise you would not get this error:

Suoervisors is only included in Home Assistant Supervised or Home Assistant OS. A Home Assistant Core installation in docker will certainly not complain about supervisor.

1 Like

I do run Home Assistant Container (and others) on both - Raspbian and Ubuntu - and all containers having restart policy „allways“ are (re-) started as soon as the Docker service is restarted by systemd.

No need for dedicated container service units. Do not use „unless-stopped“ as the container of course is stopped during shutdown.

Unless-stopped will restart always, unless the user sends a stop command. The container will start automatically with this option when you reboot the host. I have this for lots of containers running fine for years.

Yes, i know. And I don’t run anything before after etc.
Just

docker run --init -d -p 8123:8123 --name homeassistant -v /etc/localtime:/etc/localtime:ro -v /dockerfs/home-assistant:/config --net vnet --ip 172.16.8.3 --hostname hass homeassistant/home-assistant:stable

then

sudo docker stop homeassistant
sudo systemctl start docker-homeassistant.service

and docker image fails to start. :grinning: Same time a command from service executed in prompt starts successfully Home Assistant container
docker-homeassistant.service is a simple file

user@vmware-box:~$ cat /etc/systemd/system/docker-homeassistant.service 
[Unit]
Description=Home Assistant container
Requires=docker.service
After=docker.service

[Service]
Restart=always
ExecStart=/usr/bin/docker start homeassistant
ExecStop=/usr/bin/docker stop -t 5 homeassistant

[Install]
WantedBy=default.target

I have seen the supervisor mentioned when a HA container install is starting

You don’t need a service file as pointed out by Christoph. Where did you read these instructions?

Anyway, I suggest running it in docker compose, easier to maintain in my opinion.

I did not read this kind of instructions. Actually I’m just curious why this happen. I’m already find a other way to control when home assistant container should start - in my case after ocfs is mounted…

No (or not quite):

Similar to always , except that when the container is stopped (manually or otherwise), it is not restarted even after Docker daemon restarts.

When I restart the docker daemon or the host, these containers are restarted, unless something stopped them before. A restart of the docker daemon or host does not stop the container, or at least it doesn’t do it in a way that it doesn’t restart automatically afterwards.

I second burningstone’s experience.

I use “unless-stopped” on every single container I run (over 25 now) and every one of them restarts as expected after a reboot of the host unless I manually stop the container first.

Okay, so we all agree dedicated systemd service units are not required.

1 Like

Yeah, I agree with that. :slightly_smiling_face:

I’ve never needed a system service to start a container.