HA-Dockermon – Use Home Assistant to monitor, start or stop Docker Containers

Being a developer, I love the flexibility and power Docker brings to Home Automation. Docker makes it easy to run multiple services which can play a big role in your smart home.

It’s no surprise why HASS.IO, the all-in-one solution is also powered by Docker. Using the power of isolated Docker containers, HASS.IO has the ability to keep Home Assistant up-to-date without the need for the command line. It also powers the concept of Home Assistant Add-ons, which are isolated Docker containers, adding their own additional functionality.

Right now, HASS.IO can only be installed on some Linux based systems. My Synology NAS, which I run Docker from is based on Linux but isn’t compatible with HASS.IO. For the time being, I’m still using Home Assistant via the official Home Assistant DockerHub image. This gives me an always-on network server which is more powerful than a Raspberry Pi and allows me to run multiple applications.

Having such power means I end up running a few Docker containers. These include InfluxDB and Grafana, and MySQL for my history and recorder components. You can also take it further with Docker containers for almost anything else: Transmission Download clients, the Plex media server, ad-blocking software PiHole, and of course even more Home Automation tools like HomeBridge for connecting Home Assistant to your iOS devices.

With so many potential Docker containers, they create more moving parts in your smart home. The more moving parts you have, the more things must be up and operational for your smart home to function. If one of those containers goes down, it would be nice for Home Assistant to be able to bring them back up (or at least notify you if it can’t).

 

Introducing HA-Dockermon

Home Assistant has great sensor and switch components that can easily be adapted for wide use-cases. Some of those include the RESTful Sensor and RESTful Switch. These components make it easy for Home Assistant to read data from a REST API, and display or utilise them in Home Assistant either as a switch or sensor.

HA-Dockermon is a NodeJS service which can be run from its own Docker container (or NodeJS if you’re that way inclined). It exposes a RESTful API which Home Assistant can call to see the status of Docker containers on your system. As at the first release, here’s what HA-Dockermon can do:

  • Get the status of a container (running, stopped).
  • Start or stop a container by issuing a POST  request.
  • Restart a container by making a GET  request to a URL for the container.

 

The Latest In Your Inbox

Enter your email address below to receive my latest blog posts and videos about Home Automation in your Inbox

 

Installing with Docker

The easiest way to get going with HA-Dockermon is to run its own Docker container. The trick is to mount the Docker connection socket using a volume mount. This is critical, as it allows the Docker container to access the host Docker instance, and perform actions against the Docker API directly.

The following run command will spin up a Docker container with HA-Dockermon running on port 8126.

The second volume mount is to mount a config directory. You can use this to add a configuration.yaml file to define additional settings like a username and password which must be used to call the service. This mount is optional, and default settings will be used if you don’t set it. See the GitHub repo for the default settings and an example file.

 

Adding to Home Assistant

Because HA-Dockermon is designed to work with RESTful Switches and Sensors, adding these to Home Assistant just requires a few lines of YAML in your configuration file.

Once you’ve added those switches, you can use customise to add some icons, and then use groups to add them to your Home Assistant front-end.

Here’s how that card now looks in Home Assistant

Tapping those switches on or off will send their respective docker start  and docker stop  commands to the containers.

 

Restarting Home Assistant

One problem I have running Home Assistant in a Docker container is that the stop and restart Home Assistant buttons don’t work. For some reason, Home Assistant is unable to stop the container, and the container just hangs with Home Assistant down and unusable. This means each time I make a change to my config that needs a Home Assistant restart, I need to SSH into my Synology NAS to execute docker restart home-assistant .

HA-Dockermon includes a /restart  endpoint which can be added after the container name in the URL. This allows you to simply make a call to a URL to restart a container, like Home Assistant.

Here’s a shell command and script you can add to your Home Assistant which will make a call to HA-Dockermon to restart Home Assistant.

We can then add that to a Home Assistant group, and we’ll see a nice Activate button which will restart the entire Home Assistant Docker container.

 

Adding Alerts

Now that we have those switches inside Home Assistant, we can use them like any other normal switch. For example, we might want to send an alert if the MySQL or MQTT containers crashed and haven’t been up for 5 minutes.

 

Automating When Containers Run/Stop

Another thing we can now do is automate when Docker containers are run or stopped. Here’s an example automation that would shut down a Plex Media Server container when the Harmony Remote turns off the TV after midnight, or at midnight if the TV is already turned off.

 

 

Other Ideas

So far, this has been a great addition to my Home Assistant setup. I now have more visibility into the moving parts of my smart home from within Home Assistant. There’s an extra layer of security knowing that Home Assistant has the ability to inform me that something isn’t right before I or someone else in the house notice.

