Exposing docker.sock for monitor_docker

Quick question, yes it works now from my current Docker. However, I do have an extra docker container in another machine and would like to get data from it. Is there a way to remotely grab it? Using portainer can be easily done but how to setup from HA?

1 Like

how do you show Portainer in HA Backend? :slight_smile: Thanks

Install the portainer add-on.

1 Like

Check point for of the QA in the monitor docker docs -> https://github.com/ualex73/monitor_docker#qa you can monitor them over tcp.

simply used panel_iframe to add portainer

1 Like

yeah… just need to add more volume to the certs, again thanks to @Craig_McGowan method it’s simple. Again thank @Burningstone for the insight.

1 Like

Off-topic a bit here but what card(s) are you using to display your containers? They look fantastic.

Hi Mike

Each container just has a card containing some entities, glances and gauges. They are then just put together in the custom vertical-stack-in-card card.

Please see below

Thanks

type: 'custom:vertical-stack-in-card'
title: Log Viewer (Addon)
cards:
  - type: horizontal-stack
    cards:
      - type: gauge
        entity: sensor.docker_addon_a0d7b954_logviewer_cpu
        min: 0
        max: 100
        name: CPU (Cores)
        severity:
          green: 0
          yellow: 20
          red: 40
      - type: gauge
        entity: sensor.docker_addon_a0d7b954_logviewer_1cpu
        min: 0
        max: 100
        name: CPU (Overall)
        severity:
          green: 0
          yellow: 20
          red: 40
      - type: gauge
        entity: sensor.docker_addon_a0d7b954_logviewer_memory_percent
        min: 0
        max: 100
        name: Memory
        severity:
          green: 0
          yellow: 20
          red: 40
  - type: glance
    show_name: false
    entities:
      - entity: sensor.docker_addon_a0d7b954_logviewer_memory
      - entity: sensor.docker_addon_a0d7b954_logviewer_network_speed_up
      - entity: sensor.docker_addon_a0d7b954_logviewer_network_speed_down
      - entity: sensor.docker_addon_a0d7b954_logviewer_network_total_down
      - entity: sensor.docker_addon_a0d7b954_logviewer_network_total_up
  - type: entities
    entities:
      - entity: switch.docker_addon_a0d7b954_logviewer
        name: Log Viewer
      - entity: sensor.docker_addon_a0d7b954_logviewer_image
        name: Image
      - entity: sensor.docker_addon_a0d7b954_logviewer_status
        name: Status

2 Likes

Awesome, thank you!

Not sure if you are still trying to expose docker.sock, but here is the solution I use with monitor_docker. Thought I would document this here for anyone else having this issue.

I use docker compose file for HomeAss plus all my other containers. Rather than fight the fact that you cannot add docker.sock to the HomeAss container, instead “proxy” it.

I run HomeAssistant in an unsupported container config on a NUC. Tried to add docker.sock to my docker compose for HomeAssistant, but it never worked. When you add docker.sock via Portainer and restart HomeAssistant then you are not really changing “your” container, but rather Portainer is recreating a copy of yours and adding the docker.sock to that. If you do an upgrade then that copy (with the added docker.sock line) is trashed - which is the problem you are having.

The smarter, more permanent solution is to proxy docker.sock, and configure monitor_docker to use the proxy rather than docker.sock. If the HomeAssistant docker is re-created, upgraded, whatever then who cares because monitor_docker is just picking up the info from the proxy.

Long intro, sorry. Short solution:

  1. Create a proxy container
  2. Configure monitor_docker to use proxy
  3. There is no 3

So:

  1. Add the following to your docker compose file (I’m using port 2375 - feel free to change, but use the same port in the monitor_docker config in step 2 below):
# Proxy the Docker sock so that we can pick up stats for HomeAssistant
  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
      - SYSTEM=1
      - TASKS=1
      - VOLUMES=1
  1. Change monitor_docker config to use port 2375 on the ip address you are running the docker image above.
monitor_docker:
  - name: Docker
    url: tcp://192.168.1.99:2375
    # containers:
    rename:
      plex: Plex
    # monitored_conditions:

  1. As I said, there is no step 3.
