您好,登錄后才能下訂單哦!
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 -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.查看內核地址轉發參數
[root@docker03 ~]# sysctl -a | grep ipv4|grep ip_forward
net.ipv4.ip_forward = 1 (默認是1,當容器啟動變成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.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 -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.宿主機隨機端口映射到容器端口
[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.基于UDP端口映射
[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.多端口映射
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 -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.查看內核地址轉發參數
[root@docker03 ~]# sysctl -a | grep ipv4|grep ip_forward
net.ipv4.ip_forward = 1 (默認是1,當容器啟動變成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.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 -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.宿主機隨機端口映射到容器端口
[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.基于UDP端口映射
[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.多端口映射
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
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。