Files
nexus/wiki/端口映射.md
2026-04-22 04:03:04 +08:00

44 lines
1.7 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 端口映射
## Type
- Concept
## Definition
端口映射Port Mapping是 Docker 容器网络配置的核心机制,通过 `-p host:container` 语法将容器内部端口暴露到宿主机网络,使得外部流量可以通过宿主机 IP:端口 访问容器内的服务。
## Mechanism
### 基本语法
```yaml
ports:
- "9091:9091" # host:containerTCP
- "51413:51413/udp" # 支持协议指定
```
### 映射规则
| 格式 | 说明 | 适用场景 |
|------|------|---------|
| `host:container` | 指定宿主机和容器端口 | 生产环境 |
| `host:container/protocol` | 指定协议TCP/UDP | BT下载、语音等 |
| `:container` | 宿主机随机端口 | 开发环境 |
| `host-ip:host:container` | 绑定特定IP | 多网卡服务器 |
## Key Claims
- Docker 通过 iptables 规则在宿主机层面实现端口映射转发
- 桥接网络bridge模式下端口映射生效host 网络模式下端口映射被忽略
- 容器重启后端口映射保持(配置持久化在 Docker daemon
- 宿主机端口冲突会导致容器启动失败
## Relationship to [[桥接网络]]
端口映射是 [[桥接网络]] 模式的核心功能:
- **host 网络模式**:容器直接共享宿主机网络命名空间,端口映射无效(服务直接在宿主机端口监听)
- **bridge 网络模式**(默认):容器有独立网络命名空间,通过 NAT 规则实现端口映射访问
## Relationship to [[Transmission]]
Transmission 依赖端口映射实现两个关键功能:
- Web UI 访问:`9091:9091` → 用户通过浏览器访问 http://host:9091
- BT Peer 通信:`51413:51413` + `51413:51413/udp` → 其他BT客户端发现并连接
## Sources
- [[用docker安装transmission]]