MqDockerUp: A tool to monitor and update your docker containers from Home Assistant

I happy to provide you exactly that, it creates a new card for each container and shows its entities. You need card-mod and auto-entities:

type: custom:auto-entities
show_empty: true
card:
  type: grid
  square: false
  columns: 1
card_param: cards
filter:
  template: |-
    {% set ns = namespace(entities=[]) %}
    {% for sensor in states.sensor %}
      {% if sensor.entity_id | regex_search("sensor\..*_container_name$", ignorecase=True) and sensor.state not in ['unavailable', 'unknown'] %}
        {% set container_name = sensor.attributes.friendly_name | regex_replace(" Container Name", "", ignorecase=True) %}
        
        {% set status_entity_id = sensor.entity_id | replace("_container_name", "_container_status") %}
        {% set registry_entity_id = sensor.entity_id | replace("_container_name", "_docker_registry") %}
        {% set image_entity_id = sensor.entity_id | replace("_container_name", "_docker_image") %}
        {% set tag_entity_id = sensor.entity_id | replace("_container_name", "_docker_tag") %}
        {% set ports_entity_id = sensor.entity_id | replace("_container_name", "_exposed_ports") %}
        
        {% set status_entity = states.sensor[status_entity_id.split('.')[1]] %}
        {% set registry_entity = states.sensor[registry_entity_id.split('.')[1]] %}
        {% set image_entity = states.sensor[image_entity_id.split('.')[1]] %}
        {% set tag_entity = states.sensor[tag_entity_id.split('.')[1]] %}
        {% set ports_entity = states.sensor[ports_entity_id.split('.')[1]] %}
        
        {% if status_entity is defined and status_entity.state not in ['unavailable', 'unknown'] %}
          {% set container_info = {
            'type': 'entities',
            'title': container_name,
            'entities': [
              {'entity': status_entity_id, 'name': status_entity.attributes.friendly_name | replace(container_name, "")},
              {'entity': registry_entity_id, 'name': registry_entity.attributes.friendly_name | replace(container_name, "")},
              {'entity': image_entity_id, 'name': image_entity.attributes.friendly_name | replace(container_name, "")},
              {'entity': tag_entity_id, 'name': tag_entity.attributes.friendly_name | replace(container_name, "")},
              {'entity': ports_entity_id, 'name': ports_entity.attributes.friendly_name | replace(container_name, "")},
            ]
          } %}
          {% set ns.entities = ns.entities + [container_info] %}
        {% endif %}
        
      {% endif %}
    {% endfor %} {{ ns.entities | tojson }}

2 Likes

Thanks for the code, I added the update value in your code:

{% set update_entity_id = sensor.entity_id | replace("_container_name", "_update") %}
{% set update_entity = update_entity_id | replace("sensor", "update") %}
{'entity': update_entity, 'name': "Update"}

See below:

type: custom:auto-entities
show_empty: true
card:
  type: grid
  square: false
  columns: 1
