Monitor Docker containers from Home Assistant
Custom Monitor Docker component for Home Assistant
About
This repository contains the Monitor Docker component I developed for monitoring my Docker environment from Home-Assistant. It is inspired by the Sander Huisman Docker Monitor, where I switched mainly from threads to asyncio and added my own wishes/functionality. Feel free to use the component and report bugs if you find them. If you want to contribute, please report a bug or pull request, and I will reply as soon as possible.
Monitor Docker
The Monitor Docker allows you to monitor Docker and container statistics and turn on/off containers. It can connect to the Docker daemon locally or remotely. When Home Assistant is used within a Docker container, the Docker daemon should be mounted as follows -v /var/run/docker.sock:/var/run/docker.sock.
Docker run Example
docker run -d \ ... \ -v /var/run/docker.sock:/var/run/docker.sock \ homeassistant/home-assistant
docker-compose.yaml Example
services: hass: image: homeassistant/home-assistant ... volumes: - /var/run/docker.sock:/var/run/docker.sock ... NOTE: Making /var/run/docker.sock read-only has no effect because it is a socket (not a file).
Raspberry Pi (Raspbian)
Using a Raspberry Pi with Raspbian it could happen no memory is reported. In such case, the Docker API does not report it to Monitor Docker. Making the following changes normally fixes the problem:
- Open the file
/boot/cmdline.txt - Add the following to the end of the existing line
cgroupenable=cpuset cgroupmemory=1 cgroup_enable=memory - Reboot your Raspberry Pi
Ubuntu / Debian
Also on Ubuntu/Debian, no memory may be shown, the following changes could solve your problem:
- Open the file
/etc/default/grub - Modify the
GRUBCMDLINELINUXDEFAULT=""toGRUBCMDLINELINUXDEFAULT="quiet cgroup_enable=memory swapaccount=1" - Run
sudo update-grub - Reboot your Ubuntu/Debian
Installation
HACS - Recommended
- Have HACS installed, this will allow you to easily manage and track updates.
- Search for 'Monitor Docker'.
- Click Install below the found integration.
- Configure using the configuration instructions below.
- If applicable, add the volume
/var/run/docker.sockto your Home Assistant container. - Restart Home-Assistant.
Manual
- Copy directory
customcomponents/monitordockerto your<config dir>/custom_componentsdirectory. - Configure with the config below.
- If applicable, add the volume
/var/run/docker.sockto your Home Assistant container. - Restart Home-Assistant.
Configuration
To use the monitor_docker in your installation, add the following to your configuration.yaml file:
# Example configuration.yaml entry
monitor_docker:
- name: Docker
containers:
- appdaemon
- db-dsmr
- db-hass
- deconz
- dsmr
- hass
- influxdb
- mosquitto
- nodered
- unifi
rename:
appdaemon: AppDaemon
db-dsmr: "Database DSMR-Reader"
db-hass: Database Home Assistant
deconz: DeCONZ
dsmr: "DSMR-Reader"
hass: Home Assistant
influxdb: InfluxDB
mosquitto: Mosquitto
nodered: "Node-RED"
unifi: UniFi
monitored_conditions:
- version
- containers_running
- containers_total
- state
- status
- memory
Important NOTE: The rename functionality works with regular expression. If you got containers with roughly the same name, it could match the wrong one. Examples:
appdaemon: AppDaemon - Will match anything with "appdaemon"
^appdaemon: AppDaemon - Will match if it starts with "appdaemon"
^appdaemon$: AppDaemon - Only match if it exactly matches "appdaemon", thus "appdaemon-2" will not match
Configuration variables
| Parameter | Type | Description | | --------------------------- | -------------------------- | --------------------------------------------------------------------- | | name | string (Required) | Client name of Docker daemon. Defaults to Docker. | | url | string (Optional) | Host URL of Docker daemon. Defaults to unix://var/run/docker.sock. Remote Docker daemon via TCP socket is also supported, use e.g. http://ip:2375. Do NOT add a slash add the end, this will invalidate the URL. For TLS support see the Q&A section. SSH is not supported. | | scaninterval | timeperiod (Optional) | Update interval. Defaults to 10 seconds. | | retry | time_period (Optional) | Retry interval when a TCP error is detected. Defaults to 60 seconds. | | certpath | string (Optional) | If a TCP socket is used, you can define your Docker certificate path, forcing Monitor Docker to enable TLS. The filenames must be ca.pem, cert.pem and key.pem| | containers | list (Optional) | Array of containers to monitor. Defaults to all containers. | | containers_exclude | list (Optional) | Array of containers to be excluded from monitoring, when all containers are included. | | monitored_conditions | list (Optional) | Array of conditions to be monitored. Defaults to all conditions. | | rename | dictionary (Optional) | Dictionary of containers to rename. Renaming is done on the name in HA Lovelace, not the entity name (see rename_entity). Default no renaming. | | rename_entity | boolean (Optional) | If rename is enabled, it changes the name in HA Lovelace, not the entity name. Enable this setting to also rename the entity name (Default: False) | | sensorname | string (Optional) | Sensor string to format the name used in Home Assistant. Defaults to {name} {sensor}, where {name} is the container name and {sensor} is e.g. Memory, Status, Network speed Up | | switchname | string (Optional) | Switch string to format the name used in Home Assistant. Defaults to {name}, where {name} is the container name. | | switchenabled | boolean / list (Optional) | Enable/Disable the switch entity for containers (Default: True Enabled switch for all containers, False: Disabled switch for all containers). Or specify a list of containers for which to enable switch entities. | | buttonenabled | boolean (Optional) | Enable/Disable the button entity for containers (Default: False Enabled button for all containers, False: Disabled button for all containers). Or specify a list of containers for which to enable button entities. | | precision_cpu | integer (Optional) | Precision of CPU usage percentage (Default: 2) | | precisionmemorymb | integer (Optional) | Precision of memory usage in MB (Default: 2) | | precisionmemorypercentage | integer (Optional) | Precision of memory usage in percentage (Default: 2) | | precisionnetworkkb | integer (Optional) | Precision of network bandwidth in kB (Default: 2) | | precisionnetworkmb | integer (Optional) | Precision of network usage in MB (Default: 2) |
| Monitored Conditions | Description | Unit | | --------------------------------- | ------------------------------- | ----- | | version | Docker version | - | | containers_total | Total number of containers | - | | containers_running | Number of running containers | - | | containers_paused | Number of paused containers | - | | containers_stopped | Number of stopped containers | - | | containerscpupercentage | CPU Usage. The CPU usage depends on the number of CPU cores, e.g. if you have 8 cores, this value can have a maximum of 800% | % | | containers1cpupercentage | CPU Usage, between 0-100% | % | | containers_memory | Memory usage | MB | | containersmemorypercentage | Memory usage | % | | images | Number of images | - | | state | Container state. This is created, restarting, running, removing, paused, exited or dead | - | | status | Container status. E.g. Up 13 days, Up 5 hours, Exited (0) 11 hours ago | - | | health | Container health if available | - | | uptime | Container start time | - | | image | Container image | - | | cpu_percentage | CPU usage. The CPU usage depends on the number of CPU cores, e.g. if you have 8 cores, this value can have a maximum of 800% | % | | 1cpu_percentage | CPU Usage, between 0-100% | % | | memory | Memory usage | MB | | memory_percentage | Memory usage | % | | networkspeedup | Network speed upstream. Not available when using network mode is 'host' | kB/s | | networkspeeddown | Network speed downstream. Not available when using network mode is 'host' | kB/s | | networktotalup | Network total upstream. Not available when using network mode is 'host' | MB | | networktotaldown | Network total downstream. Not available when using network mode is 'host' | MB | | allinone | This is a special condition and when used, it will only create 1 sensor per container with all the monitored conditions as attribute value. NOTE: If you use this sensor, all other sensors are NOT created, just 1 sensor |- |
Debugging
It is possible to debug the Monitor Docker component, this can be done by adding the following lines to the configuration.yaml file:
logger:
logs:
customcomponents.monitordocker: debug
Q&A
Here are some possible questions/errors with their answers.- Question: Does this integration work with the HASS or supervisord installers?
docker-socket-proxy. Start this docker with the following docker-compose code. It exposes the socket over TCP and monitor_docker can listen to it.
# Proxy the Docker sock so that we can pick up stats for HomeAssistant
services:
dockerproxy:
image: tecnativa/docker-socket-proxy
container_name: dockerproxy
privileged: true
volumes:
- /var/run/docker.sock:/var/run/docker.sock
ports:
- 2375:2375
environment:
- BUILD=1
- COMMIT=1
- CONFIGS=1
- CONTAINERS=1
- DISTRIBUTION=1
- EXEC=1
- IMAGES=1
- INFO=1
- NETWORKS=1
- NODES=1
- PLUGINS=1
- SERVICES=1
- SESSSION=1
- SWARM=1
- POST=1
Add the following to your configuration.yaml:
monitor_docker:
- name: Docker
url: http://<host_ip>:2375
- Error:
Missing valid dockerhost.Either DOCKERHOST or local sockets are not available.
/var/run/docker.sock
- Error:
aiodocker.exceptions.DockerError: DockerError(900, "Cannot connect to Docker Engine via http://10.0.0.1:2376...).
docker -H tcp://10.0.0.1:2376 ps if it works (replace 10.0.0.1 with your IP address). Also you can consult the following URL for more help: https://docs.docker.com/config/daemon/remote-access/
- Question: Is Docker TCP socket via TLS supported?
tcp://ip:2376 and the per instance configurationg certpath need to be set
The following is a docker-compose example of how to set the environment variables and the volume with the certificates:
services:
hass:
image: homeassistant/home-assistant
...
volumes:
- /var/run/docker.sock:/var/run/docker.sock
# The files need to be named "ca.pem", "cert.pem" and "key.pem"
- ./certs:/certs
...
- Question: Can this integration monitor 2 or more Docker instances?
# Example configuration.yaml entry
monitor_docker:
- name: Docker
containers:
...
- name: RemoteDocker
url: tcp://10.0.0.1:2376
containers:
...
- Question: Can create, delete or re-create a container be implemented in the integration?
- Question: Can you add more security to a switch?
- Question: All the reported memory values are 0 (zero), can this be fixed in the integration?
- Question: Is it possible to monitor HASS.IO?
- Question: I get a permission denied error?
$ sudo usermod -a -G docker <user>
$ sudo reboot
- Question: Can you add the feature to check if there are updates to images in e.g. hub.docker.com?
- Question: Is Docker via SSH supported?
socat. The following URL may help you: https://serverfault.com/questions/127794/forward-local-port-or-socket-file-to-remote-socket-file/362833#362833
- Question: Can the sensors have unique entity identifiers? This is useful for renaming it in the HA GUI
Credits
License
Apache License 2.0