docker容器网络配置
Linux内核实现名称空间的创建
ip netns命令
可以借助ip netns命令来完成对 Network Namespace 的各种操作。ip netns命令来自于iproute安装包,一般系统会默认安装,如果没有的话,请自行安装。
注意:ip netns命令修改网络配置时需要 sudo 权限。
可以通过ip netns命令完成对Network Namespace 的相关操作,可以通过ip netns help查看命令帮助信息:
[root@Raygussie ~]# ip netns help
Usage: ip netns list
ip netns add NAME
ip netns set NAME NETNSID
ip [-all] netns delete [NAME]
ip netns identify [PID]
ip netns pids NAME
ip [-all] netns exec [NAME] cmd ...
ip netns monitor
ip netns list-id
默认情况下,Linux系统中是没有任何 Network Namespace的,所以ip netns list命令不会返回任何信息。
创建Network Namespace
通过命令创建一个名为ns1的命名空间:
[root@Raygussie ~]# ip netns list
[root@Raygussie ~]# ip netns add ns1
[root@Raygussie ~]# ip netns list
ns1
新创建的 Network Namespace 会出现在/var/run/netns/目录下。如果相同名字的 namespace 已经存在,命令会报Cannot create namespace file "/var/run/netns/ns0": File exists的错误。
[root@Raygussie ~]# ls /var/run/netns
ns1
[root@Raygussie ~]# ip netns add ns1
Cannot create namespace file "/var/run/netns/ns1": File exists
对于每个 Network Namespace 来说,它会有自己独立的网卡、路由表、ARP 表、iptables 等和网络相关的资源。
操作Network Namespace
ip命令提供了ip netns exec
子命令可以在对应的 Network Namespace 中执行命令。
查看新创建 Network Namespace 的网卡信息
[root@Raygussie ~]# ip netns exec ns1 ip a
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
//启动网卡
[root@Raygussie ~]# ip netns exec ns1 ip link set lo up
[root@Raygussie ~]# ip netns exec ns1 ip a
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
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
可以看到,新创建的Network Namespace中会默认创建一个lo回环网卡,此时网卡处于关闭状态。此时,尝试去 ping 该lo回环网卡,会提示Network is unreachable
[root@Raygussie ~]# ip netns exec ns2 ip a
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
[root@Raygussie ~]# ip netns exec ns2 ping 127.0.0.1
connect: Network is unreachable
通过下面的命令启用lo回环网卡:
[root@Raygussie ~]# ip netns exec ns2 ip link set lo up
[root@Raygussie ~]# ip netns exec ns1 ping 127.0.0.1
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.114 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.033 ms
^C
--- 127.0.0.1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 13ms
rtt min/avg/max/mdev = 0.033/0.073/0.114/0.041 ms
转移设备
我们可以在不同的 Network Namespace 之间转移设备(如veth)。由于一个设备只能属于一个 Network Namespace ,所以转移后在这个 Network Namespace 内就看不到这个设备了。
其中,veth设备属于可转移设备,而很多其它设备(如lo、vxlan、ppp、bridge等)是不可以转移的。
veth pair
veth pair 全称是 Virtual Ethernet Pair,是一个成对的端口,所有从这对端口一 端进入的数据包都将从另一端出来,反之也是一样。
引入veth pair是为了在不同的 Network Namespace 直接进行通信,利用它可以直接将两个 Network Namespace 连接起来。
创建veth pair
[root@Raygussie ~]# ip link add type veth
[root@Raygussie ~]# ip link show
。。。。。。
4: veth0@veth1: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
link/ether 0a:c8:2f:b4:cc:9a brd ff:ff:ff:ff:ff:ff
5: veth1@veth0: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
link/ether f2:6e:d1:9b:08:8f brd ff:ff:ff:ff:ff:ff
可以看到,此时系统中新增了一对veth pair,将veth0和veth1两个虚拟网卡连接了起来,此时这对 veth pair 处于”未启用“状态。
实现Network Namespace间通信
创建两个Network Namespace,一个命名为ns1,一个为ns2
[root@Raygussie ~]# ip netns add ns1
Cannot create namespace file "/var/run/netns/ns1": File exists
[root@Raygussie ~]# ip netns list
ns2
ns1
将veth0加入到ns1,将veth1加入到ns2
[root@Raygussie ~]# ip link set veth0 netns ns1
[root@Raygussie ~]# ip netns exec ns1 ip a
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
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
4: veth0@if5: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
link/ether 0a:c8:2f:b4:cc:9a brd ff:ff:ff:ff:ff:ff link-netnsid 0
//将veth1加入到ns2
[root@Raygussie ~]# ip link set veth1 netns ns2
[root@Raygussie ~]# ip netns exec ns2 ip a
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
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
5: veth1@if4: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
link/ether f2:6e:d1:9b:08:8f brd ff:ff:ff:ff:ff:ff link-netns ns1
//分别为这对veth pair配置上ip地址,并启动
[root@Raygussie ~]# ip netns exec ns1 ip addr add 1.1.1.1/8 dev veth0
[root@Raygussie ~]# ip netns exec ns1 ip link set veth0 up
[root@Raygussie ~]# ip netns exec ns2 ip addr add 1.1.1.2/8 dev veth1
[root@Raygussie ~]# ip netns exec ns2 ip link set veth1 up
//查看这对veth pair的状态
[root@Raygussie ~]# ip netns exec ns1 ip a
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
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
4: veth0@if5: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether 0a:c8:2f:b4:cc:9a brd ff:ff:ff:ff:ff:ff link-netns ns2
inet 1.1.1.1/8 scope global veth0
valid_lft forever preferred_lft forever
inet6 fe80::8c8:2fff:feb4:cc9a/64 scope link
valid_lft forever preferred_lft forever
[root@Raygussie ~]# ip netns exec ns2 ip a
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
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
5: veth1@if4: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether f2:6e:d1:9b:08:8f brd ff:ff:ff:ff:ff:ff link-netns ns1
inet 1.1.1.2/8 scope global veth1
valid_lft forever preferred_lft forever
inet6 fe80::f06e:d1ff:fe9b:88f/64 scope link
valid_lft forever preferred_lft forever
//用ns2命名空间来ping通ns1命名空间
[root@Raygussie ~]# ip netns exec ns2 ping 1.1.1.1
PING 1.1.1.1 (1.1.1.1) 56(84) bytes of data.
64 bytes from 1.1.1.1: icmp_seq=1 ttl=64 time=0.142 ms
64 bytes from 1.1.1.1: icmp_seq=2 ttl=64 time=0.113 ms
64 bytes from 1.1.1.1: icmp_seq=3 ttl=64 time=0.040 ms
^C
--- 1.1.1.1 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 57ms
rtt min/avg/max/mdev = 0.040/0.098/0.142/0.043 ms
//veth pair成功实现了两个不同Network Namespace之间的网络交互
veth设备重命名
[root@Raygussie ~]# ip netns exec ns1 ip link set veth0 down
[root@Raygussie ~]# ip netns exec ns1 ip link set dev veth0 name eth0
[root@Raygussie ~]# ip netns exec ns1 ip link set eth0 up
[root@Raygussie ~]# ip netns exec ns1 ip a
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
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
4: eth0@if5: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether 0a:c8:2f:b4:cc:9a brd ff:ff:ff:ff:ff:ff link-netns ns2
inet 1.1.1.1/8 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::8c8:2fff:feb4:cc9a/64 scope link
valid_lft forever preferred_lft forever
四种网络模式配置
bridge模式配置
[root@Raygussie ~]# docker run -it --rm --name mf busybox
/ # ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:AC:11:00:02
inet addr:172.17.0.2 Bcast:172.17.255.255 Mask:255.255.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:7 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:586 (586.0 B) TX bytes:0 (0.0 B)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
/ # ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue 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
10: eth0@if11: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
/ # exit
[root@Raygussie ~]# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
//在创建容器时添加--network bridge与不加--network选项效果是一致的
[root@Raygussie ~]# docker run -it --rm --network bridge busybox
/ # ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue 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
8: eth0@if9: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
link/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.3/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
/ # ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:AC:11:00:03
inet addr:172.17.0.3 Bcast:172.17.255.255 Mask:255.255.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:11 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:866 (866.0 B) TX bytes:0 (0.0 B)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
/ # exit
none模式配置
没网络的,不能和外部通讯
[root@Raygussie ~]# docker run -it --rm --network none busybox
/ # ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue 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
/ # exit
container模式配置
//启动第一个容器,名字为mf
[root@Raygussie ~]# docker run -it --rm --name mf busybox
/ # ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue 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
12: eth0@if13: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
/ #
//启动第二个容器,名字为quin
[root@Raygussie ~]# docker run -it --rm --name quin busybox
/ # ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue 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
14: eth0@if15: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
link/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.3/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
/ # exit
//退出quin容器后再与mf容器共享ip
[root@Raygussie ~]# docker run -it --rm --name quin --network container:mf busybox/ # ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue 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
12: eth0@if13: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
/ #
//在mf容器上创建一个目录
/ # mkdir data
/ # ls
bin data dev etc home proc root sys tmp usr var
/ #
//到quin容器发现没有这个目录,因为文件系统是处于隔离状态,仅仅是共享了网络而已。
/ # ls
bin dev etc home proc root sys tmp usr var
//在quin容器上部署一个站点
/ # echo 'helly world' > /tmp/index.html
/ # netstat -antl
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
/ # httpd -h /tmp/
/ # netstat -antl
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 :::80 :::* LISTEN
/ #
//在mf容器上用本地地址去访问此站点
/ # wget -O - 172.17.0.2
Connecting to 172.17.0.2 (172.17.0.2:80)
writing to stdout
helly world
- 100% |**********************************| 12 0:00:00 ETA
written to stdout
host模式配置(真正意义上的桥接模式)
[root@Raygussie ~]# docker run -it --name mf --rm --network host busybox
/ # ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue 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
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq qlen 1000
link/ether 00:0c:29:d4:5d:1d brd ff:ff:ff:ff:ff:ff
inet 192.168.186.130/24 brd 192.168.186.255 scope global dynamic ens160
valid_lft 1707sec preferred_lft 1707sec
inet6 fe80::9102:9b4e:7d85:d875/64 scope link
valid_lft forever preferred_lft forever
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue
link/ether 02:42:93:27:a7:67 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
valid_lft forever preferred_lft forever
inet6 fe80::42:93ff:fe27:a767/64 scope link
valid_lft forever preferred_lft forever
/ # ls /tmp/
/ # echo'hello world' > /tmp/index.html
sh: echohello world: not found
/ # echo 'hello world' > /tmp/index.html
/ # httpd -h /tmp/
/ # netstat -antl
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 192.168.186.130:22 192.168.186.1:54890 ESTABLISHED
tcp 0 0 192.168.186.130:22 192.168.186.1:54792 ESTABLISHED
tcp 0 0 192.168.186.130:22 192.168.186.1:55009 ESTABLISHED
tcp 0 0 192.168.186.130:22 192.168.186.1:53856 ESTABLISHED
tcp 0 0 :::22 :::* LISTEN
tcp 0 0 :::80 :::* LISTEN
/ #
此时如果我们在这个容器中启动一个http站点,我们就可以直接用宿主机的IP直接在浏览器中访问这个容器中的站点了。
容器的常用操作
查看容器的主机名
[root@Raygussie ~]# docker run -it --rm --name mf busybox
/ # hostname
359356ebbcbd
/ #
在容器启动时注入主机名
[root@Raygussie ~]# docker run -it --rm --hostname mfdsg busybox
/ # hostname
mfdsg
/ # cat /etc/hosts //自动写上去的
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2 mfdsg //注入主机名时会自动创建主机名到IP的映射关系
/ # ping mfdsg
PING mfdsg (172.17.0.2): 56 data bytes
64 bytes from 172.17.0.2: seq=0 ttl=64 time=0.063 ms
64 bytes from 172.17.0.2: seq=1 ttl=64 time=0.153 ms
64 bytes from 172.17.0.2: seq=2 ttl=64 time=0.114 ms
^C
--- mfdsg ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 0.063/0.110/0.153 ms
/ # cat /etc/hostname
mfdsg
手动指定容器要使用的DNS
[root@localhost ~]# docker run -it --rm --hostname quin --dns 114.114.114.114 busybox
/ # cat /etc/resolv.conf
search localdomain
nameserver 114.114.114.114
/ # hostname
quin
/ # ping www.baidu.com
PING www.baidu.com (182.61.200.7): 56 data bytes
64 bytes from 182.61.200.7: seq=0 ttl=127 time=42.637 ms
64 bytes from 182.61.200.7: seq=1 ttl=127 time=24.979 ms
64 bytes from 182.61.200.7: seq=2 ttl=127 time=26.879 ms
^C
--- www.baidu.com ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 24.979/31.498/42.637 ms
/ #
手动往/etc/hosts文件中注入主机名到IP地址的映射
[root@Raygussie ~]# docker run -it --rm --hostname quin --dns 114.114.114.114 --add-host quin2:172.17.0.2 busybox
/ # cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2 quin2
172.17.0.3 quin
/ # ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue 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
10: eth0@if11: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
link/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.3/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
/ # ping quin
PING quin (172.17.0.3): 56 data bytes
64 bytes from 172.17.0.3: seq=0 ttl=64 time=0.100 ms
64 bytes from 172.17.0.3: seq=1 ttl=64 time=0.040 ms
^C
--- quin ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.040/0.070/0.100 ms
/ # ping quin2
PING quin2 (172.17.0.2): 56 data bytes
64 bytes from 172.17.0.2: seq=0 ttl=64 time=0.153 ms
64 bytes from 172.17.0.2: seq=1 ttl=64 time=0.078 ms
^C
--- quin2 ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.078/0.115/0.153 ms
/ #
开放容器端口
执行docker run的时候有个-p选项,可以将容器中的应用端口映射到宿主机中,从而实现让外部主机可以通过访问宿主机的某端口来访问容器内应用的目的。
-p选项能够使用多次,其所能够暴露的端口必须是容器确实在监听的端口。
-p选项的使用格式:
- -p <containerPort>
- 将指定的容器端口映射至主机所有地址的一个动态端口
- -p <hostPort>:<containerPort>
- 将容器端口<containerPort>映射至指定的主机端口<hostPort>
- -p <ip>::<containerPort>
- 将指定的容器端口<containerPort>映射至主机指定<ip>的动态端口
- -p <ip>:<hostPort>:<containerPort>
- 将指定的容器端口<containerPort>映射至主机指定<ip>的端口<hostPort>
动态端口指的是随机端口,具体的映射结果可使用docker port命令查看。
-p <containerPort>
将指定的容器端口映射至主机所有地址的一个动态端口
[root@Raygussie ~]# docker run -itd -p 80 httpd
6583addaa684bbebfaf54c9f16e6e26018adacaf4fc9f45e1a7253f08ed3ad60
[root@Raygussie ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6583addaa684 httpd "httpd-foreground" 9 seconds ago Up 7 seconds 0.0.0.0:49154->80/tcp silly_wilbur
[root@Raygussie ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:49154 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
[root@Raygussie ~]# curl 172.17.0.2
<html><body><h1>It works!</h1></body></html>
-p <hostPort>:<containerPort>
将容器端口<containerPort>映射至指定的主机端口<hostPort>
[root@Raygussie ~]# docker run -d -p 99:80 httpd
123d7a5c7c79eeeb9b6ff3dbe8ca3c11d5bc2012e0574898528a3bf1500f9e86
[root@Raygussie ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
123d7a5c7c79 httpd "httpd-foreground" 7 seconds ago Up 5 seconds 0.0.0.0:99->80/tcp adoring_visvesvaraya
[root@Raygussie ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:49154 0.0.0.0:*
LISTEN 0 128 0.0.0.0:99 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
[root@Raygussie ~]# curl 172.17.0.3
<html><body><h1>It works!</h1></body></html>
[root@Raygussie ~]# curl 192.168.186.130:99
<html><body><h1>It works!</h1></body></html>
[root@Raygussie ~]#
-p <ip>::<containerPort>
将指定的容器端口<containerPort>映射至主机指定<ip>的动态端口
[root@Raygussie ~]# docker run -d --rm -p 192.168.186.130::80 httpd
1409ead9ca616dd1594a48224590e8f49aa7d25a8a1c7bf79354e053b3b05ffd
[root@Raygussie ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1409ead9ca61 httpd "httpd-foreground" 10 seconds ago Up 7 seconds 192.168.186.130:49153->80/tcp infallible_thompson
123d7a5c7c79 httpd "httpd-foreground" 5 minutes ago Up 5 minutes 0.0.0.0:99->80/tcp adoring_visvesvaraya
6583addaa684 httpd "httpd-foreground" 17 minutes ago Up 17 minutes 0.0.0.0:49154->80/tcp silly_wilbur
[root@Raygussie ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 192.168.186.130:49153 0.0.0.0:*
LISTEN 0 128 0.0.0.0:49154 0.0.0.0:*
LISTEN 0 128 0.0.0.0:99 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
[root@Raygussie ~]# curl 172.17.0.4
<html><body><h1>It works!</h1></body></html>
[root@Raygussie ~]# curl 192.168.186.130:49153
<html><body><h1>It works!</h1></body></html>
-p <ip>:<hostPort>:<containerPort>
将指定的容器端口<containerPort>映射至主机指定<ip>的端口<hostPort>
[root@Raygussie ~]# docker run -d --rm -p 192.168.186.130:80:80 httpd
218ee35acadf0ca06a7453802f04a1a91a143d0d0c2520293432dc328bc6c8a0
[root@Raygussie ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
218ee35acadf httpd "httpd-foreground" 16 seconds ago Up 14 seconds 192.168.186.130:80->80/tcp distracted_easley
[root@Raygussie ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 192.168.186.130:49153 0.0.0.0:*
LISTEN 0 128 0.0.0.0:49154 0.0.0.0:*
LISTEN 0 128 0.0.0.0:99 0.0.0.0:*
LISTEN 0 128 192.168.186.130:80 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
[root@Raygussie ~]# curl 172.17.0.5
<html><body><h1>It works!</h1></body></html>
[root@Raygussie ~]# curl 192.168.186.130:80
<html><body><h1>It works!</h1></body></html>
动态端口指的是随机端口,具体的映射结果可使用docker port命令查看。
[root@Raygussie ~]# docker port 218ee35acadf
80/tcp -> 192.168.186.130:80
iptables防火墙规则将随容器的创建自动生成,随容器的删除自动删除规则。
将容器端口映射到指定IP的随机端口
[root@Raygussie ~]# iptables -t nat -nvL
Chain PREROUTING (policy ACCEPT 33 packets, 2506 bytes)
pkts bytes target prot opt in out source destination
14 728 DOCKER all -- * * 0.0.0.0/0 0.0.0.0/0 ADDRTYPE match dst-type LOCAL
Chain INPUT (policy ACCEPT 5 packets, 260 bytes)
pkts bytes target prot opt in out source destination
Chain POSTROUTING (policy ACCEPT 26 packets, 2368 bytes)
pkts bytes target prot opt in out source destination
6 404 MASQUERADE all -- * !docker0 172.17.0.0/16 0.0.0.0/0
0 0 MASQUERADE tcp -- * * 172.17.0.2 172.17.0.2 tcp dpt:80
0 0 MASQUERADE tcp -- * * 172.17.0.3 172.17.0.3 tcp dpt:80
0 0 MASQUERADE tcp -- * * 172.17.0.4 172.17.0.4 tcp dpt:80
0 0 MASQUERADE tcp -- * * 172.17.0.5 172.17.0.5 tcp dpt:80
Chain OUTPUT (policy ACCEPT 21 packets, 2076 bytes)
pkts bytes target prot opt in out source destination
3 180 DOCKER all -- * * 0.0.0.0/0 !127.0.0.0/8 ADDRTYPE match dst-type LOCAL
Chain DOCKER (2 references)
pkts bytes target prot opt in out source destination
0 0 RETURN all -- docker0 * 0.0.0.0/0 0.0.0.0/0
4 208 DNAT tcp -- !docker0 * 0.0.0.0/0 0.0.0.0/0 tcp dpt:49154 to:172.17.0.2:80
0 0 DNAT tcp -- !docker0 * 0.0.0.0/0 0.0.0.0/0 tcp dpt:99 to:172.17.0.3:80
0 0 DNAT tcp -- !docker0 * 0.0.0.0/0 192.168.186.130 tcp dpt:49153 to:172.17.0.4:80
0 0 DNAT tcp -- !docker0 * 0.0.0.0/0 192.168.186.130 tcp dpt:80 to:172.17.0.5:80
docker ps -q 只取出容器ID
[root@Raygussie ~]# docker ps -q
218ee35acadf
1409ead9ca61
123d7a5c7c79
6583addaa684
[root@Raygussie ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
218ee35acadf httpd "httpd-foreground" 14 minutes ago Up 14 minutes 192.168.186.130:80->80/tcp distracted_easley
1409ead9ca61 httpd "httpd-foreground" 18 minutes ago Up 18 minutes 192.168.186.130:49153->80/tcp infallible_thompson
123d7a5c7c79 httpd "httpd-foreground" 23 minutes ago Up 23 minutes 0.0.0.0:99->80/tcp adoring_visvesvaraya
6583addaa684 httpd "httpd-foreground" 35 minutes ago Up 35 minutes 0.0.0.0:49154->80/tcp silly_wilbur
停下所有容器
[root@Raygussie ~]# docker stop $(docker ps -q)
218ee35acadf
1409ead9ca61
123d7a5c7c79
6583addaa684
[root@Raygussie ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@Raygussie ~]#
删除所有容器
[root@Raygussie ~]# docker rm -f $(docker ps -aq)
自定义docker0桥的网络属性信息
官方文档 https://docs.docker.com/network/bridge/
自定义docker0桥的网络属性信息需要修改/etc/docker/daemon.json
配置文件
[root@Raygussie ~]# vi /etc/docker/daemon.json
{
"bip": "192.168.30.1/24",
"registry-mirrors": ["https://zyva0762.mirror.aliyuncs.com"]
}
[root@Raygussie ~]# systemctl daemon-reload
[root@Raygussie ~]# systemctl restart docker
[root@Raygussie ~]# ip a
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
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:d4:5d:1d brd ff:ff:ff:ff:ff:ff
inet 192.168.186.130/24 brd 192.168.186.255 scope global dynamic noprefixroute ens160
valid_lft 1758sec preferred_lft 1758sec
inet6 fe80::9102:9b4e:7d85:d875/64 scope link noprefixroute
valid_lft forever preferred_lft forever
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
link/ether 02:42:63:f4:d0:a1 brd ff:ff:ff:ff:ff:ff
inet 192.168.30.1/24 brd 192.168.30.255 scope global docker0
valid_lft forever preferred_lft forever
[root@Raygussie ~]# docker run -it --rm busybox
/ # ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue 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
4: eth0@if5: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
link/ether 02:42:c0:a8:1e:02 brd ff:ff:ff:ff:ff:ff
inet 192.168.30.2/24 brd 192.168.30.255 scope global eth0
valid_lft forever preferred_lft forever
/ # route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.30.1 0.0.0.0 UG 0 0 0 eth0
192.168.30.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
/ #
核心选项为bip,即bridge ip之意,用于指定docker0桥自身的IP地址;其它选项可通过此地址计算得出。
docker远程连接
dockerd守护进程的C/S,其默认仅监听Unix Socket格式的地址(/var/run/docker.sock),如果要使用TCP套接字,则需要修改/etc/docker/daemon.json配置文件,添加如下内容,然后重启docker服务:
"hosts": ["tcp://0.0.0.0:2375", "unix:///var/run/docker.sock"]
在客户端上向dockerd直接传递“-H|--host”选项指定要控制哪台主机上的docker容器
docker -H 192.168.10.145:2375 ps
docker创建自定义桥
//创建一个网桥,区别于docker0,名字mf1
[root@localhost ~]# docker run -it --rm --name mf1 busybox
/ # ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue 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
11: eth0@if12: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
link/ether 02:42:c0:a8:1e:02 brd ff:ff:ff:ff:ff:ff
inet 192.168.30.2/24 brd 192.168.30.255 scope global eth0
valid_lft forever preferred_lft forever
/ #
//在另一个终端创建名字为mf2的网桥
[root@localhost ~]# docker run -it --rm --name mf2 --network br1 busybox
/ # ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue 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
13: eth0@if14: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
link/ether 02:42:c0:a8:01:02 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.2/24 brd 192.168.1.255 scope global eth0
valid_lft forever preferred_lft forever
/ # ping 192.168.30.2
PING 192.168.30.2 (192.168.30.2): 56 data bytes
^C
--- 192.168.30.2 ping statistics ---
2 packets transmitted, 0 packets received, 100% packet loss
/ #
//把mf1加到br1网络里
[root@localhost ~]# docker network connect br1 mf1
//在mf1里查看ip,出现了192.168.1.3
/ # ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue 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
11: eth0@if12: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
link/ether 02:42:c0:a8:1e:02 brd ff:ff:ff:ff:ff:ff
inet 192.168.30.2/24 brd 192.168.30.255 scope global eth0
valid_lft forever preferred_lft forever
15: eth1@if16: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
link/ether 02:42:c0:a8:01:03 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.3/24 brd 192.168.1.255 scope global eth1
valid_lft forever preferred_lft forever
//ping通mf2容器
/ # ping 192.168.1.2
PING 192.168.1.2 (192.168.1.2): 56 data bytes
64 bytes from 192.168.1.2: seq=0 ttl=64 time=0.456 ms
64 bytes from 192.168.1.2: seq=1 ttl=64 time=0.168 ms
64 bytes from 192.168.1.2: seq=2 ttl=64 time=0.126 ms
^C
--- 192.168.1.2 ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 0.126/0.250/0.456 ms
//在mf2里ping通mf1
/ # ping 192.168.1.3
PING 192.168.1.3 (192.168.1.3): 56 data bytes
64 bytes from 192.168.1.3: seq=0 ttl=64 time=0.843 ms
64 bytes from 192.168.1.3: seq=1 ttl=64 time=0.116 ms
^C
--- 192.168.1.3 ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.116/0.479/0.843 ms
//把mf2加到bridge网络里
[root@localhost ~]# docker network connect bridge mf2
//查看mf2的ip,出现了192.168.30.3
/ # ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue 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
7: eth0@if8: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
link/ether 02:42:c0:a8:01:02 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.2/24 brd 192.168.1.255 scope global eth0
valid_lft forever preferred_lft forever
11: eth1@if12: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
link/ether 02:42:c0:a8:1e:03 brd ff:ff:ff:ff:ff:ff
inet 192.168.30.3/24 brd 192.168.30.255 scope global eth1
valid_lft forever preferred_lft forever
/ #
//在mf2里ping通192.168.30.2
/ # ping 192.168.30.2
PING 192.168.30.2 (192.168.30.2): 56 data bytes
64 bytes from 192.168.30.2: seq=0 ttl=64 time=0.210 ms
64 bytes from 192.168.30.2: seq=1 ttl=64 time=0.276 ms
^C
--- 192.168.30.2 ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.210/0.243/0.276 ms
/ #
//由此,mf1容器与mf2容器的网络连接成功
下图里的c1与c2容器是刚才的mf1和mf2