3 Likes

Thank you so, so much!

This is the perfect answer and will now save me the hassle of having to expose the sock every time I upgrade!!

I did have to add some extra environmental properties to allow monitor_docker to be able to turn on and off the containers.

System, Post and Auth I set as 1. I’m not sure all were required but as stated in the original thread, my docker experience is limited so I’m just happy it now works.

Thanks again.

Further to my excitement last night, it seems im struggling a bit today with this solution.

I keep getting lots of errors about being unable to connect to docker, resulting in my sensors not returning any values.

Is this something you have experienced?

addon_core_mariadb: Container not available anymore (3) (DockerError(900, 'Cannot connect to Docker Engine via tcp://192.168.1.101:2375 [Server disconnected]'))
addon_a0d7b954_logviewer: Container not available anymore (3) (DockerError(900, 'Cannot connect to Docker Engine via tcp://192.168.1.101:2375 [Server disconnected]'))
facebox: Container not available anymore (3) (DockerError(900, 'Cannot connect to Docker Engine via tcp://192.168.1.101:2375 [Server disconnected]'))
portainer: Container not available anymore (3) (DockerError(900, 'Cannot connect to Docker Engine via tcp://192.168.1.101:2375 [Server disconnected]'))
hassio_dns: Container not available anymore (3) (DockerError(900, 'Cannot connect to Docker Engine via tcp://192.168.1.101:2375 [Server disconnected]'))

Yup, I see some timeouts like you in the log. I had always assumed that these were just one-off missed collections, as the container State entity was still showing “running” (this is currently as far as I have got to introduce these stats into a lovelace panel). On review though all stats stop once you get a timeout message.

I can’t see any errors in the log file for the proxy. I tried restarting the container via Portainer, but still not getting any stats for the once that timed out.

I guess checking with the guy who wrote monitor_docker is the next step?

Yup, I see some timeouts like you in the log. I had always assumed that these were just one-off missed collections, as the container State entity was still showing “running” (this is currently as far as I have got to introduce these stats into a lovelace panel). On review though all stats stop once you get a timeout message.

Yes I have the same behaviour unfortunately and once I get an error everything stops reporting back, despite all containers, including my proxy still running fine. Im currently monitoring 25 containers so im going to try and cut this down to a handful and see if I still get issues.

The 900 error message is mentioned in the github for the addon - GitHub - ualex73/monitor_docker: Monitor Docker containers from Home Assistant question 3

After a bit of testing, setting the scan interval to 30 (rather than the default 10) seconds seems to keep everything stable.

If I stop a container via docker monitor I do lose control and information from just this specific container until I restart HA. This is due to the container ID changing. I’m not sure why it changes when stopping, maybe to do with hassio looking after the containers and resetting them

Interesting. I think I will request a change that if the list of containers is blank (ie monitor everything) then the integration rescans to generate the list of running containers every 5 or 10 minutes. This would mean if a container wasn’t started at load time, or the connection failed for some reason, then it would be picked up again. This would also fix your issue of the container ID changing - you’d lose 5 - 10 minutes of stats but better than having to restart HA.

Just added up the number of containers it is monitoring - 34.

Requested enhancement - https://github.com/ualex73/monitor_docker/issues/60

Ive seen your request and hopefully the dev can do something to come around this issue.

However, one point I am confused on…

Firstly, I use Portainer as my experience in Docker is very (non existent) limited.
However, when I create a new container the sensors straight away show up for it in HA.

Therefore, I cant understand why when recreating a container it doesnt show up why in your examples above (ie wasnt started on load or connection failed) that it wouldnt pick them up when they reconnected.

Odd…

Hi guys, sorry if I’m a little off topic here, but it’s kind of related… firstly, am running Hassio on an intel nuc (latest and greatest version) but my main docker stack is on another ubuntu server (through portainer), but I am unable to get to that at all. I am using url: tcp://myip:2376 but i get nothing in the logs. When I check in portainer on the remote server, i can see the following:

Is that enough? Or is there something else I am missing.
Cheers!

need more detailed information… did you install the proxy on your ubuntu? did you create LTS certs?