docker_practice/advanced_network/port_mapping.md

58 lines
1.9 KiB
Go
Raw Normal View History

# 宿
2014-09-05 15:50:54 +08:00
2014-09-18 17:38:20 +08:00
访访
2017-11-26 11:28:55 +08:00
## 访
2017-11-26 11:28:55 +08:00
NAT IP 使 `iptables`
2014-09-05 15:50:54 +08:00
NAT
2017-11-26 11:28:55 +08:00
2017-11-22 11:13:23 +08:00
```bash
2014-09-18 17:38:20 +08:00
$ sudo iptables -t nat -nL
2014-09-05 15:50:54 +08:00
...
Chain POSTROUTING (policy ACCEPT)
target prot opt source destination
MASQUERADE all -- 172.17.0.0/16 !172.17.0.0/16
...
```
2017-11-26 11:28:55 +08:00
`172.17.0.0/16` MASQUERADE SNAT
2014-09-05 15:50:54 +08:00
## 访
2014-09-05 15:50:54 +08:00
访 `docker run` `-p` `-P`
2014-09-18 17:38:20 +08:00
`iptable` nat
2014-09-18 17:38:20 +08:00
使 `-P`
2017-11-26 11:28:55 +08:00
2017-11-22 11:13:23 +08:00
```bash
2014-09-18 17:38:20 +08:00
$ iptables -t nat -nL
2014-09-05 15:50:54 +08:00
...
Chain DOCKER (2 references)
target prot opt source destination
DNAT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:49153 to:172.17.0.2:80
2014-09-18 17:38:20 +08:00
```
使 `-p 80:80`
2017-11-26 11:28:55 +08:00
2017-11-22 11:13:23 +08:00
```bash
2014-09-18 17:38:20 +08:00
$ iptables -t nat -nL
2014-09-05 15:50:54 +08:00
Chain DOCKER (2 references)
target prot opt source destination
DNAT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 to:172.17.0.2:80
```
2017-11-26 11:28:55 +08:00
2014-09-05 15:50:54 +08:00
2017-11-26 11:28:55 +08:00
2017-11-29 10:23:42 +08:00
* `0.0.0.0` `-p IP:host_port:container_port` `-p IP::port` 访 IP
2017-11-26 11:28:55 +08:00
2017-11-29 10:23:42 +08:00
* IP Docker `/etc/docker/daemon.json`
```json
{
"ip": "0.0.0.0"
}
```