Sanderhuisman / docker_monitor

Apache License 2.0
15 stars 5 forks source link

Docker_monitor: linked to image name instead of container name #2

Closed elRadix closed 5 years ago

elRadix commented 5 years ago

Your component should use the container name instead of the image name.

When my image gets auto updated by Ouroboros in this case for plex then the switch does not recognize it anymore. Can you have it linked as dockermon did to the container name ie. plex which never change.

Ouroboros has updated containers! Host/Socket: raspi.lan / var/run/docker.sock Containers Monitored: 3 Total Containers Updated: 1 Containers updated this pass: 1 plex updated from 2488685646 to d158f2e9c4

Workaround is to restart HA inorder to get the switch linked again to correct image.

Failed to call service switch/turn_on. 404 Client Error: Not Found ("No such container: e3fd618dcb2ede9a1f1dcba69e0e175b96be576d6065482fccc63b84a81295e0")

404 Client Error: Not Found ("No such container: e3fd618dcb2ede9a1f1dcba69e0e175b96be576d6065482fccc63b84a81295e0")
Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.7/site-packages/docker/api/client.py", line 256, in _raise_for_status
    response.raise_for_status()
  File "/srv/homeassistant/lib/python3.7/site-packages/requests/models.py", line 940, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 404 Client Error: Not Found for url: http+docker://localhost/v1.35/containers/e3fd618dcb2ede9a1f1dcba69e0e175b96be576d6065482fccc63b84a81295e0/start

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/components/websocket_api/commands.py", line 121, in handle_call_service
    connection.context(msg))
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/core.py", line 1150, in async_call
    self._execute_service(handler, service_call))
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/core.py", line 1172, in _execute_service
    await handler.func(service_call)
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/helpers/entity_component.py", line 194, in handle_service
    required_features
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/helpers/service.py", line 316, in entity_service_call
    future.result()  # pop exception if have
  File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/helpers/service.py", line 337, in _handle_service_platform_call
    await getattr(entity, func)(**data)
  File "/usr/local/lib/python3.7/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/home/homeassistant/.homeassistant/custom_components/docker_monitor/switch.py", line 100, in turn_on
    self._container.start()
  File "/home/homeassistant/.homeassistant/custom_components/docker_monitor/__init__.py", line 294, in start
    self._container.start()
  File "/srv/homeassistant/lib/python3.7/site-packages/docker/models/containers.py", line 392, in start
    return self.client.api.start(self.id, **kwargs)
  File "/srv/homeassistant/lib/python3.7/site-packages/docker/utils/decorators.py", line 19, in wrapped
    return f(self, resource_id, *args, **kwargs)
  File "/srv/homeassistant/lib/python3.7/site-packages/docker/api/container.py", line 1091, in start
    self._raise_for_status(res)
  File "/srv/homeassistant/lib/python3.7/site-packages/docker/api/client.py", line 258, in _raise_for_status
    raise create_api_error_from_http_exception(e)
  File "/srv/homeassistant/lib/python3.7/site-packages/docker/errors.py", line 31, in create_api_error_from_http_exception
    raise cls(e, response=response, explanation=explanation)
docker.errors.NotFound: 404 Client Error: Not Found ("No such container: e3fd618dcb2ede9a1f1dcba69e0e175b96be576d6065482fccc63b84a81295e0")
pprazzi commented 5 years ago

I just noted the same today. I updated a container using portainer and restarted that container, the new container wasn´t picked-up by docker_monitor. When restarting HA it got monitored again.

ualex73 commented 5 years ago

@pprazzi - yes this is the 'current' coded behavior.

Sanderhuisman commented 5 years ago

The master branch uses an old version of my docker monitor. This version connects at startup to the docker API and is for example not able to reconnect or to reinitialize when a container is recreated.

In a different branch, I've rewritten the sensor completely but I need more testing to merge it to the stable master branch;) I've been busy last months finishing my master thesis, but due to illness I've got some time to work a bit on this sensor.

ualex73 commented 5 years ago

@Sanderhuisman - maybe it is an option to create 1 dev branch, then we can test with it and submit pull request against that one too?

Sanderhuisman commented 5 years ago

I've moved the docker monitor component to its own repository. The rewritten sensor is available in the develop branch. Warning: the configuration is different. You can now specify the sensors and switch for each individual container ;)

In this way, the component is compatible with HACS.

juan11perez commented 5 years ago

@Sanderhuisman thank you. I've tried the dev component and works great. container doesnt "unlink" from HA on recreation. However, I tried different configuration permutations and the only way for the component to work is to list each container. The document says it defaults to all, but If i dont specify a container, it doesnt load. Can this be unspecified so it loads all containers? Thank you

Sanderhuisman commented 5 years ago

With the rewrite, I made the component such that it can reconnect to the docker host after lost connection. Therefore, it can be that the host is not available when home-assistant starts.

I will make a new bug report and I will look in the direction of auto-discovery of containers. See #16 .

juan11perez commented 5 years ago

thank you for your response and consideration