1

I'm running a Home Assistant instance in a Docker container with an integration I'm working on connecting to a local device.

My Python looks like this, which I've tested directly on my Windows machine, it runs and connects just fine

        if self.session is None:
            self.session = aiohttp.ClientSession()
            self._close_session = True

        try:
            async with async_timeout.timeout(self.request_timeout):
                response = await self.session.request(method, url, json=data)
                LOGGER.info(response)

When running this in the container however, it fails to connect. The IP of my Windows machine is 192.168.1.174, the IP of the device I'm trying to connect to is 192.168.1.190. I've also tried connecting the container to a Docker network with a 192.168.1.0/24 subnet, which gave my container the IP of 192.168.1.2, but it's still unable to reach the device.

I'm at a loss now as to what the issue could be. Any suggestions please?

K20GH
  • 6,032
  • 20
  • 78
  • 118
  • Did you map your ports when you created the Docker container? You won't be able to reach the HA service in the container if the ports aren't mapped – CodeCaffeinateContinue Sep 01 '22 at 12:20
  • I can access the UI on HA just fine, mapping port 8123. The integration is trying to get to the network on port 80 – K20GH Sep 01 '22 at 12:33

1 Answers1

0

If you are inside a container and trying to access a local device using the URL, the base image used by the container must be able to resolve hostnames to IPs or else it will not be able to connect since there is no resolution to an IP for the local device

Make sure to go inside the container and run

host google.com 

this should resolve into an IP or else you will to add that capability

shravan
  • 11
  • 4
  • `google.com has address 142.250.187.238` `google.com has IPv6 address 2a00:1450:4009:815::200e` `google.com mail is handled by 10 smtp.google.com.` Seems like thats functioning correctly – K20GH Sep 01 '22 at 10:47
  • you have connected your docker container to a network having the same subnet. I would suggest using the bridge network with a different network subnet. that should make things work – shravan Sep 01 '22 at 10:50
  • It's now running on the standard bridge network `172.17.0.0/16` with an IP of `172.17.0.2`, but is still unable to connect – K20GH Sep 01 '22 at 10:59
  • I have the same setup on my workstation where i have an alpine image with `172.17.0.2` IP and its able to ping the host machine at `192.168.1.3` . Please check if there are any firewall running on Windows and turn it off if its safe to do so and restart docker – shravan Sep 01 '22 at 11:16
  • Fully disabled windows firewall and restarted docker. Still nothing :/ – K20GH Sep 01 '22 at 11:23
  • can you please try logging in to the container and try a ping using the IP address ( workstation itself ) to see if its pingable or not? – shravan Sep 01 '22 at 11:37
  • This doesn't work as I think Home Assistant may block it. I've just tried my MacBook and it works fine, same environment etc, so this must be something specific to Windows – K20GH Sep 01 '22 at 11:38
  • this is surely a Windows issue. one last try would be to set a static DNS for docker application on windows or use a host network configuration. – shravan Sep 01 '22 at 11:43