docker容器端口映射
Docker
Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器或Windows 机器上,也可以实现虚拟化,容器是完全使用沙箱机制,相互之间不会有任何接口。
docker端口映射
端口映射是NAT地址转换的一种,它可以把在公网的地址转翻译成私有地址,采用路由方式的ADSL宽带路由器拥有一个动态或固定的公网IP。分为动态和静态两种 。
端口映射功能可以让内部网络中某台机器对外部提供WWW服务,这不是将真P地址直接转到内部提供www服务的主机。如果这样的话,有两个弊端,一是内部机器不安全,因为除了WWW之外,外部网络可以通过地址转换功能访问到这台机器的所有功能;二是当有多台机器需要提供这种服务时,必须有同样多的P地址进行转换,从而达不到节省P地址的目的。端口映射功能是将一台主机的假P地址映射成一个真P地址,当用户访问提供映射端口主机的某个端口时,服务器将请求转到内部提供这种特定服务的主机;利用端口映射功能还可以将一台真P地址机器的多个端口映射成内部不同机器上的不同端口。端口映射功能还可以完成一些特定的代理功能,比如代理POP、SMP、 TELNET等协议。理论上可以提供6万多个端口的映射,恐怕我们永远都用不完。
1.docker run -d -p 80:80 nginx:latest
docker端口映射
1.docker run -d -p 80:80 nginx:latest
2.iptables查看规则
[root@docker03 ~]# iptables -t nat -L -nChain PREROUTING (policy ACCEPT)target prot opt source destination DOCKER all -- 0.0.0.0/0 0.0.0.0/0 ADDRTYPE match dst-type LOCALChain INPUT (policy ACCEPT)target prot opt source destination Chain OUTPUT (policy ACCEPT)target prot opt source destination DOCKER all -- 0.0.0.0/0 !127.0.0.0/8 ADDRTYPE match dst-type LOCALChain POSTROUTING (policy ACCEPT)target prot opt source destination MASQUERADE all -- 172.17.0.0/16 0.0.0.0/0 MASQUERADE tcp -- 172.17.0.2 172.17.0.2 tcp dpt:80 Chain DOCKER (2 references)target prot opt source destination RETURN all -- 0.0.0.0/0 0.0.0.0/0 DNAT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 to:172.17.0.2:80
3.查看内核地址转发参数
[root@docker03 ~]# sysctl -a | grep ipv4|grep ip_forwardnet.ipv4.ip_forward = 1 (默认是1,当容器启动变成1)net.ipv4.ip_forward_use_pmtu = 0sysctl: reading key "net.ipv6.conf.all.stable_secret"sysctl: reading key "net.ipv6.conf.default.stable_secret"sysctl: reading key "net.ipv6.conf.docker0.stable_secret"sysctl: reading key "net.ipv6.conf.eth0.stable_secret"sysctl: reading key "net.ipv6.conf.eth2.stable_secret"sysctl: reading key "net.ipv6.conf.lo.stable_secret"sysctl: reading key "net.ipv6.conf.veth93eb530.stable_secret"
4.基于IP地址绑定同一个端口
[root@docker03 ~]# docker run -d -p 10.0.0.13:80:80 nginx:latest e7aa9f43ee1291c1dc57f3fdbd6b144896df862571002bd64ca061fd4e646d9c[root@docker03 ~]# docker run -d -p 10.0.0.110:80:80 nginx:latest 22e0d4ce1a264b0538651659f538361f4c54926bdea64ef9d937b47a86d812e1[root@docker03 ~]# netstat -lntupActive Internet connections (only servers)Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 10.0.0.110:80 0.0.0.0:* LISTEN 20210/docker-proxy tcp 0 0 10.0.0.13:80 0.0.0.0:* LISTEN 20114/docker-proxy
5.宿主机随机端口映射到容器端口
[root@docker03 ~]# docker run -d -p 10.0.0.13::80 nginx:latest 3f3a37db1b6b0c4dddbc18f358ef8282f54e93fc06aeb96d80c76b9a806e1bf1[root@docker03 ~]# netstat -lntupActive Internet connections (only servers)Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp6 0 0 :::32768 :::* LISTEN 22649/docker-proxy
6.基于UDP端口映射
[root@docker03 ~]# docker run -d -p ::80/udp nginx:latest 0b4d888d4afb9b81a71a205375b23c9e68d514070d391e44370da5381b2c000b[root@docker03 ~]# netstat -lntupActive Internet connections (only servers)Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp6 0 0 :::32768 :::* LISTEN 22649/docker-proxy udp6 0 0 :::32768 :::* 22926/docker-proxy
7.多端口映射
docker run -d -p 80:80 -p 3306:3306 nginx:latest
8.端口范围映射(端口数量一致)
[root@docker03 ~]# docker run -d -p 1111-1119:1110-1118 nginx:latest 06d03e55631c4e550898bae551b95b28074758093c1272746a1f2ccb781d2a3a
2.iptables查看规则
[root@docker03 ~]# iptables -t nat -L -nChain PREROUTING (policy ACCEPT)target prot opt source destination DOCKER all -- 0.0.0.0/0 0.0.0.0/0 ADDRTYPE match dst-type LOCALChain INPUT (policy ACCEPT)target prot opt source destination Chain OUTPUT (policy ACCEPT)target prot opt source destination DOCKER all -- 0.0.0.0/0 !127.0.0.0/8 ADDRTYPE match dst-type LOCALChain POSTROUTING (policy ACCEPT)target prot opt source destination MASQUERADE all -- 172.17.0.0/16 0.0.0.0/0 MASQUERADE tcp -- 172.17.0.2 172.17.0.2 tcp dpt:80 Chain DOCKER (2 references)target prot opt source destination RETURN all -- 0.0.0.0/0 0.0.0.0/0 DNAT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 to:172.17.0.2:80
3.查看内核地址转发参数
[root@docker03 ~]# sysctl -a | grep ipv4|grep ip_forwardnet.ipv4.ip_forward = 1 (默认是1,当容器启动变成1)net.ipv4.ip_forward_use_pmtu = 0sysctl: reading key "net.ipv6.conf.all.stable_secret"sysctl: reading key "net.ipv6.conf.default.stable_secret"sysctl: reading key "net.ipv6.conf.docker0.stable_secret"sysctl: reading key "net.ipv6.conf.eth0.stable_secret"sysctl: reading key "net.ipv6.conf.eth2.stable_secret"sysctl: reading key "net.ipv6.conf.lo.stable_secret"sysctl: reading key "net.ipv6.conf.veth93eb530.stable_secret"
4.基于IP地址绑定同一个端口
[root@docker03 ~]# docker run -d -p 10.0.0.13:80:80 nginx:latest e7aa9f43ee1291c1dc57f3fdbd6b144896df862571002bd64ca061fd4e646d9c[root@docker03 ~]# docker run -d -p 10.0.0.110:80:80 nginx:latest 22e0d4ce1a264b0538651659f538361f4c54926bdea64ef9d937b47a86d812e1[root@docker03 ~]# netstat -lntupActive Internet connections (only servers)Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 10.0.0.110:80 0.0.0.0:* LISTEN 20210/docker-proxy tcp 0 0 10.0.0.13:80 0.0.0.0:* LISTEN 20114/docker-proxy
5.宿主机随机端口映射到容器端口
[root@docker03 ~]# docker run -d -p 10.0.0.13::80 nginx:latest 3f3a37db1b6b0c4dddbc18f358ef8282f54e93fc06aeb96d80c76b9a806e1bf1[root@docker03 ~]# netstat -lntupActive Internet connections (only servers)Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp6 0 0 :::32768 :::* LISTEN 22649/docker-proxy
6.基于UDP端口映射
[root@docker03 ~]# docker run -d -p ::80/udp nginx:latest 0b4d888d4afb9b81a71a205375b23c9e68d514070d391e44370da5381b2c000b[root@docker03 ~]# netstat -lntupActive Internet connections (only servers)Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp6 0 0 :::32768 :::* LISTEN 22649/docker-proxy udp6 0 0 :::32768 :::* 22926/docker-proxy
7.多端口映射
docker run -d -p 80:80 -p 3306:3306 nginx:latest
8.端口范围映射(端口数量一致)
[root@docker03 ~]# docker run -d -p 1111-1119:1110-1118 nginx:latest 06d03e55631c4e550898bae551b95b28074758093c1272746a1f2ccb781d2a3a