My use case was pretty basic. To give me the ability to send alerts when something is wrong, and to restart Home Assistant remotely. Of course, you could use this to go a few steps further (and I hope you do). Here are some ideas you could explore with HA-Dockermon.

  • Combine with the Certificate Expiry sensor to have Certbot automatically regenerate an SSL certificate, and then reboot the Home Assistant container when it’s done. This will ensure your Home Assistant always has a valid SSL certificate, and you don’t need to do anything.
  • Voice control Docker containers. Ask Alexa to restart Home Assistant instead of finding the reset button in the UI. Or tell Google Assistant to turn off Transmission because you’re trying to stream Netflix.
  • Detect when your host machine is using too many resources (RAM, CPU etc) and shut down any non-critical Docker containers to improve performance.

 

Eventually, I’d like to add more features to HA-Dockermon such as pulling/updating images, renaming containers etc. You can star or follow the GitHub repo for the latest updates. If you’ve got another idea on how to use HA-Dockermon, I’d love to hear it too!

Buy me a hot chocolateBuy me a hot chocolate
Recommended Posts
  • ASA

    That is really quite awesome. And its made me realize how many containers I have running that I use once in a blue moon (calibre, Unifi, …) I can now turn them on as I need them from HA 🙂 Thanks!

  • Jokerigno

    Hi!

    It’s still me!

    I’ve tried to setup this metadocker in my home assistant.

    But it does’nt work.

    This is the log I’ve found:

    Unable to connect to Docker socket at /var/run/docker.sock. Is Docker running?
    npm ERR! code ELIFECYCLE
    npm ERR! errno 1
    npm ERR! [email protected] start: node index.js
    npm ERR! Exit status 1
    npm ERR!
    npm ERR! Failed at the [email protected] start script.
    npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

    I tried also to run it as root but nothing change.

    Any hint?

    • Looks like your docker socket might be in a different location. Can you post your docker run command?

      • Jokerigno

        This is what I ran:

        docker run -d
        –name=HomeAssistantDockermon –restart=always
        -v /var/run/docker.sock:/var/run/docker.sock
        -p 8126:8126
        philhawthorne/ha-dockermon

        I checked into my nas. In var/run folder a docker.sock file exist with root as owner.

    • PATER Semper Incertus

      Same error here. I went on and ssh-ed into my Synology, then chmod the sh*t out of /var/run/docker.sock, to 777 … the container ran fine after, even altered its configuration from Synology DSM Docker GUI, then restarted from there … all fine. Hope it helps.

  • Love this. Great implementations, please keep them coming.

    I have a somewhat unrelated question that I hope you can help with. I need to access the usr folder of my Home Assistant container image hosted on a Synology NAS at volume1dockerhass-dev. Where is this usr folder normally located and how do I access it with SSH?

    • So just to confirm, you want to access the folder at /volume1/docker/hass-dev? You should be able to use a program like WinSCP to login with your SSH credentials, and then view the contents

      • Hey Phil, nope. I was told to change settings in this file: /usr/local/lib/python3.6/site-packages/pydaikin/appliance.py
        These changes are needed to get my AC unit fixed, but i have no idea where to access this for a container. I SSH’ed into the DSM usr folder, but python3.6 is not even there… Thanks for your trouble

        • Ah right, yes that directory isn’t mounted by default. You can create a new docker container and mount the dir, however I would advise against this.

          When I need to change those files, here’s what I do.

          From the host machine, execute

          Where home-assistant is the name of your running docker container.

          This will give you access to the containers shell. Now, you can

          Now in your Home Assistant config directory in the NAS you’ll see appliance.py. Make your changes to the file and save it. Then, copy back into the /usr folder by doing the reverse to the command above

          Then exit out of the shell, which will bring you back into the SSH of the NAS, then do a docker restart of the container so your new file changes are loaded.

  • By the way Phil,
    My understanding is that Hass-io cannot currently run as a Synology docker container. I’m interested in using a similar implementation to the Let’s Encrypt!/DuckDNS plug-in for Hass-io. This is needed to use things like Google Assistant that require SSL external access. Have you toyed with this at all?

    • Yeah that’s right. HASS.IO currently isn’t compatible with Synology.

      I have looked into the DuckDNS side of things. I’m surprised Home Assistant doesn’t have a component to do that to be honest.

      I currently don’t use SSL, but might be a good idea for another post once I get it working. There’s a docker image you can use which will generate, or renew a SSL certificate from Let’s Encrypt (called Certbot). I didn’t get very far into automating it though.

      You would essentially create a scheduled task in Synology, to renew the certificates with the certbot container, then restart the Home Assistant container so that it loads the new certificates

      • Ced

        Cheers for that Phil. I’ll give it a try!

  • Ced

    Mmh, I’m having no luck running this one. Here is the code I used (adapted to DSM 6.1):
    ”’docker run -d

    –name=ha-dockermon

    —-mount type=bind,source=/run/docker.sock,target=/run/docker.sock

    -—mount type=bind,source=/volume1/docker/dockermon/config,target=/config

    -p 8126:8126

    philhawthorne/ha-dockermon”’
    The error message I get is
    docker: invalid reference format.
    See ‘docker run –help’.

    Still no luck using -v either…
    Running out of ideas!

    • So strange.

      I’m not sure if it is a copy/paste issue, but I noticed there are two types of dashes in front of the mount there. There’s a long dash and a small dash. Not sure if that might be causing the issues?

      • Ced

        Oh, yeah! I see what you mean… I shall give it another try as soon as I get home. I really see a lot of value to this and hope to make it work!

        • Ced

          Yup, it was indeed a dash issue. Good eyesight! Thanks again Phil…

  • Bernie Blais

    Hi Phil,
    I have been trying to install this component on my Raspberry Pi 3 with no luck, it seems like it never fully starts.
    I would really like to make this work on my side, this is a great addition in my opinion. My preferred method would be through a docker-compose file entry.
    I’m really new to this docker technology, so do you think that I am doing something wrong on my side? Is there a way that I can adapt this somehow to deploy on a Pi? Thanks.

    • Ah yes, I don’t think the base image I’m using runs on Raspberry Pi.

      Lucky though someone else created a fork of mine to create a Raspberry Pi version. See https://hub.docker.com/r/tribunex/ha-dockermon-pi/

      • Bernie Blais

        Thanks Phil, perfect! I just installed it with docker-compose and a file entry similar to yours.
        Thank you for your contribution, much appreciated.

  • Thanks a lot for this, I’ve just got it up and running.

    I noticed if I put the URL of one of my containers in a browser is shows some details
    {“state”:”running”,”status”:”Up 15 hours”,”image”:”linuxserver/transmission:latest”}

    Is there a way for me to show the status (running time) in Home Assistant’s frontend?

    • Hey Jono,

      Glad you’re finding it useful!

      You sure can. This was one of the reasons I wanted to include that, incase someone like you wanted to use that info 🙂

      You can use a RESTfulSensor. https://www.home-assistant.io/components/sensor.rest/

      Be sure to use a GET request (don’t use a POST request), and then use the value template to extract the information you want out. This will add a sensor to Home Assistant you can use in your front end.

      Cheers

  • Kyle Klicker

    This is frickin awesome! I have a fire tv server that kept disconnecting from the stick each time I turned it off. I was manually starting the server each time I turned on the fire stick. Now I put an automation in Home Assistant that detects the Harmony Hub activity and turns it on and off accordingly with the remote. This really opens up my mind to a whole new level of automations. Nice work!

  • Strex

    Hey @philhawthorne:disqus Great work on this!
    Just wondering, is there any way of pulling data from multiple docker instances?
    (and yes, I have 2 synology’s, both running their own docker stacks)

    • Thanks @xstrex:disqus

      Yeah of course. For each Docker instance, just run another HA-Dockermon on that instance.

      So, Synology 1 on IP 192.168.1.2 will run HA-Dockermon, and Synology 2 on IP 192.168.1.3 will also run its own HA-Dockermon.

      Then in your Home Assistant, just make the REST calls to the IP address of the right Synology where the container exists

  • Reggie Suplido

    Thanks for this! Finally I can easily restart node-red correctly on every HA restart.

    • Fletcher Kelly

      Did you get this done?
      NodeRed reboot arter every HA restart – yes please 🙂

      • Reggie Suplido

        The node-RED HA plugin has come a long way and has improved ever since. There’s no need to restart it when HA restarts.

  • Matthew David Krivanek

    Hi @philhawthorne:disqus Many thanks for creating this. Following your instructions, I’m able to stop and start all of my dockers except for Home Assistant. I see the section you have detailing how to do specifically that but when I click the “Execute” button, I see a push notification–“Turned on Restart Home Assistant”, but it doesn’t actually restart. Any idea what might be wrong?

    • Interesting. Can you post your yaml for the script?

      • Matthew David Krivanek

        In my scripts.yaml, I have:

        restart_hass:
        alias: Restart Home Assistant
        sequence:
        - service: shell_command.restart_hass

        • Matthew David Krivanek

          In configuration.yaml

          shell_command:
          restart_hass: 'curl http://127.0.0.1:8126/container/home-assistant/restart'

          In groups.yaml

          system:
          name: System
          entities:
          - script.restart_hass

          Strangely, if I remove the script from my scripts file and notate it as you do in your article, the System group on my frontend disappears.

          • Hmmmm strange. That all looks ok to me.

            Just to confirm, the container name of your container is set to home-assistant?

            Also, if you go to xxx.xxx.xxx:8126/container/home-assistant/restart in a browser, does it restart your home Assistant instance?

          • Matthew David Krivanek

            Ah! Found the issue. And it’s a silly one at that….my docker for HA is named homeassistant (without the dash). Updated to reflect the correct path and it works!!! Thanks for the help and for creating HA-Dockermon.

          • Michael McGuinness

            Hi Phil,

            Having a similar issue to Matthew (My on off switch works fine and the API call works as well, http://mynasaddress:8126/container/home-assistant/restart). However I have reviewed all the code Matthew posted and mine looks correct. I did not get from your guide to add the stuff into the scripts.yaml (it says configuration.yaml twice)? But done that now.

            I still can’t get it to the system group to show (with the restart button). I can see that group.system is in my list of Unused entities but has a status of unknown. Could I get some guidance please?

            Here is my config.

            configuration.yaml

            #Docker Manager
            #home-assistant docker
            switch:
            - platform: rest
            resource: http://127.0.0.1:8126/container/home-assistant
            name: home-assistant
            body_on: '{"state": "start"}'
            body_off: '{"state": "stop"}'
            is_on_template: '{{ value_json is not none and value_json.state == "running" }}'

            shell_command:
            restart_hass: 'curl http://127.0.0.1:8126/container/home-assistant/restart'

            scripts.yaml

            script:
            restart_hass:
            alias: Restart Home Assistant
            sequence:
            - service: shell_command.restart_hass

            groups.yaml

            system:
            name: System
            entities:
            - script.restart_hass

            Thanks heaps

          • Interesting, nothing looks to be wrong there.

            If you go to /container/home-assistant/restart in your browser, does it work?

          • Michael McGuinness

            Sure does, I use that every time I make changes to my config. Just using the NAS mount on my mac to access the files.

          • Ok interesting. Could be a network issue. Can you try use the IP address of your host, instead of 127.0.0.1?

          • Michael McGuinness

            @philhawthorne:disqus Thanks for your speedy reply. I fixed it. I just went back and reviewed what code Matthew had, in my Scripts file I had “script:” on the top line, once I removed that it works as expected. Awesome. What got me looking at this was in the notification area it said there was an error in my scripts file. So I knew to look there, will keep that in mind next time I an issue 🙂

  • Nasked

    Hi:
    After configure your dockermon container, i writted your containeralert.yaml with code:

    alias: Container_offline
    initial_state: 'on'
    trigger:
    - platform: state
    entity_id: switch.mqtt, switch.portainer
    to: 'off'
    for:
    minutes: 5
    condition:
    condition: and
    conditions:
    # Only send this once per hour
    - condition: template
    value_template: >
    {% if states.automation.container_offline.last_triggered is not none %}
    {% if as_timestamp(now()) | int - as_timestamp(states.automation.container_offline.attributes.last_triggered) | int > 3600 %} true {% else %} false
    {% endif %}
    {% else %}
    false
    {% endif %}
    action:
    - service: notify.naskedbot
    data_template:
    message: 'El contenedor {{ trigger.to_state.name }}no funciona.'
    title: Alerta Contenedor
    - service: persistent_notification.create
    data_template:
    notification_id: offline_container
    title: Contenedor desconectado
    message: 'El contenedor {{ trigger.to_state.name }}no funciona.'

    Triggering automation HA show me this error:

    Error rendering data template: UndefinedError: ‘trigger’ is undefined

    thanks in advance…

    • Hey Nasked,

      When manually triggering an automation from the Home Assistant UI, the “trigger” variable won’t be set. The only real way to test that automation would be to shut down that container and wait the 5 minutes

      • Nasked

        I stopped container by ssh cli and wait 5 minutes, but automation doesn’t works. Also log send: Error rendering data template: UndefinedError: ‘trigger’ is undefined
        9:40 helpers/service.py (ERROR)

        • Hmm interesting. I am wondering if there’s an issue with my code here.

          You might have to use

          in the template instead.

          Let me know how you go, and if that works I’ll update the examples.

          • Nasked

            Nothing, i used it in template but automotion not run. Actually my code is:

            – alias: Container_offline
            initial_state: ‘on’
            trigger:
            – platform: state
            entity_id: switch.mqtt
            to: ‘off’
            for:
            minutes: 5
            condition:
            condition: and
            conditions:
            # Only send this once per hour
            – condition: template
            value_template: >
            {% if states.automation.container_offline.last_triggered is not none %}
            {% if as_timestamp(now()) | int – as_timestamp(states.automation.container_offline.attributes.last_triggered) | int > 3600 %} true {% else %} false
            {% endif %}
            {% else %}
            false
            {% endif %}
            action:
            – service: notify.naskedbot
            data_template:
            message: ‘El contenedor {{ trigger.to_state.attributes.friendly_name }}no funciona.’
            title: Alerta Contenedor
            – service: persistent_notification.create
            data_template:
            notification_id: offline_container
            title: Contenedor desconectado
            message: ‘El contenedor {{ trigger.to_state.attributes.friendly_name }}no funciona.’

          • Nasked

            Hi Phil:
            I only want to say you that your automation is working fine now. Great Job. My problem is that i work with rpi (arm) and some times dockers containers that you use i must to find others for my rpi.
            Now i installed influxdb and grafana. I have a doub….but better i will ask for my problem in your blog about grafana.

  • Michael McGuinness

    Hi,

    I am getting the following error once I try to start the docker image.

    2019-01-07 12:15:05 stderr npm ERR! /root/.npm/_logs/2019-01-07T12_15_04_637Z-debug.log
    2019-01-07 12:15:05 stderr npm ERR! A complete log of this run can be found in:
    2019-01-07 12:15:05 stderr
    2019-01-07 12:15:04 stderr npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
    2019-01-07 12:15:04 stderr npm ERR! Failed at the [email protected] start script.
    2019-01-07 12:15:04 stderr npm ERR!
    2019-01-07 12:15:04 stderr npm ERR! Exit status 1
    2019-01-07 12:15:04 stderr npm ERR! [email protected] start:
    node index.js
    2019-01-07 12:15:04 stderr npm ERR! errno 1
    2019-01-07 12:15:04 stderr npm ERR! code ELIFECYCLE
    2019-01-07 12:15:04 stderr at startup (bootstrap_node.js:193:16)
    2019-01-07 12:15:04 stderr at Function.Module.runMain (module.js:701:10)
    2019-01-07 12:15:04 stderr at Function.Module._load (module.js:505:3)
    2019-01-07 12:15:04 stderr at tryModuleLoad (module.js:513:12)
    2019-01-07 12:15:04 stderr at Module.load (module.js:573:32)
    2019-01-07 12:15:04 stderr at Object.Module._extensions..js (module.js:671:10)
    2019-01-07 12:15:04 stderr at Module._compile (module.js:660:30)
    2019-01-07 12:15:04 stderr at Object. (/usr/src/app/index.js:18:8)
    2019-01-07 12:15:04 stderr at Object.file (/usr/src/app/node_modules/merge-config/lib/configuration.js:200:20)
    2019-01-07 12:15:04 stderr at TherrorNamespace.errorConstructor (/usr/src/app/node_modules/therror/lib/therror.js:171:17)
    2019-01-07 12:15:04 stderr ^
    2019-01-07 12:15:04 stderr customize: !include customize.yaml
    2019-01-07 12:15:04 stderr FILE_INVALID: Invalid configuration file: "/config". unknown tag ! at line 14, column 37:
    2019-01-07 12:15:04 stderr ^
    2019-01-07 12:15:04 stderr throw errors.FILE_INVALID(filePath, e.message);
    2019-01-07 12:15:04 stderr /usr/src/app/node_modules/merge-config/lib/configuration.js:200
    2019-01-07 12:15:04 stderr
    2019-01-07 12:15:04 stdout Loading settings from /config

    Any ideas, I also used your other setup guide to setup home assistant of my Nas using Docker. I think from some reason I am missing the part where I mount the folder for it to read from the hass main docker?

    https://uploads.disquscdn.com/images/6044393422a13e85034fce33fca2a00619d915d50d5132cc36dcebb28033baf5.png https://uploads.disquscdn.com/images/65542b0b41a3ad298a8be07277f1316cdf7d1a48464009ffe8baadc8fbee667b.png

    • It looks like you’ve mounted the Home Assistant configs into HA-Dockermon. HA-Dockermon has its own configuration.yaml file, which you should mount in its own folder.

      I would suggest creating a docker/hadockermon/config folder on your NAS.

      • Michael McGuinness

        Thanks heaps, all fixed.

  • Nasked

    Hi Phil:

    I’m using many of your great jobs, as dishwasher automations, etc.

    Now i’m using two rpi. In my rpi3 i have 8 container with homeassistant and grafana running inside, and in my rpi1 i’m running ingluxdb in a container and telegraf (this last is impossible to run inside container with a rpi1—:-( )

    Well my question is How can i monitoring influxdb with your ha-dockermon if this container is running in other host? I tried this but HA don’t show me influxdb as switch in HA.

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

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

    Thank You.

  • Nasked

    Hi Phil:

    I’m using many of your great jobs, as dishwasher automations, etc.

    Now i’m using two rpi. In my rpi3 i have 8 container with homeassistant and grafana running inside, and in my rpi1 i’m running ingluxdb in a container and telegraf (this last is impossible to run inside container with a rpi1—:-( )

    Well my question is How can i monitoring influxdb with your ha-dockermon if this container is running in other host? I tried this but HA don’t show me influxdb as switch in HA.

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

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

    Thank You.

    • Hey Nasked,

      Just out of curiosity, the IP address 10.17.17.4, is that IP address the same as your local network? It looks like an internal docker IP address.

      You might need to run ha-dockermon with the –net=host flag set. From there, you should be able to access it from the same IP address as your Pi, for example 192.168.1.x:8126/

      • Nasked

        Ja ja ja, yes is my internal lan ip. My lan in 10.17.17.0/24 I like to make things difficult for strangers, 🙂 . I’m running your container as:
        docker run -d –name=ha-dockermon –restart=always -v /var/run/docker.sock:/var/run/docker.sock -v /docker/hadockermon:/config -p 8126:8126 tribunex/ha-dockermon-pi

        i thinked net host flag was running by default.. what probe can i do to check if ha-dockermon (installed at my rpi3 (10.17.17.3) can connect with my otrher rpi1 (10.17.17.4)?

        • Hmm so you’ll want to run a CURL command from the rpi1 to 10.17.17.4 on the port 8126.

          I would also test that you can get to both 10.17.17.3:8126 and 10.17.17.4:8126 from your PC/laptop to ensure that that’s working as expected.

  • Mark Rennie

    Has something changed Phil? As I have been getting this error for sometime now but never really looked into it..
    “2019-07-17 15:47:50 ERROR (MainThread) [homeassistant.components.shell_command] Error running command: curl http://192.168.0.102:8126/container/home-assistant/restart, return code: -11
    NoneType: None”

    • Mark Rennie

      edit… just found this https://github.com/MagicStack/uvloop/issues/118
      disable stream: and it works again.. so which do I need the most 😀

      • Oh damn that sucks!

        At least its not my code at fault 🙂

        Could you run that library inside a Docker of its own or something to isolate it from the host?

        • Mark Rennie

          not a clue Phil, beyond my skills.. and yep 100% not your code 🙂

  • Patrick Langenhuizen

    Thanks for this! This indeed should save me the trouble of doing ssh to restart home assistant. I do run into a strange thing though and I hope you can help me sort it out. I setup the restarting a docker container for my home assistant container and for the plex container that I have running. I did the setup for both containers the same. I created 2 scripts for restarting both containers using:

    restart_hass:
    alias: Restart Home-Assisant
    sequence:
    – service: shell_command.restart_hass

    and

    restart_plex:
    alias: Restart Plex
    sequence:
    - service: shell_command.restart_plex

    Now, when I do a call service from the lovelace page (either using a glance card, or the call service under developer tools), I get the notification

    Failed to call service script/restart_hass. is undefined

    . If I go to my unused entities and then to scripts, I see the restart_hass script and I can execute it. This will cause Home Assistant to restart. The same happens if I use the url that is used in the shell_command. So, why is it undefined when I do the call service from Lovelace? The code in my lovelace page is:

    - type: glance
    columns: 1
    title: Server
    entities:
    - entity: switch.hass
    name: Home Assistant
    tap_action:
    action: call-service
    service: script.restart_hass

    I have the exact same setup for the Plex container, and that one works perfect. Any idea?

    • Hey Patrick,

      That is quite strange. Nothing seems to be incorrect there, so not sure what it could be. I use the same setup, and it works fine here. (Classic developer “it works on my computer”)

      Could there be a problem with line endings somewhere? Maybe the YAML is getting muffed somehow. Otherwise I have no idea why it would be throwing an “undefined” error when you call it :/

      • Patrick Langenhuizen

        Hi Phil,

        thanks for the reply. Indeed, I would expect the error to be in the lovelace-ui.yaml, but I do not see it. I did some more testing and noticed that when I perform the tap-action as described previously, 4 errors appear in the Chrome console:
        once:

        Uncaught (in promise) {type: "result", success: false, error: {…}} connection-mixin.ts:75
        callService @ connection-mixin.ts:75
        async function (async)
        callService @ connection-mixin.ts:55
        R @ handle-click.ts:55
        value @ hui-glance-card.ts:223
        handleEvent @ parts.ts:502
        __boundHandleEvent @ parts.ts:460
        r @ long-press-directive.ts:130

        Three times:

        WebSocket connection to 'ws://192.168.2.15:8123/api/websocket' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED
        e @ socket.ts:53
        (anonymous) @ socket.ts:136
        createSocket @ socket.ts:136
        (anonymous) @ connection.ts:323
        r @ helpers.js:567
        (anonymous) @ connection.ts:324

        Does this mean anything to you?

        • Hmm no can’t say that means anything to me. Are you logged out of your session with Home Assistant or something?

          • Patrick Langenhuizen

            If I run the script from the unused enitity page, I only get the second error (the one that appears three times). This would make sense I guess, because Home Assistant docker is restarting and during restarting the connection is refused. But: I do not see the first error…

          • Have you tried without using call service and a plain entities card?

            Here is what I am using in Lovelace

            If you execute on that, does it work?

          • Patrick Langenhuizen

            Yes, that works like a charm

          • Hmm that makes me think there is a problem somewhere with the call service implementation. It might be expecting arguments to be sent to the script? You might need to specify service_data in the card, and just enter dummy values. Maybe that will solve it.

          • Patrick Langenhuizen

            It looks like it is working within the glance card now. It does however throw me the failure notification. Still weird that it did not work previously. Thank you for the help! Really appreciate you taking the time!

  • Patrick Langenhuizen

    Hi Phil.
    Quick question. I saw in the docker API info that it is also possible to obtain info on the docker images. This would include for instance the version and creation date of the image, making it easy to check whether you have the latest image or that an update is available. Is there any way I could do this using your HA dockermon software? This would be very useful for notifying me of an update on other docker images that I have using home assistant.

    • Hey Patrick,

      That’s rather interesting! There are some PR’s on the repo that will introduce the ability to pull docker images. However I’m currently in two minds about introducing a full-fledged pull and update capability. After all Watchtower seems to do that nicely, so I don’t have to re-invent Watchtower. But then a one click update button inside Home Assistant does sound very appealing!

      Now from my understanding, you can certainly see the version of the image you’re running locally. However I am not sure how to check for an updated image remotely. For example if Home Assistant pushes an update to the latest tag, the local machine won’t know about it until a docker pull occurs. Looking into Watchtower, it looks like all that does is every few minutes do a docker pull to fetch the latest image. Which doesn’t seem ideal to me.

      I guess this could be achieved as well, with HA-Dockermon doing a docker pull every x minutes. Then every x minutes it can check the running image of a container compared to the latest image, and if there’s an update available add a flag or something.

      What are your thoughts?

      • Patrick Langenhuizen

        Hi Phil

        There is a way of obtaining the information on the latest image using the docker registry API. I found this in the HA community under “sensor for latest docker image versions”: By doing a call to
        https://registry.hub.docker.com/v2/repositories///tags
        (for instance https://registry.hub.docker.com/v2/repositories/homeassistant/home-assistant/tags)
        will get you a JSON response that contains a number of image tags. In these tags you could do a search for "name": "latest" and extract the corresponding last_updated property. This is the date and time the “latest” tagged image was created. This could be used as a sensor in home assistant, where you can compare it to the last_updated of the local image you are running.

        Is this something you could use for implementing a one click update button?

        • Oooh! Great idea. This could be good info to add to the images. I’ll take a look

          • Patrick Langenhuizen

            Hi Phil,

            Did you get a chance to look at it yet?

          • Hey Patrick,

            Not yet. I’m thinking of adding it as part of hacktoberfest this month. Just trying to think of the best way to expose the data via the API

          • Patrick Langenhuizen

            Sounds like a good plan. Let me know if you need any help

          • Hey Patrick

            Taking a look at what I can get, I can get the hash of the running container image on the local machine. But it doesn’t look like the Docker API exposes the hash of this image in the response (see https://registry.hub.docker.com/v2/repositories/homeassistant/home-assistant/tags)

            If I could compare the two ID’s, I would then be able to make a decision if there is an update or not

          • Patrick Langenhuizen

            Hi Phil

            That is rather unfortunate. How about the “last_updated”-tag, or is that not robust enough for decision-making?

          • Hmm I’m sure there must be a way. Watchtower wouldn’t exist if there wasn’t a way! More investigation needed I think

          • Patrick Langenhuizen

            Did some digging and found the following on stackoverflow. Not sure if it is any help to you.
            https://stackoverflow.com/questions/41808763/how-to-determine-the-docker-image-id-for-a-tag-via-docker-hub-api

          • Hmm that seems to require a user token. Which I don’t think Watchtower needs. More research required I think

          • OK so the way WatchTower works, it continuously pulls the images on a schedule. For example every hour, it will issue a docker pull repo/name:latest

            If that command results in a new image being pulled down, WatchTower will then restart the container in place.

            Based on this, it could be possible to automate this with HA-Dockermon. For example you could setup a cron which makes a call to /pull/homeassistant/home-assistant and set a callback URI in the post body.

            From there HA-Dockermon will hit your callback. This could be anything, even a Home Assistant script which simply turns an input_boolean on to say there is a new update available.

            Eventually I’d like HA-Dockermon to get to a point where you can re-create a container in place, allowing a one-click upgrade button inside Home Assistant for docker users.

          • Patrick Langenhuizen

            I think this can be done a lot easier for Home Assistant. You could use the “updater” binary sensor, included in Home Assistant, for this purpose. For other containers, using the

            sensor for latest docker image versions

            I mentioned previously, creates kind of the same thing. Using these binary sensors, you could then create a simple button which issues a callback to HA-Dockermon to pull the latest image.

            It would be really awesome if HA-Dockermon is able to re-create a container. That is what I am hoping for 🙂

  • Klaus Giulio

    is it possible to restart all docker containers at once?

    • Not at the moment. However from Home Assistant you could put them into a Group and then turn the group on/off as needed

  • Miguelpdm

    Hi! My responses don’t include state:

    {“status”:”Exited (137) 6 minutes ago”,”image”:”alpine-py-dasher:latest”}

    Is it normal?

    Thaks and congrats on the development!

  • Following the above directions as-shown doesn’t work due to typos, unclear directions, etc.

    1) In the docker run command, you’re missing several at the ends of the lines.
    2) Where does my_customize.yaml go? In the same folder as configuration.yaml or in some subdirectory?
    3) Following the above (with the docker run fixed and with my_customize.yaml in the same directory as configuration.yaml) down to the Restarting Home Assistant section, I get: Entity not available: switch.mqtt

    This is on a stock HomeAssistant install with only the Configurator addon installed and no manual changes to the config files.

    Also with the automation section, you have it in automation.yaml but stock HomeAssistant has an automations.yaml. And you show the first line of automation.yaml as:

    – alias: Alert when a critical container goes offline

    But that’s not valid syntax (i.e. the – indicates that it’s one of several lines of the same type under a higher level.) So what would be in automations before that? And if there is something before it, like:

    automations:
    – alias: Alert when a critical container goes offline

    then the line starting with “- alias” should be indented some.

    • Hi Mike,

      Please point out these typos that make it unclear for you? I’ll address your points

      1) Yes, looks like some are missing. I’ll fix that.
      2) See https://www.home-assistant.io/docs/configuration/customizing-devices/#customization-using-the-ui for instructions
      3) Have you declared a switch which will be named MQTT? You might need to remove my default ones if you’re just copy pasting to match your containers

      Regarding the automations, please note the article was written in Jan 2018. Before the Home Assistant UI automation editor. Please see the home assistant website on how to use automation old to configure your own manual automations in YAML files, or use packages.

  • Nasked

    Hi! Phil:

    Two questions:
    I’m using your ha-dockemon on my Home Assistant docker container. All my containers are running in a CT on Proxmox. I just create a new CT with others containers running and i wish to controll them.
    I moved tasmoadmind from my Domotic container (Ip:XXX.XXX.XXX.100 ) to my new container Domoaddons (Ip:XXX.XXX.XXX.101). I changed ip on switch.yaml file from 127.0.0.1:8126 to XXX.XXX.XXX.101:8126 but seems doesn’t works.
    I must install another ha-dockermon in this new CT?

    Thanks in advance.

    • Hi Nasked,

      I think that’s right. You’ll need to run another ha-dockermon instance there and it should work

  • Jörgen Karlsson

    Hi Phil,

    I saw in the github repo that mqtt support is on it’s way. When can we expect a release of that?

    • Very soon! I have been testing it myself to make sure it doesn’t crash.

      I’ve still got a couple of tweaks to make before I’m happy to push it to the release version (but nothing critical).

      if you want to install it now, you can use the “edge” tag on Dockerhub to pull it in and try it out (and can use Git to report any issues if you come across any)

  • Matt Bridges

    Hi Phil, love this post, was just wondering how I’d go about displaying the container statuses for all the containers I’m running. I’m not clear on api calls etc although I’ve got a rest switch configured for one of my containers so I can stop/start it. Any help greatly received.

  • Andy

    Hey Phil! I’m not sure if this question went through the first time so I’ll try again. I have the dockermon container up and running and all works perfectly! I can start and stop containers and get status through http GET without issue. My problem is with adding authentication to the http port:

    I bound the container /config directory to a folder on my rpi. In that folder I created a configuration.yaml file. In that file i have added three lines:


    http.port: 8127
    http.username: 'test'
    http.password: 'test_password'

    I can see from the container logs that ha-dockermon sees the file. For example, if i format it incorrectly, the container bugs out in the logs. However, the above configuration does not cause the container to listen on a different port (logs show it listening on 8126) and the connection does not require authentication (I can start and stop containers via GET without a password):


    > [email protected] start /usr/src/app
    > node index.js
    Loading settings from /config
    HA-Dockermon server listening on port 8126

    Any ideas as to what I’m doing wrong here?

    • Hey Andy,

      I think HTTP needs to be specified like this:

      Hopefully that does the trick

      • Andy

        That did it! Thanks for the help!

  • Adi Ezaguy

    Hi Phil, tnx for the arrticle which i’m using already for few years.
    lately I get this message in HA logs: Template variable error: 'value_json' is undefined when rendering '{{ value_json is not none and value_json.state == "running" }}'
    I guess it is something with some recent HA update.
    what shall I update?

    Thanks!

    • Matt Lamb

      Hey Adi, I was seeing this in my logs also. Turns out that I had changed the name of one of my docker containers and needed therefore to update the url link in the rest sensor.

      • Adi Ezaguy

        Right! for my case it was an old un-used container. TNX!

  • Scott Crawford

    @philhawthorne:disqus thanks so much for sharing such an awesome tool.

    I have a
    question.. I run most of my containers with their own IP addresses. Is
    there a way to allow the /container/grafana command show all details of
    the container like the /containers command does? So I can then pick out
    the IP addresses (and other stuff) to use in the value_template.. Or if
    not, do you have any suggestions for how I could use the /containers
    command to put that info into each HA sensor?

Start typing and press Enter to search