Home-Assistant: Monitor & Control Docker Containers

Home-Assistant: Monitor & Control Docker Containers

Ok, so recently, I went through my Home-Assistant installation and decided to add a few integrations allowing me to interact with my Proxmox server a little closer.

Initial Packaging Impressions were good
Initial Packaging Impressions were good

For those of you who don’t know, Beelink were kind enough to send me a small home server for review towards the end of last year. They sent me their GK mini, which has been the perfect little unit to take over the server role in our home. You can read about the initial review and set up of the unit here. At present I am running the following on it:

Current Proxmox Stack
Current Proxmox Stack
  1. LXC running Docker (this maintains my full stack of apps)
  2. LXC running Tailscale (I like to keep this separate and basically leave it alone).
  3. LXC running Adguard
  4. LXC running Unifi Network Controller
  5. A VM running Home-Assistant Supervised (HA OS)
  6. A VM running Windows 11

Still not using Tailscale? Definitely check this out. Best remote access tool (VPN) by far,

The Windows 11 vm is used for testing and is rarely on. I installed it initially to see if I wanted to update my main workstation from Windows 10 and just left it there. Everything else is running all day every day. To remind you of the specs, I was sent the Beelink GK mini, Celeron J4125, 8gb of RAM, 256gb of Storage.

I wanted a way to be able to interact with the containers in my Docker LXC without needing to open a web browser and then navigate and login to portainer. Thankfully there’s a way to do this directly from Home-Assistant with the help of HA_dockermon.

Installation

For my container installations I am using portainer. You can read more about this here. I created and deployed the following stack:

version: “2.1”
services:
  docker_mon:
    image: philhawthorne/ha-dockermon
    container_name: ha_dockermon
    restart: always
    logging:
      driver: json-file
    network_mode: bridge
    volumes:
      – /etc/localtime:/etc/localtime:ro
      – /var/run/docker.sock:/var/run/docker.sock
      – /docker/appdata/dockermon/config:/config
    ports:
      – 8126:8126
    environment:
      – PUID=${PUID}
      – PGID=${PGID}
      – TZ=${TZ} 

In the above example, you can replace PUID, PGID and TZ with your own variables. I am just referencing strings that I have entered into a file located in /etc/environment.

Hit deploy and wait for the docker image to be pulled down and installed. If all goes well you should be met with a green image and dockermon running. For more information and to see the repository, you can check here.

Now that we have the container running on our server, let’s go over to Home-Assistant to configure further:

Home-Assistant Configuration

According to your needs, you can set up either sensors, switches or both. I opted for both. I want to be able to see what is running and how long they’ve been up for, as well as the ability to stop and start the containers at will.

Sensor Creation

Either under sensors in configuration.yaml or in sensors.yaml (if you’ve broken it out), create the following Rest sensor:

– platform: rest
  name: portainer
  json_attributes:
    – state
    – status
    – image
  resource: http://YourServerIP:8126/container/portainer
  value_template: “{{state_attr(‘sensor.portainer’,’status’)}} : {{state_attr(‘sensor.portainer’,’image’)}}”

This sensor is basically monitoring the status of the portainer container I am running on the server. The sensor returns the following information: State (running or not), Status (how long it’s been up for), and the image it’s using.

I wanted to add both the stauts and the image to be displayed on one line, which is exactly what the ‘value_template’ is doing. You can test the language in the Developer Tools / Template editor (see below).

Template Testing
Template Testing

Once you’re comfortable with the sensor, save the file, check the config and restart. If successful, you should have something resembling the below in Developer Tools / States.

Sensor Data in Action
Sensor Data in Action

Once you’ve got this far, it’s just a case of rinse and repeat for all the other containers that you wish to create sensors for.

Switch Creation

Ok, so now time for the switch. Go to switches.yaml and create the following switch:

– platform: rest
   resource: http://YourServerIP:8126/container/portainer
   name: portainer
   body_on: ‘{“state”: “start”}’
   body_off: ‘{“state”: “stop”}’
   is_on_template: ‘{{ value_json is not none and value_json.state == “running” }}’

Be mindful to name your containers accurately.

Save the file, check your config and restart the Home-Assistant server. Once it pops back online, you should be able to add a switch entity to a card to test it.

Testing the Switch Example
Testing the Switch Example
Switch Control of the Container
Switch Control of the Container

Assuming all went well, you should now be able to stop and start a container with the flick of a toggle. Again, rinse and repeat for each container you wish to be able to control. Be mindful about creating a switch for dockermon, because if you inadvertently switch it off, you’ll effectively take down the docker connection.

Displaying the Entities

Now the UI side of things is subjective. I wanted to somehow display the information in a clear and readable fashion. I’m not 100% happy with the look, but ultimately it’s a panel to control docker containers, it’s lack of sexiness isn’t an issue 🙂

Completed Layout
Completed Layout

I created a new tab, and opted to change the tab from masonry to sidebar.

Setting up the Tab
Setting up the Tab

I’ll show you the result as well as the raw code that you can copy in should you wish to duplicate it. WordPress is poor at allowing me to paste in raw yaml, so forgive the screenshot.

Raw Code for the Layout
Raw Code for the Layout

For the docker icon, just change the icon in the properties tag to mdi:docker. Now you can sit back and marvel at your efforts!

Now I’ve only dipped my toe into what can be possible with dockermon. Full credit to Phil Hawthorne for setting up the repo in Github and actually putting this all together. I am sure there will be other components that will make it easier to pull in the information, but for now I am happy to stop there. I can monitor and control the containers, and things are relatively simple and neatly laid out.

Check out Phil’s blog for more cool stuff.

 

If you have any good ideas or additions for Docker, let me know in the comments, or if you have anything cool that you’re working on, think about joining our facebook group to share your inspiration.

https://www.facebook.com/groups/386238285944105

 

If you’re considering a renovation and looking at the structured wiring side of things, or maybe you just want to support the blog, have a look below at my smarthome book, it’s available in all the usual places (including paperback)!

 

The Smarthome Book
The Smarthome Book

Comments

Leave a Reply