
Docker Networking
- 10 installs
- 2 repo stars
- Updated July 29, 2026
- full-statck-skills/docker-skills
Configure Docker networking and inter-container communication across bridge, host, and overlay modes, with port mapping and network troubleshooting.
About
Guides Docker networking configuration and inter-container communication across network modes and port mapping. A developer uses it to connect containers or debug networking issues.
- Network modes: bridge, host, overlay, none, macvlan, ipvlan with DNS resolution
- Port publishing, service discovery, and network troubleshooting with inspect/ping/nslookup
Docker Networking by the numbers
- 10 all-time installs (skills.sh)
- Ranked #1,007 of 1,435 DevOps & CI/CD skills by installs in the Skillselion catalog
- Data as of Jul 30, 2026 (Skillselion catalog sync)
npx skills add https://github.com/full-statck-skills/docker-skills --skill docker-networkingAdd your badge
Show developers this skill is listed on Skillselion. Paste this into your README.
| Installs | 10 |
|---|---|
| repo stars | ★ 2 |
| Last updated | July 29, 2026 |
| Repository | full-statck-skills/docker-skills ↗ |
What it does
Configure Docker networking and inter-container communication across bridge, host, and overlay modes, with port mapping and network troubleshooting.
Files
Docker Networking — 网络配置与容器通信
Comprehensive guidance for Docker networking modes, configuration, and troubleshooting.
When to Use
ALWAYS use this skill when the user mentions:
- "docker network", "容器网络"
- "bridge network", "host network", "overlay network"
- "端口映射", "publish port"
- "容器之间怎么通信", "跨容器通信"
- "网络不通", "network troubleshooting"
Network Mode Comparison
| Mode | Isolation | Use Case | Cross-Host? |
|---|---|---|---|
| bridge (default) | Container-level | Single-host containers | ❌ |
| host | None (share host) | Max performance, no isolation | ❌ |
| overlay | Container-level | Swarm multi-host | ✅ |
| none | Full | No networking needed | ❌ |
| macvlan/ipvlan | MAC/IP-level | Legacy apps needing physical IP | ✅ |
Custom Bridge Network (Recommended)
# Create a custom network
docker network create --driver bridge mynet
# Run containers on it
docker run -d --name app1 --network mynet nginx:alpine
docker run -d --name app2 --network mynet nginx:alpine
# DNS resolution works automatically
docker exec app1 ping app2 # ✅ responds
docker exec app1 nslookup app2 # ✅ resolves
# Benefits over default bridge:
# - Automatic DNS resolution
# - Container isolation from default bridge
# - Can connect/disconnect at runtimePort Publishing
# Basic port mapping
docker run -p 8080:80 nginx # host:8080 → container:80
docker run -p 8080 # random host port → container:8080
docker run -P nginx # publish all EXPOSE'd ports to random host ports
# Multiple ports
docker run -p 8080:80 -p 8443:443 nginx
# Bind to specific interface
docker run -p 127.0.0.1:8080:80 nginx # only localhostService Discovery
Custom bridge network provides automatic DNS:
┌──────────────────────────────────┐
│ my-network (bridge) │
│ │
│ ┌──────┐ ┌──────┐ ┌──────┐ │
│ │ app1 │ │ app2 │ │ db │ │
│ └──┬───┘ └──┬───┘ └──┬───┘ │
│ │ │ │ │
│ └─── DNS: app2 → 172.18.0.3 │
│ DNS: db → 172.18.0.4 │
└──────────────────────────────────┘
app1 can connect to db:5432 directly — no IP needed!Overlay Network (Swarm)
# Create overlay for multi-host
docker network create --driver overlay --attachable my-overlay
# In compose:
networks:
backend:
driver: overlayNetwork Troubleshooting
# Inspect network details
docker network inspect mynet
# Test connectivity from container
docker exec app1 ping -c 3 app2
docker exec app1 nslookup db
docker exec app1 curl http://app2:8080/health
# Check iptables (host)
sudo iptables -t nat -L DOCKER -nWorkflow — 推荐配置流程
Step 1: 确定需求: 容器间通信?端口暴露?跨主机?选择网络模式 Step 2: 创建网络: docker network create --driver bridge mynet Step 3: 启动容器: docker run -d --network mynet --name db mysql + docker run -d --network mynet --name api myapp Step 4: 验证连通性: docker exec api ping db(服务名 DNS 解析) Step 5: 排查问题: docker network inspect mynet → docker exec api nslookup db
Gotchas — Common Pitfalls
- Default bridge no DNS: Containers on the default bridge CANNOT resolve each other by name. → Recovery:
docker network create mynet && docker run --network mynet ...; always use custom bridge networks. - Port already in use:
-p 8080:80fails if host port 8080 is taken. → Recovery:lsof -i :8080to find the process; use-p 8081:80or-p 80for random host port. - Mac host networking limitation:
--network hoston Docker Desktop (Mac) joins the VM's network, not the Mac's. → Recovery: Publish ports with-pinstead of--network host. - Overlay without Swarm: Overlay networks only work with Swarm mode. → Recovery:
docker swarm initfirst, or add--attachableflag for standalone containers. - EXPOSE does NOT publish:
EXPOSEin Dockerfile is documentation only. → Recovery: Always use-por Composeports:to actually make ports accessible.
Boundary — 能力边界(适用与不适用场景)
| 分类 | 场景 | 说明 |
|---|---|---|
| ✅ 能做 | 单机容器网络配置 | bridge/host/none 模式选择 |
| ✅ 能做 | 容器间 DNS 通信 | 自定义 bridge 网络 + 服务名解析 |
| ✅ 能做 | 端口发布 | -p host:container 端口映射 |
| ⚠️ 需条件 | 跨主机通信 | 需要 overlay 网络 + Swarm 模式 |
| ⚠️ 需条件 | macOS --network host | 行为不同于 Linux,建议用 -p 替代 |
| ❌ 超范围 | K8s 网络策略(NetworkPolicy) | 使用 docker-production + K8s 文档 |
| ❌ 超范围 | 外部负载均衡(Nginx/HAProxy) | 基础设施层级 |
| ❌ 超范围 | iptables/nftables 精细配置 | Linux 网络管理 |
When NOT to Use This Skill
| ❌ Skip | ✅ Use Instead |
|---|---|
| Docker basics | docker-basics |
| Running containers | docker-run |
| Compose networking | docker-compose (compose handles network creation automatically) |
| Kubernetes networking | K8s documentation |
Security & Stability
- Use
--internalflag for networks that should not have internet access. - Encrypt overlay network traffic:
--opt encrypted=true. - Avoid
--network hostin production — no network isolation. - No executable scripts bundled. Guidance only.
📚 官方文档参考
| 文档 | 地址 |
|---|---|
| Docker 网络概述 | https://docs.docker.com/network/ |
| docker network 命令 | https://docs.docker.com/reference/cli/docker/network/ |
| 网络驱动 | https://docs.docker.com/network/drivers/ |
| Bridge 网络 | https://docs.docker.com/network/bridge/ |
| Overlay 网络 | https://docs.docker.com/network/overlay/ |
| 端口发布 | https://docs.docker.com/network/#published-ports |
🧭 Docker Skills Journey
📍 You are here: `docker-networking` — 网络配置
← Previous: docker-run | → Next: docker-compose
FAQ
Q1: 如何快速上手此技能? A: 参考上方的快速开始章节,按步骤操作即可。
Q2: 遇到版本不兼容问题怎么办? A: 检查依赖版本,使用 lock 文件锁定,参考常见陷阱章节。
Q3: 如何在生产环境使用? A: 参考最佳实践章节,确保配置正确,做好监控和日志。
Q4: 性能如何优化? A: 参考性能优化相关文档,使用缓存、索引等手段。
Q5: 如何贡献或反馈问题? A: 在 GitHub 仓库提交 Issue 或 Pull Request。
Q6: 是否支持中文? A: 支持中文文档和中文注释,详见国内适配章节。
Web + Database on custom network
# 1. Create custom network
docker network create app-net
# 2. Start database
docker run -d --name db --network app-net -e POSTGRES_PASSWORD=secret -v pgdata:/var/lib/postgresql/data postgres:16-alpine
# 3. Start web app (connects to db by name!)
docker run -d --name web --network app-net -e DATABASE_URL=postgres://postgres:secret@db:5432/mydb -p 8080:8080 myapp:latest
# 4. Verify: web can reach db via DNS
docker exec web ping -c 2 db # ✅
docker exec web nslookup db # ✅ resolves to 172.x.x.x自定义网络:多容器 DNS 互通
# 创建自定义 bridge 网络
docker network create --driver bridge myapp-net
# 运行数据库
docker run -d --name db \
--network myapp-net \
-e MYSQL_ROOT_PASSWORD=secret \
mysql:8.4
# 运行应用(通过服务名 db 连接数据库)
docker run -d --name api \
--network myapp-net \
-p 8080:8080 \
-e DB_HOST=db \
-e DB_PORT=3306 \
myapp:latest
# 验证 DNS 解析
docker exec api ping db
docker exec api nslookup db # 返回 db 容器的 IP动态连接/断开网络
# 运行时不指定网络
docker run -d --name redis redis:7-alpine
# 动态连接到网络
docker network connect myapp-net redis
# 断开
docker network disconnect myapp-net redis查看网络详情
docker network ls
docker network inspect myapp-net
# 输出:
# {
# "Containers": {
# "abc...": { "Name": "db", "IPv4Address": "172.18.0.2/16" },
# "def...": { "Name": "api", "IPv4Address": "172.18.0.3/16" }
# }
# }
Docker Network Modes
| Mode | Command | DNS | Cross-Host | Use Case |
|---|---|---|---|---|
| bridge (default) | — | ❌ | ❌ | Legacy single-host |
| custom bridge | --network mynet | ✅ | ❌ | Single-host recommended |
| host | --network host | ❌ | ❌ | Max performance (no isolation) |
| overlay | --network my-overlay | ✅ | ✅ | Swarm multi-host |
| none | --network none | ❌ | ❌ | No network needed |
| macvlan | --network macnet | ❌ | ✅ | Legacy apps needing physical IP |
Port Mapping Reference
-p HOST_PORT:CONTAINER_PORT[/PROTOCOL]
# Map container port 80 to host port 8080
docker run -p 8080:80 nginx
# Map to random host port
docker run -p 80 nginx
docker port <container> # See which host port was assigned
# Protocol-specific
docker run -p 8080:80/tcp -p 8080:80/udp nginx
# Bind to specific interface
docker run -p 127.0.0.1:8080:80 nginx # localhost only
docker run -p 0.0.0.0:8080:80 nginx # all interfaces
# Publish all EXPOSE'd ports to random host ports
docker run -P nginxIn compose.yml
ports:
- "8080:80" # host:container
- "127.0.0.1:8443:443" # bind to localhost
- "8080:8080/udp" # UDP网络排查四步诊断法
步骤 1:inspect 收集信息
# 查看容器网络配置
docker inspect --format='{{json .NetworkSettings.Networks}}' web | jq .
# 查看网关和 IP
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}} {{.Gateway}}{{end}}' web
# 查看 DNS 配置
docker exec web cat /etc/resolv.conf步骤 2:连通性测试
# 容器间 ping
docker exec web ping api
# 端口测试
docker exec web nc -zv db 3306
# DNS 解析
docker exec web nslookup api
docker exec web nslookup api.myapp-net # 完整域名步骤 3:iptables 检查
# 查看 NAT 规则(端口映射)
sudo iptables -t nat -L DOCKER -n
# 查看 FORWARD 规则
sudo iptables -L DOCKER-USER -n步骤 4:抓包分析
# 抓取容器网卡流量
docker run --rm --net container:web nicolaka/netshoot \
tcpdump -i eth0 port 80常见问题速查
| 症状 | 可能原因 | 解决 |
|---|---|---|
| 容器间 ping 通但端口不通 | 防火墙/应用未监听 0.0.0.0 | netstat -tlnp 检查监听地址 |
| 服务名无法解析 | 未加入同一自定义网络 | docker network connect |
localhost 访问失败 | 容器中 localhost=本容器 | 使用 host.docker.internal 或服务名 |
| 端口映射无效 | 端口已被占用或被防火墙拦截 | lsof -i :PORT 检查 |
| 容器无法出网 | IP forwarding 未开启 | sysctl net.ipv4.ip_forward=1 |
| Docker DNS 失败 | /etc/resolv.conf 被覆盖 | 使用 --dns 参数指定 |
| 跨主机不通 | overlay 网络未配置 | 检查 Swarm 网络状态 |