docker port mapping

docker port mapping

1.docker run -d -p 80:80 nginx:latest
docker port mapping

1.docker run -d -p 80:80 nginx:latest

2.iptables viewing rules

[root@docker03 ~]# iptables -t nat -L -n
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         
DOCKER     all  --  0.0.0.0/0            0.0.0.0/0            ADDRTYPE match dst-type LOCAL

Chain 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 LOCAL

Chain 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. View kernel address forwarding parameters

[root@docker03 ~]# sysctl -a | grep ipv4|grep ip_forward
net.ipv4.ip_forward = 1  (The default is 1. When the container starts, it becomes 1)
net.ipv4.ip_forward_use_pmtu = 0
sysctl: 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.eth1.stable_secret"
sysctl: reading key "net.ipv6.conf.lo.stable_secret"
sysctl: reading key "net.ipv6.conf.veth93eb530.stable_secret"

4. Bind the same port based on IP address

[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 -lntup
Active 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. Host random port mapped to container port

[root@docker03 ~]# docker run -d -p 10.0.0.13::80 nginx:latest 
3f3a37db1b6b0c4dddbc18f358ef8282f54e93fc06aeb96d80c76b9a806e1bf1
[root@docker03 ~]# netstat -lntup
Active 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. Based on UDP port mapping

[root@docker03 ~]# docker run -d -p ::80/udp nginx:latest 
0b4d888d4afb9b81a71a205375b23c9e68d514070d391e44370da5381b2c000b
[root@docker03 ~]# netstat -lntup
Active 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. Multi port mapping

docker run -d -p 80:80  -p  3306:3306 nginx:latest

8. Port range mapping (consistent number of ports)

[root@docker03 ~]# docker run -d -p 1111-1119:1110-1118 nginx:latest 
06d03e55631c4e550898bae551b95b28074758093c1272746a1f2ccb781d2a3a

2.iptables viewing rules

[root@docker03 ~]# iptables -t nat -L -n
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         
DOCKER     all  --  0.0.0.0/0            0.0.0.0/0            ADDRTYPE match dst-type LOCAL

Chain 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 LOCAL

Chain 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. View kernel address forwarding parameters

[root@docker03 ~]# sysctl -a | grep ipv4|grep ip_forward
net.ipv4.ip_forward = 1  (The default is 1. When the container starts, it becomes 1)
net.ipv4.ip_forward_use_pmtu = 0
sysctl: 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.eth1.stable_secret"
sysctl: reading key "net.ipv6.conf.lo.stable_secret"
sysctl: reading key "net.ipv6.conf.veth93eb530.stable_secret"

4. Bind the same port based on IP address

[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 -lntup
Active 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. Host random port mapped to container port

[root@docker03 ~]# docker run -d -p 10.0.0.13::80 nginx:latest 
3f3a37db1b6b0c4dddbc18f358ef8282f54e93fc06aeb96d80c76b9a806e1bf1
[root@docker03 ~]# netstat -lntup
Active 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. Based on UDP port mapping

[root@docker03 ~]# docker run -d -p ::80/udp nginx:latest 
0b4d888d4afb9b81a71a205375b23c9e68d514070d391e44370da5381b2c000b
[root@docker03 ~]# netstat -lntup
Active 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. Multi port mapping

docker run -d -p 80:80  -p  3306:3306 nginx:latest

8. Port range mapping (consistent number of ports)

[root@docker03 ~]# docker run -d -p 1111-1119:1110-1118 nginx:latest 
06d03e55631c4e550898bae551b95b28074758093c1272746a1f2ccb781d2a3a

Tags: Linux Docker Nginx iptables

Posted on Tue, 07 Jan 2020 09:42:16 -0800 by ehask