Use Home Assistant to start and stop Docker Containers

So I’ve been using Home Assistant on my Synology NAS for quite some time. However, I’ve had trouble starting/stopping Home Assistant.

I wanted to be able to monitor the Docker containers running on my NAS, so I could start and stop them as needed, and also add them into alerts so if they go down, Home Assistant can tell me about it. (If there’s a critical problem, the --restart flag doesn’t bring them back up until the problem is resolved).

So, I created a simple NodeJS API which connects to the running Docker socket, and is designed to work with Home Assistant’s RESTful Switches and Sensors.

You can get the example YAML code for Home Assistant and the Docker run commands here: https://philhawthorne.com/ha-dockermon-use-home-assistant-to-monitor-start-or-stop-docker-containers/

12 Likes

I created an image which runs on a Pi3, in case someone wants to use this excellent addon on a pi:
https://hub.docker.com/r/tribunex/ha-dockermon-pi/

2 Likes

That’s awesome! Thanks

Thank you for the script and container. I have been using it for a while now and it works great.

1 Like

Is it possible to use this as an add-on ?

Unfortunately not yet. However if you’re using hass.io, I am not sure you should want to control HassIO containers with this, as it may cause some problems.

For hassio users, its not as clean, but you can monitor and start/stop the containers via a sensor and switch. The exception is the main home assistant container and the hassio supervisor container cannot be controlled in this way, however, there are existing means to stop/start those available via the UI. It also won’t work if you are running docker containers that are not addons.

Use restful sensors to get the status of the addon.

There are services exposed to create a switch to turn on/off the addons and the above sensor can be used to get the state of the addon.

You can also use the sensor to trigger notifications if a container stops.

You can then add them to your UI (left most panel on the screen shot below)

2 Likes

Ow, don’t think but can use it then…
I don’t want to restart ha itself , I have installed Portainer as add-on, in Portainer I created a custom container… So I can’t control that one? Is that what you are saying?

Portainer has an api that you could look into, you may be able to control things through that.

https://app.swaggerhub.com/apis/deviantony/Portainer/1.23.0/

1 Like

Ok, something to read for tomorrow :slight_smile:
Thnx in advance