Docker管理-容器的网络映射
2020-04-17 本文已影响0人
复苏的兵马俑
1、容器的网络访问
[root@Server ~]# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@Server ~]# docker container run -it --name="centos01" centos:8.1.1911
[root@80281d5bfaf7 /]#
[root@Server ~]# docker container run -it --name="centos02" centos:8.1.1911
[root@8c6d819522be /]#
[root@Server ~]# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8c6d819522be centos:8.1.1911 "/bin/bash" 21 seconds ago Up 19 seconds centos02
80281d5bfaf7 centos:8.1.1911 "/bin/bash" 40 seconds ago Up 39 seconds centos01
[root@80281d5bfaf7 /]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group 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
44: eth0@if45: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
[root@8c6d819522be /]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group 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
46: eth0@if47: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.17.0.3/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
[root@80281d5bfaf7 /]# ping 172.17.0.3 -c 4
PING 172.17.0.3 (172.17.0.3) 56(84) bytes of data.
64 bytes from 172.17.0.3: icmp_seq=1 ttl=64 time=0.124 ms
64 bytes from 172.17.0.3: icmp_seq=2 ttl=64 time=0.105 ms
64 bytes from 172.17.0.3: icmp_seq=3 ttl=64 time=0.096 ms
64 bytes from 172.17.0.3: icmp_seq=4 ttl=64 time=0.089 ms
--- 172.17.0.3 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 84ms
rtt min/avg/max/mdev = 0.089/0.103/0.124/0.016 ms
[root@8c6d819522be /]# ping 172.17.0.2 -c 4
PING 172.17.0.2 (172.17.0.2) 56(84) bytes of data.
64 bytes from 172.17.0.2: icmp_seq=1 ttl=64 time=0.086 ms
64 bytes from 172.17.0.2: icmp_seq=2 ttl=64 time=0.094 ms
64 bytes from 172.17.0.2: icmp_seq=3 ttl=64 time=0.087 ms
64 bytes from 172.17.0.2: icmp_seq=4 ttl=64 time=0.087 ms
--- 172.17.0.2 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 69ms
rtt min/avg/max/mdev = 0.086/0.088/0.094/0.009 ms
2、容器的端口映射
A)docker container run -p hostPort:containerPort ...
[root@Server ~]# docker container run -d -p 8080:80 --name="nginx01" nginx:1.17
7b19f6018f189eeba6fea3213e0345d09aa96354f7c62d9f3cc9e72773050781
[root@Server ~]# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7b19f6018f18 nginx:1.17 "nginx -g 'daemon of…" 20 minutes ago Up 20 minutes 0.0.0.0:8080->80/tcp nginx01
8c6d819522be centos:8.1.1911 "/bin/bash" 44 minutes ago Up 44 minutes centos02
80281d5bfaf7 centos:8.1.1911 "/bin/bash" 45 minutes ago Up 45 minutes centos01
B)docker container run -p ip:hostPort:containerPort ...
[root@Server ~]# docker container run -d -p 192.168.28.8:8081:80 --name="nginx02" nginx:1.17
984de8c0b76822d9ef51090771e6a23320c10c5062dfe8d798dff4a856b445a5
[root@Server ~]# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
984de8c0b768 nginx:1.17 "nginx -g 'daemon of…" 19 minutes ago Up 19 minutes 192.168.28.8:8081->80/tcp nginx02
7b19f6018f18 nginx:1.17 "nginx -g 'daemon of…" 20 minutes ago Up 20 minutes 0.0.0.0:8080->80/tcp nginx01
8c6d819522be centos:8.1.1911 "/bin/bash" 44 minutes ago Up 44 minutes centos02
80281d5bfaf7 centos:8.1.1911 "/bin/bash" 45 minutes ago Up 45 minutes centos01
C)docker container run -p ip::containerPort ...
[root@Server ~]# docker container run -d -p 192.168.28.8::80 --name="nginx03" nginx:1.17
2c8a30cc4ec816cc6cde4934608ee58d28232dd5efbe1072c26dec07ac2b4f83
[root@Server ~]# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2c8a30cc4ec8 nginx:1.17 "nginx -g 'daemon of…" 7 minutes ago Up 7 minutes 192.168.28.8:32768->80/tcp nginx03
984de8c0b768 nginx:1.17 "nginx -g 'daemon of…" 8 minutes ago Up 8 minutes 192.168.28.8:8081->80/tcp nginx02
7b19f6018f18 nginx:1.17 "nginx -g 'daemon of…" 10 minutes ago Up 10 minutes 0.0.0.0:8080->80/tcp nginx01
8c6d819522be centos:8.1.1911 "/bin/bash" 34 minutes ago Up 34 minutes centos02
80281d5bfaf7 centos:8.1.1911 "/bin/bash" 34 minutes ago Up 34 minutes centos01
D)docker container run -p hostPort:containerPort/udp ...
[root@Server ~]# docker container run -d -p 8082:53/udp --name="nginx04" nginx:1.17
42b7ac1346b3ef82678bdac2d5b8c35d2195a6ac84d265a0148f6ad17b61089f
[root@Server ~]# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
42b7ac1346b3 nginx:1.17 "nginx -g 'daemon of…" 8 seconds ago Up 6 seconds 80/tcp, 0.0.0.0:8082->53/udp nginx04
2c8a30cc4ec8 nginx:1.17 "nginx -g 'daemon of…" 18 minutes ago Up 18 minutes 192.168.28.8:32768->80/tcp nginx03
984de8c0b768 nginx:1.17 "nginx -g 'daemon of…" 19 minutes ago Up 19 minutes 192.168.28.8:8081->80/tcp nginx02
7b19f6018f18 nginx:1.17 "nginx -g 'daemon of…" 20 minutes ago Up 20 minutes 0.0.0.0:8080->80/tcp nginx01
8c6d819522be centos:8.1.1911 "/bin/bash" 44 minutes ago Up 44 minutes centos02
80281d5bfaf7 centos:8.1.1911 "/bin/bash" 45 minutes ago Up 45 minutes centos01
E)docker container run -p hostPort:containerPort -p hostPort:containerPort ...
[root@Server ~]# docker container run -d -p 8083:80 -p 33061:3306 -p 221:22 --name="nginx05" nginx:1.17
880a0f0e6ff0474bd9018936d6da43d9a11e183afc2b0079557161d5ec75ba25
[root@Server ~]# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
880a0f0e6ff0 nginx:1.17 "nginx -g 'daemon of…" 6 seconds ago Up 5 seconds 0.0.0.0:221->22/tcp, 0.0.0.0:8083->80/tcp, 0.0.0.0:33061->3306/tcp nginx05
42b7ac1346b3 nginx:1.17 "nginx -g 'daemon of…" 4 minutes ago Up 4 minutes 80/tcp, 0.0.0.0:8082->53/udp nginx04
2c8a30cc4ec8 nginx:1.17 "nginx -g 'daemon of…" 22 minutes ago Up 22 minutes 192.168.28.8:32768->80/tcp nginx03
984de8c0b768 nginx:1.17 "nginx -g 'daemon of…" 23 minutes ago Up 23 minutes 192.168.28.8:8081->80/tcp nginx02
7b19f6018f18 nginx:1.17 "nginx -g 'daemon of…" 25 minutes ago Up 25 minutes 0.0.0.0:8080->80/tcp nginx01
8c6d819522be centos:8.1.1911 "/bin/bash" 49 minutes ago Up 49 minutes centos02
80281d5bfaf7 centos:8.1.1911 "/bin/bash" 49 minutes ago Up 49 minutes centos01
F)docker container run -p containerPort ...
[root@Server ~]# docker container run -d -p 80 --name="nginx06" nginx:1.17
d2ea174c01e9dceca0c01426ca7476aa7e5e130c7e003b63b3f38c5a32d7abd2
[root@Server ~]# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d2ea174c01e9 nginx:1.17 "nginx -g 'daemon of…" 3 seconds ago Up 2 seconds 0.0.0.0:32769->80/tcp nginx06
880a0f0e6ff0 nginx:1.17 "nginx -g 'daemon of…" About a minute ago Up About a minute 0.0.0.0:221->22/tcp, 0.0.0.0:8083->80/tcp, 0.0.0.0:33061->3306/tcp nginx05
42b7ac1346b3 nginx:1.17 "nginx -g 'daemon of…" 6 minutes ago Up 6 minutes 80/tcp, 0.0.0.0:8082->53/udp nginx04
2c8a30cc4ec8 nginx:1.17 "nginx -g 'daemon of…" 24 minutes ago Up 24 minutes 192.168.28.8:32768->80/tcp nginx03
984de8c0b768 nginx:1.17 "nginx -g 'daemon of…" 25 minutes ago Up 25 minutes 192.168.28.8:8081->80/tcp nginx02
7b19f6018f18 nginx:1.17 "nginx -g 'daemon of…" 26 minutes ago Up 26 minutes 0.0.0.0:8080->80/tcp nginx01
8c6d819522be centos:8.1.1911 "/bin/bash" 51 minutes ago Up 51 minutes centos02
80281d5bfaf7 centos:8.1.1911 "/bin/bash" 51 minutes ago Up 51 minutes centos01