1、运行了一个centos7的容器,22端口映射给宿主机5002端口:
$ docker ps |grep centos
7e5d1e9a4add centosssh "/usr/sbin/sshd -D" 5 weeks ago Up 5 weeks 0.0.0.0:5002->22/tcp, :::5002->22/tcp sharp_neumann
2、进入容器部署了应用,应用占用8888端口,准备映射给宿主机的8888端口:
iptables -t nat --list-rules DOCKER
iptables -t nat -A DOCKER ! -i docker0 -p tcp -m tcp --dport 8888 -j DNAT --to-destination 172.17.0.2:8888
iptables -t nat --list-rules POSTROUTING
iptables -t nat -A POSTROUTING -s 172.17.0.2/32 -d 172.17.0.2/32 -p tcp -m tcp --dport 8888 -j MASQUERADE
iptables --list-rules DOCKER
iptables -t filter -A DOCKER -d 172.17.0.2/32 ! -i docker0 -o docker0 -p tcp -m tcp --dport 8888 -j ACCEPT