[root@lianhua ~]$ docker network ls NETWORK ID NAME DRIVER SCOPE 1a779d0e62d5 bridge bridge local f0ae6387e721 host host local 6d565e9acb10 none null local
[root@lianhua ~]$ docker run -it --name demo0 httpd
[root@lianhua ~]$ docker inspect bridge [ { "Name": "bridge", "Id": "1a779d0e62d5a309e1e942862b76d69d4ba9ed9be9c7bcdc051e8de89b0cc3ee", "Created": "2020-08-26T00:06:03.910196776+08:00", "Scope": "local", "Driver": "bridge", "EnableIPv6": false, "IPAM": { "Driver": "default", "Options": null, "Config": [ { "Subnet": "172.17.0.0/16", "Gateway": "172.17.0.1" } ] }, "Internal": false, "Attachable": false, "Ingress": false, "ConfigFrom": { "Network": "" }, "ConfigOnly": false, "Containers": { "7be09e54b24c45100769e131b46259c519710785ccfb68afaa904a1114add9a1": { "Name": "demo0", "EndpointID": "98399b3c0560aac4ca63de9f79659176562406ac02d917c667852b9a863296bb", "MacAddress": "02:42:ac:11:00:02", "IPv4Address": "172.17.0.2/16", "IPv6Address": "" } }, "Options": { "com.docker.network.bridge.default_bridge": "true", "com.docker.network.bridge.enable_icc": "true", "com.docker.network.bridge.enable_ip_masquerade": "true", "com.docker.network.bridge.host_binding_ipv4": "0.0.0.0", "com.docker.network.bridge.name": "docker0", "com.docker.network.driver.mtu": "1500" }, "Labels": {} } ]
[root@lianhua ~]$ brctl show bridge name bridge id STP enabled interfaces docker0 8000.02426c5d38db no veth559f8be
[root@lianhua ~]$ docker exec -it demo0 /bin/bash bash-4.2$ ping 172.17.0.1 -c 3 PING 172.17.0.1 (172.17.0.1) 56(84) bytes of data. 64 bytes from 172.17.0.1: icmp_seq=1 ttl=64 time=0.049 ms 64 bytes from 172.17.0.1: icmp_seq=2 ttl=64 time=0.047 ms 64 bytes from 172.17.0.1: icmp_seq=3 ttl=64 time=0.053 ms --- 172.17.0.1 ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2001ms rtt min/avg/max/mdev = 0.047/0.049/0.053/0.008 ms bash-4.2$ ping 192.168.0.69 -c 3 PING 192.168.0.69 (192.168.0.69) 56(84) bytes of data. 64 bytes from 192.168.0.69: icmp_seq=1 ttl=64 time=0.040 ms 64 bytes from 192.168.0.69: icmp_seq=2 ttl=64 time=0.055 ms 64 bytes from 192.168.0.69: icmp_seq=3 ttl=64 time=0.052 ms --- 192.168.0.69 ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 1999ms rtt min/avg/max/mdev = 0.040/0.049/0.055/0.006 ms bash-4.2$ ping 10.57.***.*** -c 3 PING 10.57.***.*** (10.57.***.***) 56(84) bytes of data. 64 bytes from 10.57.***.***: icmp_seq=1 ttl=42 time=1.81 ms 64 bytes from 10.57.***.***: icmp_seq=2 ttl=42 time=1.59 ms 64 bytes from 10.57.***.***: icmp_seq=3 ttl=42 time=1.71 ms --- 10.57.***.*** ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2002ms rtt min/avg/max/mdev = 1.593/1.705/1.813/0.095 ms
[root@lianhua ~]$ iptables-save -A POSTROUTING -s 172.17.0.0/16 ! -o docker0 -j MASQUERADE
# 容器中 ping 外網 bash-4.2$ ping 10.57.***.*** -c 3 PING 10.57.***.*** (10.57.***.***) 56(84) bytes of data. 64 bytes from 10.57.***.***: icmp_seq=1 ttl=42 time=1.67 ms 64 bytes from 10.57.***.***: icmp_seq=2 ttl=42 time=1.59 ms 64 bytes from 10.57.***.***: icmp_seq=3 ttl=42 time=1.64 ms --- 10.57.***.*** ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2002ms rtt min/avg/max/mdev = 1.594/1.637/1.673/0.057 ms # 宿主機(虛擬機)上抓包 [root@lianhua ~]$ tcpdump -i eth0 -n icmp -vv tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes 22:03:18.733722 IP (tos 0x0, ttl 63, id 25663, offset 0, flags [DF], proto ICMP (1), length 84) 192.168.0.69 > 10.57.***.***: ICMP echo request, id 15, seq 1, length 64 22:03:18.735322 IP (tos 0x48, ttl 43, id 58922, offset 0, flags [none], proto ICMP (1), length 84) 10.57.***.*** > 192.168.0.69: ICMP echo reply, id 15, seq 1, length 64 22:03:19.735287 IP (tos 0x0, ttl 63, id 25731, offset 0, flags [DF], proto ICMP (1), length 84) 192.168.0.69 > 10.57.***.***: ICMP echo request, id 15, seq 2, length 64 22:03:19.736786 IP (tos 0x48, ttl 43, id 59208, offset 0, flags [none], proto ICMP (1), length 84) 10.57.***.*** > 192.168.0.69: ICMP echo reply, id 15, seq 2, length 64 22:03:20.736261 IP (tos 0x0, ttl 63, id 26101, offset 0, flags [DF], proto ICMP (1), length 84) 192.168.0.69 > 10.57.***.***: ICMP echo request, id 15, seq 3, length 64 22:03:20.737811 IP (tos 0x48, ttl 43, id 59632, offset 0, flags [none], proto ICMP (1), length 84) 10.57.***.*** > 192.168.0.69: ICMP echo reply, id 15, seq 3, length 64 # 外網 host 上抓包 [root@controller-2 admin(admin)]# tcpdump -i eth0 -n icmp -vv tcpdump: listening on vlan9, link-type EN10MB (Ethernet), capture size 262144 bytes 22:03:18.772846 IP (tos 0x48, ttl 42, id 25663, offset 0, flags [DF], proto ICMP (1), length 84) 10.183.**.*** > 10.57.***.***: ICMP echo request, id 15, seq 1, length 64 22:03:18.772890 IP (tos 0x48, ttl 64, id 58922, offset 0, flags [none], proto ICMP (1), length 84) 10.57.***.*** > 10.183.**.***: ICMP echo reply, id 15, seq 1, length 64 22:03:19.774331 IP (tos 0x48, ttl 42, id 25731, offset 0, flags [DF], proto ICMP (1), length 84) 10.183.**.*** > 10.57.***.***: ICMP echo request, id 15, seq 2, length 64 22:03:19.774358 IP (tos 0x48, ttl 64, id 59208, offset 0, flags [none], proto ICMP (1), length 84) 10.57.***.*** > 10.183.**.***: ICMP echo reply, id 15, seq 2, length 64 22:03:20.775339 IP (tos 0x48, ttl 42, id 26101, offset 0, flags [DF], proto ICMP (1), length 84) 10.183.**.*** > 10.57.***.***: ICMP echo request, id 15, seq 3, length 64 22:03:20.775390 IP (tos 0x48, ttl 64, id 59632, offset 0, flags [none], proto ICMP (1), length 84) 10.57.***.*** > 10.183.**.***: ICMP echo reply, id 15, seq 3, length 64
# 10.183.**.*** 是虛擬機所在 host 的網口 ip 地址
$ docker run -d -p 80 --name demo1 httpd 6070389f1362ef4ad6c6264077c4a47ffe8d9b2700c48e03afcb8afa5e92356c $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6070389f1362 httpd "httpd-foreground" 44 seconds ago Up 32 seconds 0.0.0.0:32768->80/tcp demo1
$ ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWNgroup default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether 02:42:ac:11:00:44 brd ff:ff:ff:ff:ff:ff inet 172.17.0.68/16 brd 172.17.255.255 scope global ens3 valid_lft forever preferred_lft forever inet6 fe80::42:acff:fe11:44/64 scope link valid_lft forever preferred_lft forever 3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default link/ether 02:42:b6:5f:a7:1b brd ff:ff:ff:ff:ff:ff inet 172.18.0.1/24 brd 172.18.0.255 scope global docker0 valid_lft forever preferred_lft forever inet6 fe80::42:b6ff:fe5f:a71b/64 scope link valid_lft forever preferred_lft forever 5: veth0541dca@if4: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default link/ether 2e:31:fe:95:a3:b8 brd ff:ff:ff:ff:ff:ff link-netnsid0 inet6 fe80::2c31:feff:fe95:a3b8/64 scope link valid_lft forever preferred_lft forever
$ ps -elf | grep docker-proxy | grep -v grep root /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port32768 -container-ip 172.18.0.2 -container-port 80
-A DOCKER ! -i docker0 -p tcp -m tcp --dport 32768 -j DNAT --to-destination 172.18.0.2:80