概览
使用 Docker 或 Podman 开发时,容器经常需要访问宿主机上运行的数据库、API 或开发服务器。这篇文章介绍不同平台和容器运行时下的访问方法。
方法一:使用特殊 DNS 名称(推荐)
容器运行时提供了能解析到宿主机 IP 的特殊 DNS 名称。这是最方便、可移植性也最好的一种方法。
| 运行时 | DNS 名称 |
|---|---|
| Docker | host.docker.internal |
| Podman | host.containers.internal |
用法
在容器内部,可以这样访问宿主机服务:
# Docker
http://host.docker.internal:[port]
# Podman
http://host.containers.internal:[port]
示例: 假设宿主机的 Web 服务运行在 3000 端口:
# Docker
curl http://host.docker.internal:3000
# Podman
curl http://host.containers.internal:3000
平台支持
Docker:
- macOS:✅ Docker Desktop 默认支持。
- Windows:✅ Docker Desktop 默认支持。
- Linux:⚠️ 可能要补充配置,见下文。
Podman:
- Linux:✅ Podman 4.1 及以上版本默认支持。
- macOS:✅ 可配合 Podman Desktop 使用。
- Windows:✅ 可配合 Podman Desktop 使用。
Linux 配置
旧版本 Docker 在 Linux 上可能默认无法解析 host.docker.internal。推荐在 docker-compose.yml 中添加 extra_hosts:
version: "3.8"
services:
your-service-name:
image: your-image
# ... other configurations
extra_hosts:
- "host.docker.internal:host-gateway"
作用:
host-gateway是 Docker Compose 能识别的特殊值。- 它会自动解析为宿主机的内部网关 IP。
- 这样各平台都能一致地使用
host.docker.internal。
使用 docker run 时的等效写法:
docker run --add-host=host.docker.internal:host-gateway your-image
常见用例
访问宿主机上的开发数据库
version: "3.8"
services:
web-app:
image: node:18
environment:
DATABASE_URL: "postgresql://user:[email protected]:5432/mydb"
extra_hosts:
- "host.docker.internal:host-gateway"
连接宿主机上的 API 服务器
// Inside your containerized application
const API_BASE_URL = process.env.API_URL || 'http://host.docker.internal:8000';
fetch(`${API_BASE_URL}/api/data`)
.then(response => response.json())
.then(data => console.log(data));
方法二:使用 host 网络模式
另一种做法是带 --network=host 运行容器,直接共享宿主机的网络栈。
用法
使用 docker run:
docker run --network=host your-image
使用 docker-compose.yml:
version: "3.8"
services:
your-service:
image: your-image
network_mode: host
工作原理
使用 host 网络时:
- 容器和宿主机具有相同的网络身份。
- 可以通过
http://localhost:[port]或宿主机的 IP 地址连接服务。 - 无须使用
host.docker.internal。 - 无须配置端口映射,端口会直接对外可见。
示例: 假设宿主机有服务监听 3000 端口:
# Inside the container
curl http://localhost:3000
取舍
优点:
- ✅ 配置简单。
- ✅ 无须 DNS 解析。
- ✅ 没有 NAT 开销,网络性能更好。
缺点:
- ❌ 容器与宿主机之间的网络隔离减弱。
- ❌ 可能出现端口冲突,因为容器服务会直接绑定宿主机端口。
- ❌ 跨环境移植较困难。
- ❌ 不推荐用于生产部署。
- ❌ Mac/Windows 版 Docker Desktop 不支持(原文将 host 网络模式列为 Linux 专用)。
什么时候使用 host 网络
适合 host 网络的场景包括:
- 快速的本地开发测试。
- 对网络延迟敏感的高性能应用。
- 特定的 Linux 部署场景。
以下场景应避免使用 host 网络:
- 生产环境,应使用具备适当隔离能力的桥接网络。
- 需要网络隔离的多容器应用。
- 跨平台开发,文中所述的 macOS/Windows 环境无法使用。
方法三:使用 Docker 桥接网络的网关 IP(仅 Linux)
在 Linux 上,Docker 桥接网络(通常是 docker0)有一个默认网关 IP,容器可以通过它访问宿主机。
查找网关 IP
# Method 1: Inspect docker0 interface
ip addr show docker0
# Look for: inet 172.17.0.1/16
# Method 2: Check from inside a container
docker run --rm alpine ip route | grep default
# Output: default via 172.17.0.1 dev eth0
用法
Docker 桥接网络的默认网关通常是 172.17.0.1,具体值可能不同:
# From inside the container
curl http://172.17.0.1:3000
注意: 这个地址会随 Docker 配置改变,所以用 host.docker.internal 配合 host-gateway 更可靠。
方法对比
| 方法 | macOS/Windows | Linux | 推荐用途 | 网络隔离 |
|---|---|---|---|---|
host.docker.internal(Docker) | ✅ 默认支持 | ✅ 配置 extra_hosts 后可用 | 大多数场景 | ✅ 良好 |
host.containers.internal(Podman) | ✅ 默认支持 | ✅ 默认支持(4.1+) | 大多数场景 | ✅ 良好 |
--network=host | ❌ 文中环境不支持 | ✅ 可用 | 性能测试 | ❌ 无 |
桥接网关 IP(172.17.0.1) | ❌ 不适用 | ✅ 可用 | 仅 Linux 的环境 | ✅ 良好 |
实践建议
- 使用运行时对应的 DNS 名称:Docker 用
host.docker.internal,Podman 用host.containers.internal。 - 在 docker-compose.yml 中添加
extra_hosts:让 Linux 系统上的行为保持一致。 - 避免写死 IP:用环境变量提供连接字符串。
- 按需使用 host 网络:将它留给有明确需求的场景。
- 跨平台测试:团队成员使用不同操作系统时,分别确认连接能否建立。
故障排查
无法解析 host.docker.internal 或 host.containers.internal
症状: curl: (6) Could not resolve host: host.docker.internal
Linux 上 Docker 的处理方法:
extra_hosts:
- "host.docker.internal:host-gateway"
验证:
# Docker
docker run --rm --add-host=host.docker.internal:host-gateway alpine ping -c 3 host.docker.internal
# Podman (should work out of the box on 4.1+)
podman run --rm alpine ping -c 3 host.containers.internal
旧版本 Podman 的处理方法:
使用低于 4.1 的 Podman 时,可以升级版本,或者手动添加宿主机映射:
podman run --add-host=host.containers.internal:host-gateway your-image
连接被拒绝
症状: curl: (7) Failed to connect to host.docker.internal port 3000: Connection refused
可能的原因:
- 宿主机上的服务没有运行。
- 服务只监听
127.0.0.1(回环地址)。 - 防火墙拦截了容器到宿主机的连接。
处理方法: 确认需要从容器访问的宿主机服务绑定到 0.0.0.0,而非只绑定到 127.0.0.1:
// ❌ This won't work from containers
app.listen(3000, '127.0.0.1');
// ✅ This will work
app.listen(3000, '0.0.0.0');
防火墙问题(Linux)
如果 Linux 环境中的连接受到拦截:
# Check if firewall is blocking Docker
sudo iptables -L -n | grep DOCKER
# Temporarily disable firewall for testing
sudo systemctl stop firewalld # CentOS/RHEL
sudo ufw disable # Ubuntu/Debian
安全方面的考虑
- 避免暴露敏感服务:生产环境中的数据库或管理界面不要绑定到
0.0.0.0。 - 使用网络策略:在生产环境配置适当的 Docker 网络策略和防火墙。
- 限制 host 网络的使用:确有必要时才使用
--network=host。 - 监控容器网络访问:记录并审计容器到宿主机服务的连接。
参考资料
- Docker Documentation. (2025). Networking overview. Retrieved December 22, 2025, from https://docs.docker.com/network/
- Docker Documentation. (2025). Use host networking. Retrieved December 22, 2025, from https://docs.docker.com/network/host/
- Docker Compose Documentation. (2025). Compose file version 3 reference. Retrieved December 22, 2025, from https://docs.docker.com/compose/compose-file/compose-file-v3/