简体   繁体   English

Docker 容器无法访问本地网络设备

[英]Docker Container cannot access local network devices

I'm running a Home Assistant instance in a Docker container with an integration I'm working on connecting to a local device.我正在 Docker 容器中运行 Home Assistant 实例,其中集成了我正在连接到本地设备的集成。

My Python looks like this, which I've tested directly on my Windows machine, it runs and connects just fine我的 Python 看起来像这样,我已经在我的 Windows 机器上直接测试过,它运行和连接都很好

        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 .我的 Windows 机器的 IP 是192.168.1.174 ,我试图连接的设备的192.168.1.190是 2.168 1909。 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.我还尝试将容器连接到具有192.168.1.0/24子网的 Docker 网络,这为我的容器提供了192.168.1.2的 IP ,但它仍然无法访问设备。

I'm at a loss now as to what the issue could be.我现在不知道问题可能是什么。 Any suggestions please?请问有什么建议吗?

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如果您在容器内并尝试使用 URL 访问本地设备,则容器使用的基本映像必须能够将主机名解析为 IP,否则它将无法连接,因为没有对 IP 的解析本地设备

Make sure to go inside the container and run确保 go 在容器内并运行

host google.com 

this should resolve into an IP or else you will to add that capability这应该解析为 IP 否则您将添加该功能

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM