怎么查看我的host.docker.internal
时间: 2025-03-06 21:48:10 浏览: 149
### 使用 `host.docker.internal` 访问宿主机
为了使 Docker 容器能够访问宿主机的服务,可以利用特定的 DNS 名称 `host.docker.internal`。这一方法简化了容器与宿主机间的通信过程[^2]。
当在 Linux 平台上操作时,需额外指定参数来启用该功能:
```bash
docker run -d --add-host host.docker.internal:host-gateway my-container:latest
```
上述命令中的 `--add-host=host.docker.internal:host-gateway` 参数使得容器内的应用程序可以通过 `host.docker.internal` 来解析并连接至宿主机上的服务[^1]。
对于 Windows 和 macOS 用户来说,默认情况下已经支持直接使用 `host.docker.internal` 而无需添加任何特殊选项[^3]。
一旦按照以上方式设置了容器环境,在容器内发起针对 `http://host.docker.internal:<port>` 形式的 HTTP 请求即可实现对宿主机上监听相应端口的服务进行调用。
#### 示例 Python 代码片段用于测试连通性
假设宿主机上有一个 Web API 正在本地地址 `localhost:8080` 提供服务,则可以在容器中编写如下简单的 Python 测试脚本验证是否能成功访问:
```python
import requests
response = requests.get('http://host.docker.internal:8080')
print(response.status_code, response.text)
```
如果一切设置无误,这段程序应当返回来自宿主机 Web API 的响应状态码以及消息体内容。
阅读全文
相关推荐


















