Automation script or node red example to start/stop a docker container


I’m looking for a way to start/stop a docker container based on a sensor state.

I have a Harmony Hub remote so my Home Assistant (supervised) instance knows when TV/Nvidia Shield activity is on/off. Based on that I would like to start/stop a docker container (Jellyfin to be precise, but could any thing else) to save some resources on my NAS (OMV)

Did any one do something similar, I guess I have to run my automation python script on the host machine as root (not inside the HA container) or find a way to do it using nodered.

I kinda do something similar, but I am not stopping or restarting containers (although easily implemented).
I run a cron job to run a python script every hour that polls my images and containers and publishes those results to MQTT brocker, then I have HA make binary sensors out of the MQTT data to indicate if updates to images are available. You could do something similar to restart a container.
You also have the option of using Portainer and utilizing the API to restart containers. I personally did not have success this route but the option is there.

Yeah that would be useful as well, if you have that on github as I have other docker containers that I need to monitor for updates (had to disable watchtower when moved HA from my Rpi to my NAS)

I will probably make a bash script (and run it a cron job) to get the sensor state using Home Assistant API, and based on that I can start/stop the docker image I want as the docker command must run as root in order to allow that.

I could also call the Portainer API to start/stop the docker I want with a HA automation based on the sensor state, but I think that’s available in the paid version of Portainer.

I did not post it to my GitHub since I didn’t think it would be useful to anyone else and it is a very simple python script. It is small enough I can post it here. I can’t post it until I get back home thou.

I use the Monitor Docker custom component to control my containers.

My Node-RED instance acts up when it loses connection to home assistant so I use this script to stop and restart the container when HA starts.

  alias: Restart Nodered
  - service: switch.turn_off
    data: {}
    entity_id: switch.docker_nodered
  - delay: 00:00:10
  - service: switch.turn_on
    data: {}
    entity_id: switch.docker_nodered
  mode: single
  icon: mdi:wrench