Linux内核实现名称空间的创建
ip netns命令
可以借助ip netns命令来完成对 Network Namespace 的各种操作。ip netns命令来自于iproute安装包,一般系统会默认安装,如果没有的话,请自行安装。
注意:ip netns命令修改网络配置时需要 sudo 权限。
可以通过ip netns命令完成对Network Namespace 的相关操作,可以通过ip netns help查看命令帮助信息:
[root@localhost ~]# ip netns help
Usage: ip netns list
ip netns add NAME
ip netns attach NAME PID
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
NETNSID := auto | POSITIVE-INT
默认情况下,Linux系统中是没有任何 Network Namespace的,所以ip netns list命令不会返回任何信息。
创建Network Namespace
通过命令创建一个名为aabb7的命名空间:
[root@localhost ~]# ip netns list
[root@localhost ~]# ip netns add aabb7
[root@localhost ~]# ip netns list
aabb7
新创建的 Network Namespace 会出现在/var/run/netns/目录下。如果相同名字的 namespace 已经存在,命令会报Cannot create namespace file “/var/run/netns/sb0”: File exists的错误。
[root@localhost ~]# ls /var/run/netns/
aabb7
[root@localhost ~]# ip netns add aabb7
Cannot create namespace file "/var/run/netns/aabb7": File exists
对于每个 Network Namespace 来说,它会有自己独立的网卡、路由表、ARP 表、iptables 等和网络相关的资源。
操作Network Namespace
ip命令提供了ip netns exec子命令可以在对应的 Network Namespace 中执行命令。
查看新创建 Network Namespace 的网卡信息
[root@localhost ~]# ip netns exec aabb7 ip addr
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
可以看到,新创建的Network Namespace中会默认创建一个lo回环网卡,此时网卡处于关闭状态。此时,尝试去 ping 该lo回环网卡,会提示Network is unreachable
[root@localhost ~]# ip netns exec aabb7 ping 127.0.0.1
connect: Network is unreachable
通过下面的命令启用lo回环网卡:
[root@localhost ~]# ip netns exec aabb7 ip link set lo up
[root@localhost ~]# ip netns exec aabb7 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.043 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.054 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.051 ms
64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.049 ms
64 bytes from 127.0.0.1: icmp_seq=5 ttl=64 time=0.049 ms
64 bytes from 127.0.0.1: icmp_seq=6 ttl=64 time=0.053 ms
64 bytes from 127.0.0.1: icmp_seq=7 ttl=64 time=0.058 ms
^C
--- 127.0.0.1 ping statistics ---
7 packets transmitted, 7 received, 0% packet loss, time 159ms
rtt min/avg/max/mdev = 0.043/0.051/0.058/0.004 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@localhost ~]# ip link add type veth
[root@localhost ~]# 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 fq_codel state UP group default qlen 1000
link/ether 00:0c:29:07:67:13 brd ff:ff:ff:ff:ff:ff
inet 192.168.111.141/24 brd 192.168.111.255 scope global noprefixroute ens160
valid_lft forever preferred_lft forever
inet6 fe80::386d:70f:1bdf:e99e/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:58:45:af:c9 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
4: veth0@veth1: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN group default qlen 1000
link/ether da:c0:fa:53:36:d9 brd ff:ff:ff:ff:ff:ff
5: veth1@veth0: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN group default qlen 1000
link/ether 1e:09:0d:6f:b5:9c brd ff:ff:ff:ff:ff:ff
可以看到,此时系统中新增了一对veth pair,将veth0和veth1两个虚拟网卡连接了起来,此时这对 veth pair 处于”未启用“状态。
实现Network Namespace间通信
下面我们利用veth pair实现两个不同的 Network Namespace 之间的通信。刚才我们已经创建了一个名为aabb7的 Network Namespace,下面再创建一个信息Network Namespace,命名为jjyy1
[root@localhost ~]# ip netns add jjyy1
[root@localhost ~]# ip netns list
jjyy1
aabb7
然后我们将veth0加入到aabb7,将veth1加入到jjyy1
[root@localhost ~]# ip link set veth0 netns aabb7
[root@localhost ~]# ip link set veth1 netns jjyy1
然后我们分别为这对veth pair配置上ip地址,并启用它们
[root@localhost ~]# ip netns exec aabb7 ip link set veth0 up
[root@localhost ~]# ip netns exec aabb7 ip addr add 192.168.111.155/24 dev veth0
[root@localhost ~]# ip netns exec jjyy1 ip link set lo up
[root@localhost ~]# ip netns exec jjyy1 ip link set veth1 up
[root@localhost ~]# ip netns exec jjyy1 ip addr add 192.168.111.156/24 dev veth1
查看这对veth pair的状态
[root@localhost ~]# ip netns exec aabb7 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 da:c0:fa:53:36:d9 brd ff:ff:ff:ff:ff:ff link-netns jjyy1
inet 192.168.111.155/24 scope global veth0
valid_lft forever preferred_lft forever
inet6 fe80::d8c0:faff:fe53:36d9/64 scope link
valid_lft forever preferred_lft forever
[root@localhost ~]# ip netns exec jjyy1 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 1e:09:0d:6f:b5:9c brd ff:ff:ff:ff:ff:ff link-netns aabb7
inet 192.168.111.156/24 scope global veth1
valid_lft forever preferred_lft forever
inet6 fe80::1c09:dff:fe6f:b59c/64 scope link
valid_lft forever preferred_lft forever
从上面可以看出,我们已经成功启用了这个veth pair,并为每个veth设备分配了对应的ip地址。我们尝试在jjyy1中访问aabb7中的ip地址:
[root@localhost ~]# ip netns exec jjyy1 ping 192.168.111.155
PING 192.168.111.155 (192.168.111.155) 56(84) bytes of data.
64 bytes from 192.168.111.155: icmp_seq=1 ttl=64 time=0.044 ms
64 bytes from 192.168.111.155: icmp_seq=2 ttl=64 time=0.054 ms
64 bytes from 192.168.111.155: icmp_seq=3 ttl=64 time=0.056 ms
64 bytes from 192.168.111.155: icmp_seq=4 ttl=64 time=0.058 ms
^C
--- 192.168.111.155 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 79ms
rtt min/avg/max/mdev = 0.044/0.053/0.058/0.005 ms
[root@localhost ~]# ip netns exec aabb7 ping 192.168.111.156
PING 192.168.111.156 (192.168.111.156) 56(84) bytes of data.
64 bytes from 192.168.111.156: icmp_seq=1 ttl=64 time=0.026 ms
64 bytes from 192.168.111.156: icmp_seq=2 ttl=64 time=0.058 ms
64 bytes from 192.168.111.156: icmp_seq=3 ttl=64 time=0.056 ms
64 bytes from 192.168.111.156: icmp_seq=4 ttl=64 time=0.061 ms
^C
--- 192.168.111.156 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 91ms
rtt min/avg/max/mdev = 0.026/0.050/0.061/0.015 ms
可以看到,veth pair成功实现了两个不同Network Namespace之间的网络交互。
veth设备重命名
[root@localhost ~]# ip netns exec aabb7 ip link set veth0 down
[root@localhost ~]# ip netns exec aabb7 ip link set dev veth0 name ens0
[root@localhost ~]# ip netns exec aabb7 ip link set ens0 up
[root@localhost ~]# ip netns exec aabb7 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: ens0@if5: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether da:c0:fa:53:36:d9 brd ff:ff:ff:ff:ff:ff link-netns jjyy1
inet 192.168.111.155/24 scope global ens0
valid_lft forever preferred_lft forever
inet6 fe80::d8c0:faff:fe53:36d9/64 scope link
valid_lft forever preferred_lft forever
四种网络模式配置
bridge模式配置
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
harry1004/mysql v1 bfa74e3222e9 46 hours ago 3.67GB
harry1004/nginx v1.1 7a837f83b9a7 2 days ago 576MB
centos latest 5d0da3dc9764 2 months ago 231MB
[root@localhost ~]# docker run -itd --name by --rm 5d0da3dc9764
7777965592a749b69c5025cb2b21e68d7a2ca2dd7ef6358668d80ceacf567ab4
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7777965592a7 5d0da3dc9764 "/bin/bash" 8 seconds ago Up 6 seconds by
373efde4145b centos "/bin/bash" 46 hours ago Exited (0) 40 hours ago php8
73481e8f4c33 centos "/bin/bash" 47 hours ago Exited (255) 22 minutes ago mysql
3c7370935253 centos "/bin/bash" 2 days ago Exited (0) 47 hours ago nginx
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7777965592a7 5d0da3dc9764 "/bin/bash" 19 seconds ago Up 17 seconds by
[root@localhost ~]# docker exec -it 7777965592a7 /bin/sh
sh-4.4# 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
6: eth0@if7: <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
sh-4.4# exit
exit
在创建容器时添加–network bridge与不加–network选项效果是一样的
[root@localhost ~]# docker run -itd --name by --network bridge d23834f29b38 /bin/sh
14938b7bdab6dce3ef69d777607184b7803e4bbc5020817e8c876099e5942b72
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
14938b7bdab6 d23834f29b38 "/bin/sh" 10 seconds ago Up 9 seconds by
[root@localhost ~]# docker exec -it 14938b7bdab6 /bin/sh
/ # 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
none模式
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
harry1004/mysql v1 bfa74e3222e9 47 hours ago 3.67GB
harry1004/nginx v1.1 7a837f83b9a7 2 days ago 576MB
busybox latest d23834f29b38 5 days ago 1.24MB
centos latest 5d0da3dc9764 2 months ago 231MB
[root@localhost ~]# docker run -it --name by --network none --rm d23834f29b38
/ # 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
container模式配置
启动第一个容器
[root@localhost ~]# docker run -it --name by1 --rm d23834f29b38
/ # 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
16: eth0@if17: <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
启动第二个容器
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
harry1004/mysql v1 bfa74e3222e9 47 hours ago 3.67GB
harry1004/nginx v1.1 7a837f83b9a7 2 days ago 576MB
busybox latest d23834f29b38 5 days ago 1.24MB
centos latest 5d0da3dc9764 2 months ago 231MB
[root@localhost ~]# docker run -it --name by2 --rm bfa74e3222e9
[root@21a3fc8ee3d8 /]# 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
18: eth0@if19: <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
可以看到名为by2的容器IP地址是172.17.0.3,与第一个容器的IP地址不是一样的,也就是说并没有共享网络,此时如果我们将第二个容器的启动方式改变一下,就可以使名为b2的容器IP与B1容器IP一致,也即共享IP,但不共享文件系统。
[root@localhost ~]# docker run -it --name by2 --rm --network container:by1 d23834f29b38
/ # 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
20: eth0@if21: <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
此时我们在by1上的tmp下创建一个data
/ # mkdir /tmp/data
/ # ls /tmp
data
/ #
到by2容器上检查/tmp目录会发现并没有这个目录,因为文件系统是处于隔离状态,仅仅是共享了网络而已。
/ # ls /tmp
/ #
在by2容器上部署一个站点
/ # echo "This is a jjyy" > /tmp/index.html
/ # ls /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 :::80 :::* LISTEN
在by1容器上用本地地址去访问此站点
/ # wget -qO - 127.0.0.1
This is a jjyy
/ #
由此可见,container模式下的容器间关系就相当于一台主机上的两个不同进程
host模式配置
启动容器时直接指明模式为host
[root@localhost ~]# docker run -it --name test --rm --network host busybox
/ # ifconfig
docker0 Link encap:Ethernet HWaddr 02:42:7F:8D:B3:8D
inet addr:172.17.0.1 Bcast:172.17.255.255 Mask:255.255.0.0
inet6 addr: fe80::42:7fff:fe8d:b38d/64 Scope:Link
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:49 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 B) TX bytes:6228 (6.0 KiB)
ens160 Link encap:Ethernet HWaddr 00:0C:29:1F:EB:07
inet addr:192.168.153.139 Bcast:192.168.153.255 Mask:255.255.255.0
inet6 addr: fe80::fded:f7d3:4269:f476/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:365889 errors:0 dropped:0 overruns:0 frame:0
TX packets:542487 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:30804932 (29.3 MiB) TX bytes:140902766 (134.3 MiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:24 errors:0 dropped:0 overruns:0 frame:0
TX packets:24 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:2040 (1.9 KiB) TX bytes:2040 (1.9 KiB)
/ # 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 127.0.0.1:631 0.0.0.0:* LISTEN
tcp 0 0 192.168.111.141:22 192.168.111.1:61431 ESTABLISHED
tcp 0 0 192.168.111.141:22 192.168.111.1:60069 ESTABLISHED
tcp 0 0 192.168.111.141:22 192.168.111.1:60068 ESTABLISHED
tcp 0 0 192.168.111.141:22 192.168.111.1:61437 ESTABLISHED
tcp 0 0 :::80 :::* LISTEN
tcp 0 0 :::22 :::* LISTEN
tcp 0 0 ::1:631 :::* LISTEN
[root@localhost ~]# curl 192.168.111.141:80
hello world
容器的常用操作
查看容器的主机名
[root@localhost ~]# docker run -it --name test --network bridge --rm busybox
/ # hostname
3aab638df1a9
在容器启动时加入主机名
[root@localhost ~]# docker run -it --name test --network bridge --hostname bravealove --rm busybox
/ # hostname
bravealove
/ # 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 bravealove # 注入主机名时会自动创建主机名到IP的映射关系
/ # cat /etc/resolv.conf
# Generated by NetworkManager
search localdomain
nameserver 192.168.111.2 # DNS也会自动配置为宿主机的DNS
/ # ping baidu.com
PING baidu.com (220.181.38.148): 56 data bytes
64 bytes from 220.181.38.148: seq=0 ttl=127 time=55.371 ms
64 bytes from 220.181.38.148: seq=1 ttl=127 time=53.319 ms
64 bytes from 220.181.38.148: seq=2 ttl=127 time=55.472 ms
手动往/etc/hosts文件中注入主机名到IP地址的映射
root@localhost ~]# docker run -it --name by --network bridge --hostname lyw --add-host www.jjyy.com:1.1.1.1 --rm d23834f29b38
/ # 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
1.1.1.1 www.jjyy.com
172.17.0.2 lyw
开放容器端口
执行docker run的时候有个-p选项,可以将容器中的应用端口映射到宿主机中,从而实现让外部主机可以通过访问宿主机的某端口来访问容器内应用的目的。
-p选项能够使用多次,其所能够暴露的端口必须是容器确实在监听的端口。
-p选项的使用格式:
- -p
- 将指定的容器端口映射至主机所有地址的一个动态端口
- -p :
- 将容器端口映射至指定的主机端口
- -p ::
- 将指定的容器端口映射至主机指定的动态端口
- -p ::
- 将指定的容器端口映射至主机指定的端口
动态端口指的是随机端口,具体的映射结果可使用docker port命令查看。
- 将指定的容器端口映射至主机指定的端口
[root@localhost ~]# docker run --name web --rm -p 80 nginx
以上命令执行后会一直占用着前端,我们新开一个终端连接来看一下容器的80端口被映射到了宿主机的什么端口上
[root@localhost ~]# docker port web
80/tcp -> 0.0.0.0:49157
80/tcp -> :::49157
由此可见,容器的80端口被暴露到了宿主机的49157端口上,此时我们在宿主机上访问一下这个端口看是否能访问到容器内的站点
[root@localhost ~]# curl http://127.0.0.1:49157
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
iptables防火墙规则将随容器的创建自动生成,随容器的删除自动删除规则。
将容器端口映射到指定IP的随机端口
[root@localhost ~]# docker run --name web --rm -p 192.168.111.141::80 nginx
在另一个终端上查看端口映射情况
[root@localhost ~]# docker port web
80/tcp -> 192.168.111.141:49153
[root@localhost ~]# curl http://192.168.111.141:49153
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
将容器端口映射到宿主机的指定端口
[root@localhost ~]# docker run --name web --rm -p 80:80 nginx
在另一个终端上查看端口映射情况
[root@localhost ~]# docker port web
80/tcp -> 0.0.0.0:80
80/tcp -> :::80
自定义docker0桥的网络属性信息
官方文档相关配置
自定义docker0桥的网络属性信息需要修改/etc/docker/daemon.json配置文件
[root@localhost ~]# vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://in3617d8.mirror.aliyuncs.com"],
"bip": "192.168.111.150/24"
}
[root@localhost ~]# systemctl restart docker.service
核心选项为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.111.143:2375 ps
这种方式现在基本上不怎么用,很少说用dockers远程连接去控制,所以了解一下就可以了,知道有这么一个功能就可以了
docker创建自定义桥
创建一个额外的自定义桥,区别于docker0
[root@localhost ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
66d6eade5c82 bridge bridge local
c81f7ace7dc6 host host local
621ead08fa64 none null local
[root@localhost ~]# docker network create -d bridge --subnet "192.168.1.0/24" --gateway "192.168.1.1" br0
1bf080ab11a845efa3eb687b1d81089460c28fb24d9b5ce0e00a6811c658e47a
[root@localhost ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
1bf080ab11a8 br0 bridge local
66d6eade5c82 bridge bridge local
c81f7ace7dc6 host host local
621ead08fa64 none null local
使用新创建的自定义桥来创建容器:
[root@localhost ~]# docker run -it --name by --network br0 --rm d23834f29b38
/ # 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
再创建一个容器,使用默认的bridge桥:
[root@localhost ~]# docker run -it --name by1 --rm d23834f29b38
/ # 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
9: eth0@if10: <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
试想一下,此时的by与by1能否互相通信?如果不能该如何实现通信?
我们可以把桥连接过去,br0是我们上传创建的,把默认桥连接到刚刚创建的桥,
[root@localhost ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
1bf080ab11a8 br0 bridge local
66d6eade5c82 bridge bridge local
c81f7ace7dc6 host host local
621ead08fa64 none null local
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
024a9207d378 busybox "sh" 52 minutes ago Up 52 minutes test
7777965592a7 5d0da3dc9764 "/bin/bash" 2 hours ago Up 2 hours by
[root@localhost ~]# docker network connect br024a9207d378
/ # 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:01: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
/ # hostname
d90e81025b20
/ # ping 192.168.1.2
PING 192.168.1.3 (192.168.1.2): 56 data bytes
64 bytes from 192.168.1.2: seq=0 ttl=64 time=0.142 ms
64 bytes from 192.168.1.2: seq=1 ttl=64 time=0.077 ms
64 bytes from 192.168.1.2: seq=2 ttl=64 time=0.077 ms
^C
--- 192.168.1.2 ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 0.077/0.098/0.142 ms