This is the script I came up with to check on docker containers and notify healtchecks. See my video on healthchecks for more information.


#!/usr/bin/env python
import os
import requests


#function for checking on status
def checkstatus(container, healthchecksurl):
 stream = os.popen('docker ps | grep ' + container)
 output = stream.read()

 if  bool(output):
  requests.get(healthchecksurl, timeout=10)
 else:
  print(container + " is down!")


#pass docker service name, healthchecks ping url for each docker container to check
checkstatus("dozzle", "http://thinkcentre.lan:7055/ping/b4497407-99dd-41fc-b510-ccddfe4a0c37")
checkstatus("filebrowser", "http://thinkcentre.lan:7055/ping/0dbef0c4-b368-443d-b050-f39e4318cb86")
checkstatus("pihole", "http://thinkcentre.lan:7055/ping/deecd301-c2bc-4ca1-97f5-0c2d6b920f74")
checkstatus("syncthing", "http://thinkcentre.lan:7055/ping/682ce440-bf25-417f-b4f3-38307834b685")
checkstatus("portainer", "http://thinkcentre.lan:7055/ping/7f28d97a-7a76-4e1b-951f-039c65c9e634")
checkstatus("wallabag", "http://thinkcentre.lan:7055/ping/75f39414-e217-4a1d-a03a-bf289ae4aac6")

And for python3 it uses different libraries and a different request as specified on the healthchecks official website:


#!/usr/bin/env python3
import os
import socket
import urllib.request


#function for checking on status
def checkstatus(container, healthchecksurl):
 stream = os.popen('docker ps | grep ' + container)
 output = stream.read()

 if  bool(output):
  urllib.request.urlopen(healthchecksurl, timeout=10)

 else:
  print(container + " is down!")


#pass docker service name, healthchecks ping url for each docker container to check
checkstatus("paperless", "http://thinkcentre.lan:7055/ping/90fdd24c-ba0a-494b-8711-0661d1e14171")
checkstatus("airsonic", "http://thinkcentre.lan:7055/ping/3c899b70-3bbb-4fc8-814a-d3fb647b94c6")
checkstatus("smtptelegram", "http://thinkcentre.lan:7055/ping/017fbe01-9299-498f-a152-d0ff3b614b56")
checkstatus("deluge", "http://thinkcentre.lan:7055/ping/50126e53-ff1c-41d9-9e49-1f463ba6fd49")
checkstatus("sonarr", "http://thinkcentre.lan:7055/ping/d86999d0-9214-4afc-aaff-4f3b076e6ca0")
checkstatus("sabnzbd", "http://thinkcentre.lan:7055/ping/90c86ddd-4e9a-4f5c-a83c-ab28f63f545b")