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 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命令不会返回任何信息。
通过命令创建一个名为ns0的命名空间:
[root@localhost ~]# ip netns list
[root@localhost ~]# ip netns add ns0
[root@localhost ~]# ip netns list
ns0
新创建的 Network Namespace 会出现在/var/run/netns/目录下。如果相同名字的 namespace 已经存在,命令会报Cannot create namespace file “/var/run/netns/ns0”: File exists的错误。
[root@localhost ~]# ls /var/run/netns/
ns0
[root@localhost ~]# ip netns add ns0
Cannot create namespace file "/var/run/netns/ns0": File exists
对于每个 Network Namespace 来说,它会有自己独立的网卡、路由表、ARP 表、iptables 等和网络相关的资源。
ip命令提供了ip netns exec子命令可以在对应的 Network Namespace 中执行命令。
查看新创建 Network Namespace 的网卡信息
[root@localhost ~]# ip netns exec ns0 ip addr
1: lo: 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 ns0 ping 127.0.0.1
connect: 网络不可达
通过下面的命令启用lo回环网卡:
[root@localhost ~]# ip netns exec ns0 ip link set lo up
[root@localhost ~]# ip netns exec ns0 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.055 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.086 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.073 ms
64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.025 ms
我们可以在不同的 Network Namespace 之间转移设备(如veth)。由于一个设备只能属于一个 Network Namespace ,所以转移后在这个 Network Namespace 内就看不到这个设备了。
其中,veth设备属于可转移设备,而很多其它设备(如lo、vxlan、ppp、bridge等)是不可以转移的。
veth pair 全称是 Virtual Ethernet Pair,是一个成对的端口,所有从这对端口一 端进入的数据包都将从另一端出来,反之也是一样。
引入veth pair是为了在不同的 Network Namespace 直接进行通信,利用它可以直接将两个 Network Namespace 连接起来。
[root@localhost ~]# ip link add type veth
[root@localhost ~]# ip a
20: veth0@veth1: mtu 1500 qdisc noop state DOWN group default qlen 1000
link/ether a2:c6:0a:25:d1:ce brd ff:ff:ff:ff:ff:ff
21: veth1@veth0: mtu 1500 qdisc noop state DOWN group default qlen 1000
link/ether ba:0a:b5:7d:d7:62 brd ff:ff:ff:ff:ff:ff
可以看到,此时系统中新增了一对veth pair,将veth0和veth1两个虚拟网卡连接了起来,此时这对 veth pair 处于”未启用“状态。
下面我们利用veth pair实现两个不同的 Network Namespace 之间的通信。刚才我们已经创建了一个名为ns0的 Network Namespace,下面再创建一个信息Network Namespace,命名为ns1
[root@localhost ~]# ip netns add ns1
[root@localhost ~]# ip netns list
ns1
ns0
然后我们将veth0加入到ns0,将veth1加入到ns1
[root@localhost ~]# ip link set veth0 netns ns0
[root@localhost ~]# ip link set veth1 netns ns1
然后我们分别为这对veth pair配置上ip地址,并启用它们
[root@localhost ~]# ip netns exec ns0 ip link set veth0 up
[root@localhost ~]# ip netns exec ns0 ip addr add 192.168.235.2/24 dev veth0
[root@localhost ~]# ip netns exec ns1 ip link set lo up
[root@localhost ~]# ip netns exec ns1 ip link set veth1 up
[root@localhost ~]# ip netns exec ns1 ip addr add 192.168.235.4/24 dev veth1
查看这对veth pair的状态
[root@localhost ~]# ip netns exec ns0 ip a
1: lo: 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
20: veth0@if21: mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether a2:c6:0a:25:d1:ce brd ff:ff:ff:ff:ff:ff link-netns ns1
inet 192.168.235.2/24 scope global veth0
valid_lft forever preferred_lft forever
inet6 fe80::a0c6:aff:fe25:d1ce/64 scope link
valid_lft forever preferred_lft forever
[root@localhost ~]# ip netns exec ns1 ip a
1: lo: 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
21: veth1@if20: mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether ba:0a:b5:7d:d7:62 brd ff:ff:ff:ff:ff:ff link-netns ns0
inet 192.168.235.4/24 scope global veth1
valid_lft forever preferred_lft forever
inet6 fe80::b80a:b5ff:fe7d:d762/64 scope link
valid_lft forever preferred_lft forever
从上面可以看出,我们已经成功启用了这个veth pair,并为每个veth设备分配了对应的ip地址。我们尝试在ns1中访问ns0中的ip地址:
[root@localhost ~]# ip netns exec ns1 ping 192.168.235.2
PING 192.168.235.2 (192.168.235.2) 56(84) bytes of data.
64 bytes from 192.168.235.2: icmp_seq=1 ttl=64 time=0.065 ms
64 bytes from 192.168.235.2: icmp_seq=2 ttl=64 time=0.089 ms
可以看到,veth pair成功实现了两个不同Network Namespace之间的网络交互。
[root@localhost ~]# ip netns exec ns0 ip link set veth0 down
[root@localhost ~]# ip netns exec ns0 ip link set dev veth0 name ens0
[root@localhost ~]# ip netns exec ns0 ip link set ens0 up
[root@localhost ~]# ip netns exec ns0 ip a
1: lo: 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
20: ens0@if21: mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether a2:c6:0a:25:d1:ce brd ff:ff:ff:ff:ff:ff link-netns ns1
inet 192.168.235.2/24 scope global ens0
valid_lft forever preferred_lft forever
inet6 fe80::a0c6:aff:fe25:d1ce/64 scope link
valid_lft forever preferred_lft forever
[root@localhost ~]# docker run -it --name yaya --rm busybox
Unable to find image 'busybox:latest' locally
latest: Pulling from library/busybox
3aab638df1a9: Pull complete
Digest: sha256:52817dece4cfe26f581c834d27a8e1bcc82194f914afe6d50afad5a101234ef1
Status: Downloaded newer image for busybox:latest
/ # ip a
1: lo: 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: 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 container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3f1c044207c6 busybox "sh" 26 seconds ago Up 24 seconds yaya
//在创建容器时添加--network bridge与不加--network选项效果是一致的
[root@localhost ~]# docker run -it --name yaya --network bridge --rm busybox
/ # ip a
1: lo: 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
6: eth0@if7: 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 run -it --name yaya --network none --rm busybox
/ # ip a
1: lo: 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
启动第一个容器
[root@localhost ~]# docker run -it --name y1 --rm busybox
/ # ip a
1: lo: 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: 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 run -it --name y2 --rm busybox
/ # ip a
1: lo: 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: 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
/ #
可以看到名为y2的容器IP地址是10.0.0.3,与第一个容器的IP地址不是一样的,也就是说并没有共享网络,此时如果我们将第二个容器的启动方式改变一下,就可以使名为y2的容器IP与y1容器IP一致,也即共享IP,但不共享文件系统。
[root@localhost ~]# docker run -it --name y2 --rm --network container:y1 busybox
/ # ip a
1: lo: 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: 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
/ #
此时我们在y1容器上创建一个目录
/ # mkdir /tmp/data
/ # ls tmp/
data
/ #
到y2容器上检查/tmp目录会发现并没有这个目录,因为文件系统是处于隔离状态,仅仅是共享了网络而已。
在y2容器上部署一个站点
/ # echo 'hello yaya' > /tmp/index.html
/ # ls tmp/
data 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
/ #
在y1容器上用本地地址去访问此站点
/ # wget -O - -q 127.0.0.1:80
hello yaya
由此可见,container模式下的容器间关系就相当于一台主机上的两个不同进程
启动容器时直接指明模式为host
[root@localhost ~]# docker run -it --name y2 --rm --network host busybox
/ # ip a
1: lo: 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: ens33: mtu 1500 qdisc fq_codel qlen 1000
link/ether 00:0c:29:b8:d0:10 brd ff:ff:ff:ff:ff:ff
inet 192.168.235.160/24 brd 192.168.235.255 scope global dynamic noprefixroute ens33
valid_lft 957sec preferred_lft 957sec
inet6 fe80::cb48:11a1:1d08:cdf0/64 scope link noprefixroute
valid_lft forever preferred_lft forever
3: docker0: mtu 1500 qdisc noqueue
link/ether 02:42:3e:f8:13:5c 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:3eff:fef8:135c/64 scope link
valid_lft forever preferred_lft forever
9: vethe3306e3@if8: mtu 1500 qdisc noqueue master docker0
link/ether 62:34:0a:6f:0f:fb brd ff:ff:ff:ff:ff:ff
inet6 fe80::6034:aff:fe6f:ffb/64 scope link
valid_lft forever preferred_lft forever
/ #
此时如果我们在这个容器中启动一个http站点,我们就可以直接用宿主机的IP直接在浏览器中访问这个容器中的站点了。
[root@localhost ~]# docker run -it --name y2 --rm --network bridge busybox
/ # hostname
de627a4a48a4
/ #
[root@localhost ~]# docker run -it --hostname node1 --rm busybox
/ # hostname
node1
/ # 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.3 node1
/ # cat /etc/resolv.conf
# Generated by NetworkManager
search localdomain
nameserver 192.168.235.2
/ # ping www.baidu.com
PING www.baidu.com (36.152.44.96): 56 data bytes
64 bytes from 36.152.44.96: seq=0 ttl=127 time=44.181 ms
64 bytes from 36.152.44.96: seq=1 ttl=127 time=47.264 ms
[root@localhost ~]# docker run -it --rm --hostname node1 --dns 114.114.114.114 busybox
/ # hostname
node1
/ #
/ # cat /etc/resolv.conf
search localdomain
nameserver 114.114.114.114
/ # 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.3 node1
[root@localhost ~]# docker run -it --rm --hostname node1 --dns 114.114.114.114 --add-host node2:172.17.0.3 --add-host node3:172.17.0.3 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.3 node2
172.17.0.3 node3
172.17.0.3 node1
执行docker run的时候有个-p选项,可以将容器中的应用端口映射到宿主机中,从而实现让外部主机可以通过访问宿主机的某端口来访问容器内应用的目的。
-p选项能够使用多次,其所能够暴露的端口必须是容器确实在监听的端口。
-p选项的使用格式:
[root@localhost ~]# docker run -d --name web --rm -p 80 nginx
82e9e07c1fbc617d73e32896a72fef1dc4788664a698b30b4e41264a39ed9166
以上命令执行后会一直占用着前端,我们新开一个终端连接来看一下容器的80端口被映射到了宿主机的什么端口上
[root@localhost ~]# docker port web
80/tcp -> 0.0.0.0:49154
80/tcp -> :::49154
由此可见,容器的80端口被暴露到了宿主机的32769端口上,此时我们在宿主机上访问一下这个端口看是否能访问到容器内的站点
[root@localhost ~]# curl http://192.168.235.160:49154
Welcome to nginx!
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.
For online documentation and support please refer to
nginx.org.
Commercial support is available at
nginx.com.
Thank you for using nginx.
2、最常用的一种,将容器的80映射到本机的指定8080端口号
[root@localhost ~]# docker run -d --name web --rm -p 8080:80 nginx
1ec31fc84269ee3y5bdd46feca294c21667f42cf238b198956fdcc74bd2e545
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:8080 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:8080 [::]:*
LISTEN 0 128 [::]:22 [::]:*
//访问
[root@localhost ~]# curl 192.168.235.160:8080
Welcome to nginx!
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.
For online documentation and support please refer to
nginx.org.
Commercial support is available at
nginx.com.
Thank you for using nginx.
3、iptables防火墙规则将随容器的创建自动生成,随容器的删除自动删除规则。
将容器端口映射到指定IP的随机端口
[root@localhost ~]# docker run -d --name web --rm -p 192.168.235.160::80 nginx
er53bcb066634533a7af26b3495cd6b5b485e4345b7aa782559677197914a49b
在另一个终端上查看端口映射情况
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 192.168.235.160:49154 0.0.0.0:*
LISTEN 0 128 [::]:22
//访问
[root@localhost ~]# curl 192.168.235.160:49154
Welcome to nginx!
··············
//映射
[root@localhost ~]# docker port web
80/tcp -> 192.168.235.160:49154
官方文档
自定义docker0桥的网络属性信息需要修改/etc/docker/daemon.json配置文件
[root@localhost ~]# ls /etc/docker/daemon.json
/etc/docker/daemon.json
[root@localhost ~]# vim /etc/docker/daemon.json
[root@localhost ~]# cat /etc/docker/daemon.json
{
"registry-mirrors": ["https://arq8p4a6.mirror.aliyuncs.com"]
"bip": "192.168.235.160/24"
}
[root@localhost ~]# ip a
1: lo: 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: ens33: mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:b8:d0:10 brd ff:ff:ff:ff:ff:ff
inet 192.168.235.160/24 brd 192.168.235.255 scope global dynamic noprefixroute ens33
valid_lft 1690sec preferred_lft 1690sec
inet6 fe80::cb48:11a1:1d08:cdf0/64 scope link noprefixroute
valid_lft forever preferred_lft forever
3: docker0: mtu 1500 qdisc noqueue state DOWN group default
link/ether 02:42:3e:f8:13:5c brd ff:ff:ff:ff:ff:ff
inet 192.168.235.160/24 brd 192.168.255.255 scope global docker0
valid_lft forever preferred_lft forever
inet6 fe80::42:3eff:fef8:135c/64 scope link
valid_lft forever preferred_lft forever
核心选项为bip,即bridge ip之意,用于指定docker0桥自身的IP地址;其它选项可通过此地址计算得出。
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.235.160:2375 ps
这种方式现在基本上不怎么用,很少说用dockers远程连接去控制,所以了解一下就可以了,知道有这么一个功能就可以了
创建一个额外的自定义桥,区别于docker0
[root@localhost ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
8093f454945a bridge bridge local
eed38306b990 host host local
99828ff95579 none null local
[root@localhost ~]# docker network create -d bridge --subnet "192.168.1.0/24" --gateway "192.168.1.1" br0
6a3d4797b80d12f25d2787da04386755374cf3b60f71cc228b7b444eb671bbd1
[root@localhost ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
6a3d4797b80d br0 bridge local
8093f454945a bridge bridge local
eed38306b990 host host local
99828ff95579 none null local
使用新创建的自定义桥来创建容器:
[root@localhost ~]# docker run -it --name y1 --network br0 busybox
Unable to find image 'busybox:latest' locally
latest: Pulling from library/busybox
3aab638df1a9: Pull complete
Digest: sha256:52817dece4cfe26f581c834d27a8e1bcc82194f914afe6d50afad5a101234ef1
Status: Downloaded newer image for busybox:latest
/ # ip a
1: lo: 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
5: eth0@if6: 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 --name y2 -it busybox
/ # ls
bin dev etc home proc root sys tmp usr var
/ # ip a
1: lo: 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: 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
6a3d4797b80d br0 bridge local
8093f454945a bridge bridge local
eed38306b990 host host local
99828ff95579 none null local
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9b680f83d2d3 busybox "sh" About a minute ago Exited (0) 14 seconds ago y2
5bc17726f078 busybox "sh" 12 minutes ago Exited (0) About a minute ago y1
[root@localhost ~]# docker network connect br0 9b680f83d2d3
/ # ip a
1: lo: 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: 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
/ # hostname
9b680f83d2d3
/ # ping 192.168.1.2 //这里就可以ping通了
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.091 ms
64 bytes from 192.168.1.2: seq=1 ttl=64 time=0.121 ms