card_param: cards
filter:
  template: |-
    {% set ns = namespace(entities=[]) %}
    {% for sensor in states.sensor %}
      {% if sensor.entity_id | regex_search("sensor\..*_container_name.*$", ignorecase=True) and sensor.state not in ['unavailable', 'unknown'] %}
        {% set container_name = sensor.attributes.friendly_name | regex_replace(" Container Name", "", ignorecase=True) %}
        
        {% set status_entity_id = sensor.entity_id | replace("_container_name", "_container_status") %}
        {% set registry_entity_id = sensor.entity_id | replace("_container_name", "_docker_registry") %}
        {% set image_entity_id = sensor.entity_id | replace("_container_name", "_docker_image") %}
        {% set tag_entity_id = sensor.entity_id | replace("_container_name", "_docker_tag") %}
        {% set ports_entity_id = sensor.entity_id | replace("_container_name", "_exposed_ports") %}
        {% set update_entity_id = sensor.entity_id | replace("_container_name", "_update") %}
        
        {% set status_entity = states.sensor[status_entity_id.split('.')[1]] %}
        {% set registry_entity = states.sensor[registry_entity_id.split('.')[1]] %}
        {% set image_entity = states.sensor[image_entity_id.split('.')[1]] %}
        {% set tag_entity = states.sensor[tag_entity_id.split('.')[1]] %}
        {% set ports_entity = states.sensor[ports_entity_id.split('.')[1]] %}
        {% set update_entity = update_entity_id | replace("sensor", "update") %}
        
        {% if status_entity is defined and status_entity.state not in ['unavailable', 'unknown'] %}
          {% set container_info = {
            'type': 'entities',
            'title': container_name,
            'entities': [
              {'entity': status_entity_id, 'name': status_entity.attributes.friendly_name | replace(container_name, "")},
              {'entity': registry_entity_id, 'name': registry_entity.attributes.friendly_name | replace(container_name, "")},
              {'entity': image_entity_id, 'name': image_entity.attributes.friendly_name | replace(container_name, "")},
              {'entity': tag_entity_id, 'name': tag_entity.attributes.friendly_name | replace(container_name, "")},
              {'entity': ports_entity_id, 'name': ports_entity.attributes.friendly_name | replace(container_name, "")},
              {'entity': update_entity, 'name': "Update"}
            ]
          } %}
          {% set ns.entities = ns.entities + [container_info] %}
        {% endif %}
        
      {% endif %}
    {% endfor %} {{ ns.entities | tojson }}

image

2 Likes

One more addition, somehow when I rebuild images they get the name with “_2” added at the end. Therefore I’ve changed your regular expression to:

regex_search("sensor\..*_container_name.*$", ignorecase=True)

@pvh0 Thanks for the help :slight_smile:

My compose editor is in Portainer and mqdocker in part of a stack.

The syntax I use for the environment part is slightly different and I wonder if that could be part of the problem?

The other docker part of the stack is running fine.

Here is my redacted code :

  mqdockerup:
    image: micrib/mqdockerup:latest
    container_name: mqdockerup
    environment:
      - PUID=xxx
      - PGID=xxx
      - TZ=zone
      - MAIN_INTERVAL:5m
      - MQTT_CONNECTIONURI:mqtt://myMQTT_IP:1883
      - MQTT_USERNAME:username
      - MQTT_PASSWORD:password
#      - ACCESSTOKENS_GITHUB: ""
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock # This is required to access the docker API	
      - /volume1/docker/mqdockerup/:/app/data/ # This is required to store the data (database.db)
    network_mode: xxx
    restart: unless-stopped

My compose editor will not work without the - in front of the variables names or with quotes around the values as far as I can see.

To be clear the standalone version was installed on a computer and not on the NAS, so I suspect that is why it cannot connect to the docker.

In any case, I prefer to only use the docker if that is possible so I am hoping it is simply a configuration issue.

1 Like

I’m using the same, Portainer compose editor in a stack and have shown my code. Yours is different in several ways:

  • Version is not defined ( environment and content attributes are available with Docker Compose version 2.23.1 and later.)
  • Your environment variables start with a "- " , not sure if that matters
  • I see some undocumented variables that I do not know (e.g. PUID)
  • your values are not between double quotes (e.g. MQTT_CONNECTIONURI)

Would recommend to start a new stack to test just running the MQdockerup image in portainer with the compose coe I provided, with you password and username etc added. See also here a link for the official compose example:

2 Likes

Thanks @pvh0 I think I simply went down a rabbit hole!

Launching the compose in a separate stack whilst reviewing the syntax allowed me to fix the problem.

I’m not sure why this syntax with the “-” works with my other dockers such as sonarr, radarr, etc, but not this one.

All the same I’m up and running and already loving this service. Exactly what I needed to maintain my docker within HA :star_struck:

2 Likes

It has been working very well for me since I managed to get this running.

There are however a few containers for which I have issues.

Both tecnativa/docker-socket-proxy and plexinc/pms-docker (public tag) are reporting unknown status.

Has anybody experienced this and found a fix?
I searched but could not find references.

Cheers,

Hello,

Any news regarding restart functionality?

Thanks for the great work! I’m really looking forward to the start/stop functionality, that would make your tool complete.