I have a script that does a docker pull for each image I have (pull only, I only download new images via the script). I update the actual container when I have time to deal with any potential issues.
The script checks how many images I have of a given container; I send that information via MQTT to HA.
I have binary sensors set to be true if the number of images with that specific name is >1.
I posted the script somewhere here, but I couldn’t find it easily. Someone asked for it there and I posted the scripts and yaml.
I have the script run via a cron job every hour. A new image is only download if there is a update available.
Example binary sensor (add more as needed):
- platform: template
sensors:
dock_jellyfin_update:
friendly_name: "Jellyfin Update Available"
value_template: "{{ states('sensor.docker_image_jellyfin')|float > 1 }}"
cron job calls this (modify as needed):
echo Starting Docker Pulls
sudo docker pull homeassistant/home-assistant:stable && sudo docker pull portainer/portainer-ce && sudo docker pull linuxserver/grocy && sudo docker pull linuxserver/jellyfin && sudo docker pull linuxserver/swag && sudo docker pull synesthesiam/marytts:5.2 && sudo docker pull eclipse-mosquitto:1.6.13 && sudo docker pull zwavejs/zwavejs2mqtt && sudo docker pull briis/weatherflow2mqtt:dev
sudo python3 /home/glenn/DockerMQTT.py
The MQTT script:
# Revision Date 12/28/2020 2047
# Requires paho-mqtt for MQTT intergration; if not using MQTT, then ignore
# sudo python3 -m pip install paho-mqtt
import datetime, time
import paho.mqtt.client as mqtt # Allow publishing to MQTT Server
import subprocess
# MQTT Configuration:
MQTT_enable = True # Enable MQTT
Broker_IP = "10.74.1.224" # MQTT Broker IP
Broker_Port = "1883" # MQTT Broker Port
now = datetime.datetime.now()
ETime = str(now.strftime(" %H:%M:%S on %m-%d-%Y"))
''' Only comment out the Debug = True below to turn off Debug '''
Debug = False
Debug2 = False
#Debug = True # Uncomment and Comment out this line for Debug
if Debug is True: # Debug Level 2
Debug2 = False
#Debug2 = True # Uncomment and Comment out this line for Debug Level 2
#########
def MQTT(MQ_Topic,MQ_Payload):
# Send to the MQTT Broker
try:
if MQTT_enable is True:
if Debug2 is True:
print ("MQTT pre Client")
mqttc = mqtt.Client("python_pub")
if Debug2 is True:
print ("MQTT pre Connect")
mqttc.connect(Broker_IP)
if Debug2 is True:
print ("MQTT pre Publish")
mqttc.publish(MQ_Topic, MQ_Payload, retain = True)
if Debug is True:
print ("MQTT Publish Complete for " + MQ_Topic)
if MQTT_enable is False:
if Debug is True:
print ("MQTT is not enabled")
except:
# Prevent crashing if Broker is disconnected
if Debug is True:
print ("MQTT Failed to publish All")
#########
# Main program:
try:
if __name__ == '__main__':
#time.sleep(MainWait) # Pause between Docker Image DownloadsChecks and MQTT Updates
MQTT("docker/Time",ETime)
time.sleep (10)
Dock_MQTT = int(subprocess.check_output('docker image ls -a | wc -l', shell=True, text=True)) - 1
MQTT("docker/Image",Dock_MQTT)
time.sleep (10)
Dock_MQTT = int(subprocess.check_output('docker container ls | wc -l', shell=True, text=True)) - 1
MQTT("docker/ContRun",Dock_MQTT)
time.sleep (10)
Dock_MQTT = int(subprocess.check_output('docker ps -a | wc -l', shell=True, text=True)) - 1
MQTT("docker/ContLoad",Dock_MQTT)
time.sleep (10)
Dock_MQTT = int(subprocess.check_output('docker images -f dangling=true | wc -l', shell=True, text=True)) - 1
MQTT("docker/ImageUnused",Dock_MQTT)
time.sleep (10)
Dock_MQTT = int(subprocess.check_output('docker image ls -a | grep "portainer" | wc -l', shell=True, text=True))
MQTT("docker/Image/Portainer",Dock_MQTT)
time.sleep (10)
Dock_MQTT = int(subprocess.check_output('docker image ls -a | grep "home-assistant" | wc -l', shell=True, text=True))
MQTT("docker/Image/HA",Dock_MQTT)
time.sleep (10)
Dock_MQTT = int(subprocess.check_output('docker image ls -a | grep "swag" | wc -l', shell=True, text=True))
MQTT("docker/Image/Swag",Dock_MQTT)
time.sleep (10)
Dock_MQTT = int(subprocess.check_output('docker image ls -a | grep "marytts" | wc -l', shell=True, text=True))
MQTT("docker/Image/MaryTTS",Dock_MQTT)
time.sleep (10)
Dock_MQTT = int(subprocess.check_output('docker image ls -a | grep "grocy" | wc -l', shell=True, text=True))
MQTT("docker/Image/Grocy",Dock_MQTT)
time.sleep (10)
Dock_MQTT = int(subprocess.check_output('docker image ls -a | grep "jellyfin" | wc -l', shell=True, text=True))
MQTT("docker/Image/Jellyfin",Dock_MQTT)
time.sleep (10)
Dock_MQTT = int(subprocess.check_output('docker image ls -a | grep "mosquitto" | wc -l', shell=True, text=True))
MQTT("docker/Image/Mosquitto",Dock_MQTT)
time.sleep (10)
Dock_MQTT = int(subprocess.check_output('docker image ls -a | grep "zwavejs2mqtt" | wc -l', shell=True, text=True))
MQTT("docker/Image/Zwave2MQTT",Dock_MQTT)
time.sleep (10)
Dock_MQTT = int(subprocess.check_output('docker image ls -a | grep "weatherflow2mqtt" | wc -l', shell=True, text=True))
MQTT("docker/Image/WF2MQTT",Dock_MQTT)
time.sleep (10)
except KeyboardInterrupt:
print ('Interrupted by User at keyboard')
quit()
Edit I left in all the Debug tools to make it easier for others to find any issues. Follow notes in code to turn debug on.