Control Docker containers from Home Assistant using Monitor Docker

No comments
Disclosure: This post contains affiliate links. If you click through and make a purchase, I’ll earn a commission, at no additional cost to you. Read my full disclosure here.
Advertisement
An illustration of a dock with ships. Cranes are removing containers from the ships.

If you run Home Assistant Core in a Docker container alongside other containers, you might want to take a look at the Monitor Docker custom component. Monitor Docker allows you to monitor Docker and container statistics and turn containers on or off.

Advertisement

Before discovering Monitor Docker, I was already a user of HA Dockermon. However, Monitor Docker has some very useful additions and is easier to set up when compared to HA Dockermon. If you’re just starting and were considering HA Dockermon I urge you to take a look at Docker Monitor. There might even be a few reasons for current users of HA Dockermon to switch, too.

Table of Contents

Monitor Docker vs. HA Dockermon

Monitor Docker does just about everything HA Dockermon and has the following advantages:

Advertisement
  • HA Dockermon hasn’t been updated since September 7, 2019. Though it still works perfectly in its current state, I am always about cautious with apps that haven’t been updated for a long time.
  • With Monitor Docker, you can add all containers without having to configure each one.
  • You can monitor individual container variables as well as global Docker variables. This includes reports on how much memory is used, what state the container is in, and the uptime.

However, there is one downside to Monitor Docker I’ve found in my testing. Using HA Dockermon you can set up a shell command to restart Docker containers. Using that shell command, you can restart the Home Assistant Core Docker container itself. While you can stop the Home Assistant Core container using Monitor Docker, there isn’t a way of turning it on again. Please let me know in the comments if I’m missing something! Restarting Home Assistant Core from the Server Controls has never been reliable for me.

Why use Monitor Docker?

Using Monitor Docker, you can monitor all of your containers. It can easily tell you what the status of a certain container is and for how long it has been up and running. You could also set up scripts to restart containers because sometimes turning it off and on again really does solve the problem.

Advertisement

How to install Monitor Docker

Monitor Docker is best installed using HACS (Home Assistant Community Store). Using HACS, you can easily update the integration when a new release is uploaded. If you don’t or can’t use HACS, Monitor Docker can also be installed manually. Be warned that when you install Monitor Docker manually, you are responsible for updating it. It’s best to go with HACS whenever possible.

Installing Monitor Docker from HACS

Configuring the Home Assistant Docker container

When Home Assistant is used within a Docker container, the Docker daemon should be mounted as follows:

Advertisement
-v /var/run/docker.sock:/var/run/docker.sock

Or if you’re using Docker Compose mount the following volume:

    volumes:
      - /var/run/docker.sock:/var/run/docker.sock

If you are using Docker Compose (which I am) you need to execute the following command after editing your Docker Compose file:

docker-compose pull

docker-compose up -d --build homeassistant

Configuring Docker Monitor

Once you have everything up and running, you can configure Docker Monitor. This is done using YAML, as there is no UI configuration for this integration yet. I’ve added the following to my configuration.yaml:

Advertisement
monitor_docker:
  - name: Docker
    monitored_conditions:
      - version
      - containers_running
      - containers_total
      - containers_cpu_percentage
      - containers_memory
      - containers_memory_percentage
      - state
      - status
      - memory
      - memory_percentage

With this configuration, all of my Docker containers will be monitored. You can set Docker Monitor to only monitor certain containers using the containers variable. You can also rename containers using the rename variable. A list of all available variables and another configuration example is given on the Docker Monitor GitHub page.

Monitoring Docker containers from Home Assistant

Once you’ve got everything configured, you can restart Home Assistant. Your switches and sensor for the Docker containers should now available. If you’re using the default configuration, you will find them under sensor.docker_[container_name] and switch.docker_[container_name]. You can now add the switches and sensors to your Lovelace UI and use them in automations and scripts.

Switches for Docker containers in Home Assistant
Sensors for Docker containers in Home Assistant

Leave a comment

Share to...