当前位置:   article > 正文

Docker容器网络配置_from 172.17.17.35 icmp-seq destination

from 172.17.17.35 icmp-seq destination

Docker容器网络配置

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
[root@localhost ~]# 

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

默认情况下,Linux系统中是没有任何 Network Namespace的,所以ip netns list命令不会返回任何信息。

创建Network Namespace
通过命令创建一个名为IU的命名空间:

[root@localhost ~]# ip netns list
[root@localhost ~]# ip netns add IU
[root@localhost ~]# ip netns list
IU
[root@localhost ~]# 

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

新创建的 Network Namespace 会出现在/var/run/netns/目录下。如果相同名字的 namespace 已经存在,命令会报Cannot create namespace file “/var/run/netns/sb0”: File exists的错误。

[root@localhost ~]# ls /var/run/netns/
IU
[root@localhost ~]# ip netns add IU
Cannot create namespace file "/var/run/netns/IU": File exists
[root@localhost ~]# 

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

对于每个 Network Namespace 来说,它会有自己独立的网卡、路由表、ARP 表、iptables 等和网络相关的资源。

操作Network Namespace
ip命令提供了ip netns exec子命令可以在对应的 Network Namespace 中执行命令。

[root@localhost ~]# ip netns exec IU 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
[root@localhost ~]# 

  • 1
  • 2
  • 3
  • 4
  • 5

可以看到,新创建的Network Namespace中会默认创建一个lo回环网卡,此时网卡处于关闭状态。此时,尝试去 ping 该lo回环网卡,会提示Network is unreachable

[root@localhost ~]# ip netns exec IU ping 127.0.0.1
connect: 网络不可达

  • 1
  • 2
  • 3

通过下面的命令启用lo回环网卡:

[root@localhost ~]# ip netns exec IU ip link set lo up
[root@localhost ~]# ip netns exec IU 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.018 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.020 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.057 ms
64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.020 ms

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

转移设备
我们可以在不同的 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 mq state UP group default qlen 1000
    link/ether 00:0c:29:b9:12:52 brd ff:ff:ff:ff:ff:ff
    inet 192.168.8.137/24 brd 192.168.8.255 scope global dynamic noprefixroute ens160
       valid_lft 977sec preferred_lft 977sec
    inet6 fe80::a53a:d8ca:5571:35c8/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:ba:5b:e2:b6 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 9e:32:c2:8d:64:65 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 12:e4:32:0b:07:ac brd ff:ff:ff:ff:ff:ff
[root@localhost ~]# 

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

可以看到,此时系统中新增了一对veth pair,将veth0和veth1两个虚拟网卡连接了起来,此时这对 veth pair 处于”未启用“状态。

实现Network Namespace间通信
下面我们利用veth pair实现两个不同的 Network Namespace 之间的通信。刚才我们已经创建了一个名为IU的 Network Namespace,下面再创建一个信息Network Namespace,命名为IU1

[root@localhost ~]# ip netns add IU1
[root@localhost ~]# ip netns list
IU1
IU
[root@localhost ~]# 

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

然后我们将veth0加入到IU,将veth1加入到IU1

[root@localhost ~]# ip link set veth0 netns IU
[root@localhost ~]# ip link set veth1 netns IU1
[root@localhost ~]# 

  • 1
  • 2
  • 3
  • 4

然后我们分别为这对veth pair配置上ip地址,并启用它们

[root@localhost ~]# ip netns exec IU ip link set veth0 
[root@localhost ~]# ip netns exec IU ip addr add 192.168.8.200/24 dev veth0
[root@localhost ~]# ip netns exec IU1 ip link set lo up
[root@localhost ~]# ip netns exec IU1 ip link set veth1 up
[root@localhost ~]# ip netns exec IU1 ip addr add 192.168.8.201/24 dev veth1
[root@localhost ~]# 

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

查看这对veth pair的状态

[root@localhost ~]# ip netns exec IU 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 9e:32:c2:8d:64:65 brd ff:ff:ff:ff:ff:ff link-netns IU1
    inet 192.168.8.200/24 scope global veth0
       valid_lft forever preferred_lft forever
[root@localhost ~]# 

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
[root@localhost ~]# ip netns exec IU1 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: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state LOWERLAYERDOWN group default qlen 1000
    link/ether 12:e4:32:0b:07:ac brd ff:ff:ff:ff:ff:ff link-netns IU
    inet 192.168.8.201/24 scope global veth1
       valid_lft forever preferred_lft forever
[root@localhost ~]# 

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

从上面可以看出,我们已经成功启用了这个veth pair,并为每个veth设备分配了对应的ip地址。我们尝试在IU1中访问IU中的ip地址:

[root@localhost ~]# ip netns exec IU1 ping 192.168.8.200
PING 192.168.8.200 (192.168.8.200) 56(84) bytes of data.
From 192.168.8.200icmp_seq=1 Destination Host Unreachable
From 192.168.8.200 icmp_seq=2 Destination Host Unreachable

  • 1
  • 2
  • 3
  • 4
  • 5
[root@localhost ~]# ip netns exec IU ping 192.168.8.201
PING 192.168.8.201 (192.168.8.201) 56(84) bytes of data.
64 bytes from 192.168.8.201: icmp_seq=1 ttl=64 time=0.032 ms
64 bytes from 192.168.8.201: icmp_seq=2 ttl=64 time=0.031 ms
  • 1
  • 2
  • 3
  • 4

可以看到,veth pair成功实现了两个不同Network Namespace之间的网络交互。
veth设备重命名

[root@localhost ~]# ip netns exec IU ip link set veth0 down
[root@localhost ~]# ip netns exec IU ip link set dev veth0 name ens0
[root@localhost ~]# ip netns exec IU ip link set ens0 up
[root@localhost ~]# ip netns exec IU 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 66:e8:76:be:c2:01 brd ff:ff:ff:ff:ff:ff link-netns IU1
    inet 192.168.8.201/24 scope global ens0
       valid_lft forever preferred_lft forever
    inet6 fe80::64e8:76ff:febe:c201/64 scope link 
       valid_lft forever preferred_lft forever

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

四种网络模式配置
bridge模式配置

[root@localhost ~]# docker images
REPOSITORY         TAG       IMAGE ID       CREATED        SIZE
pengyudong/php8    v4        d8e69532d64e   35 hours ago   1.52GB
pengyudong/php8    v3        c83b573c4aad   35 hours ago   1.52GB
pengyudong/php8    v2        92b554d83af7   35 hours ago   1.52GB
pengyudong/php8    v1        4e9615905123   35 hours ago   1.52GB
pengyudong/mysql   v2        698afd302784   36 hours ago   3.81GB
pengyudong/mysql   v1        e2e730f20579   36 hours ago   3.81GB
pengyudong/nginx   v2        97c5a3076d39   37 hours ago   576MB
busybox            latest    d23834f29b38   5 days ago     1.24MB
centos             latest    5d0da3dc9764   2 months ago   231MB
[root@localhost ~]# docker run -it --name test1 --rm --network bridge centos
[root@02200fa23842 /]# 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
10: eth0@if11: <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@02200fa23842 /]# exit
exit
[root@localhost ~]# docker run -it --name test1 --rm centos
[root@e3091ca6737b /]# 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
12: eth0@if13: <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@e3091ca6737b /]# 

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

none模式配置

[root@localhost ~]# docker run -it --name test2 --rm --network none centos
[root@7b94e66b0a38 /]# 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
[root@7b94e66b0a38 /]# 

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

container模式配置

[root@localhost ~]# docker run -it --name test3 --rm busybox
/ # 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: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)

/ # 

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
[root@localhost ~]# docker run -it --name test4 --rm busybox
/ # ifconfig 
eth0      Link encap:Ethernet  HWaddr 02:42:AC:11:00:04  
          inet addr:172.17.0.4  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)

/ # 

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

可以看到名为 test4 的容器IP地址 test3 容器的IP地址不是一样的,也就是说并没有共享网络,此时如果我们将第二个容器的启动方式改变一下,就可以使名为 test4 的容器IP与 test3 容器IP一致,也即共享IP,但不共享文件系统。

[root@localhost ~]# docker run -it --name test4 --rm --network container:test3 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:12 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:976 (976.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)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

此时我们在 test3 容器上创建一个目录

/ # ls /tmp/
/ # mkdir /tmp/pyd
/ # ls /tmp/
pyd

  • 1
  • 2
  • 3
  • 4
  • 5

到 test4 容器上检查/tmp目录会发现并没有这个目录,因为文件系统是处于隔离状态,仅仅是共享了网络而已。

/ # ls /tmp/
/ # 

  • 1
  • 2
  • 3

在 test4 容器上部署一个站点

/ # echo "hello pyd" > /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      
           :::*                    LISTEN  

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

在 test3 容器上用本地地址去访问此站点

/ # wget -O - -q 127.0.0.1:80
hello pyd
  • 1
  • 2

由此可见,container模式下的容器间关系就相当于一台主机上的两个不同进程
host模式配置

[root@localhost ~]# docker run -it --name p1 --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:b9:12:52 brd ff:ff:ff:ff:ff:ff
    inet 192.168.8.137/24 brd 192.168.8.255 scope global dynamic noprefixroute ens160
       valid_lft 980sec preferred_lft 980sec
    inet6 fe80::a53a:d8ca:5571:35c8/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue 
    link/ether 02:42:ba:5b:e2:b6 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:baff:fe5b:e2b6/64 scope link 
       valid_lft forever preferred_lft forever
6: veth0@veth1: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop qlen 1000
    link/ether c6:e8:cf:83:d4:ef brd ff:ff:ff:ff:ff:ff
7: veth1@veth0: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop qlen 1000
    link/ether 5a:06:54:9c:c7:38 brd ff:ff:ff:ff:ff:ff
9: veth258dbb2@if8: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue master docker0 
    link/ether 1a:66:7f:05:ee:2c brd ff:ff:ff:ff:ff:ff
    inet6 fe80::1866:7fff:fe05:ee2c/64 scope link 
       valid_lft forever preferred_lft forever
19: veth363fcb7@if18: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue master docker0 
    link/ether da:34:4a:38:32:eb brd ff:ff:ff:ff:ff:ff
    inet6 fe80::d834:4aff:fe38:32eb/64 scope link 
       valid_lft forever preferred_lft forever
21: veth8125de9@if20: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue master docker0 
    link/ether 2e:8f:b7:d9:54:a8 brd ff:ff:ff:ff:ff:ff
    inet6 fe80::2c8f:b7ff:fed9:54a8/64 scope link 
       valid_lft forever preferred_lft forever

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
[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 mq state UP group default qlen 1000
    link/ether 00:0c:29:b9:12:52 brd ff:ff:ff:ff:ff:ff
    inet 192.168.8.137/24 brd 192.168.8.255 scope global dynamic noprefixroute ens160
       valid_lft 959sec preferred_lft 959sec
    inet6 fe80::a53a:d8ca:5571:35c8/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:ba:5b:e2:b6 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:baff:fe5b:e2b6/64 scope link 
       valid_lft forever preferred_lft forever
6: veth0@veth1: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether c6:e8:cf:83:d4:ef brd ff:ff:ff:ff:ff:ff
7: veth1@veth0: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether 5a:06:54:9c:c7:38 brd ff:ff:ff:ff:ff:ff
9: veth258dbb2@if8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default 
    link/ether 1a:66:7f:05:ee:2c brd ff:ff:ff:ff:ff:ff link-netnsid 2
    inet6 fe80::1866:7fff:fe05:ee2c/64 scope link 
       valid_lft forever preferred_lft forever
19: veth363fcb7@if18: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default 
    link/ether da:34:4a:38:32:eb brd ff:ff:ff:ff:ff:ff link-netnsid 3
    inet6 fe80::d834:4aff:fe38:32eb/64 scope link 
       valid_lft forever preferred_lft forever
21: veth8125de9@if20: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default 
    link/ether 2e:8f:b7:d9:54:a8 brd ff:ff:ff:ff:ff:ff link-netnsid 4
    inet6 fe80::2c8f:b7ff:fed9:54a8/64 scope link 
       valid_lft forever preferred_lft forever
[root@localhost ~]# 

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

此时如果我们在这个容器中启动一个 http站点,我们就可以直接用宿主机的IP直接在浏览器中访问这个容器中的站点了。

/ # echo hi > /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 0.0.0.0:22              0.0.0.0:*               LISTEN      
tcp        0     52 192.168.8.137:22        192.168.8.1:52526       ESTABLISHED 
tcp        0      0 192.168.8.137:22        192.168.8.1:51352       ESTABLISHED 
tcp        0      0 192.168.8.137:22        192.168.8.1:52511       ESTABLISHED 
tcp        0      0 :::22                   :::*                    LISTEN      
tcp        0      0 :::80                   :::*                    LISTEN      
/ # 

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

在这里插入图片描述
容器的常用操作
查看容器的主机名

[root@localhost ~]# docker run -it --name p2 --rm busybox
/ # hostname 
f2820b928daa
/ # 

  • 1
  • 2
  • 3
  • 4
  • 5
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED          STATUS          PORTS     NAMES
f2820b928daa   busybox   "sh"      26 seconds ago   Up 22 seconds             p2
ebc4db5f1054   busybox   "sh"      9 minutes ago    Up 9 minutes              test3
aa94b58ccee7   busybox   "sh"      23 minutes ago   Up 23 minutes             by
[root@localhost ~]# 

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

在容器启动时注入主机名

[root@localhost ~]# docker run -it --name t2 --rm --hostname host1 busybox
/ # hostname 
host1
/ # 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.4	host1
/ # cat /etc/resolv.conf 
# Generated by NetworkManager
search localdomain
nameserver 192.168.8.2
/ # 

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

手动指定容器要使用的DNS

[root@localhost ~]# docker run -it --name p2 --rm --hostname host1 --dns 8.8.8.8 busybox
/ # cat /etc/resolv.conf
search localdomain
nameserver 8.8.8.8
/ # 

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

手动往/etc/hosts文件中注入主机名到IP地址的映射

[root@localhost ~]# docker run -it --name p2 --rm --hostname node1 --add-host www.p2.com:8.8.8.8 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
8.8.8.8	www.p2.com
172.17.0.4	node1
/ # 

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

开放容器端口

执行docker run的时候有个-p选项,可以将容器中的应用端口映射到宿主机中,从而实现让外部主机可以通过访问宿主机的某端口来访问容器内应用的目的。

-p选项能够使用多次,其所能够暴露的端口必须是容器确实在监听的端口。

-p选项的使用格式:

  • -p
  • 将指定的容器端口映射至主机所有地址的一个动态端口
[root@localhost ~]# docker run -it --name nginx --rm -p 80 97c5a3076d39
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED          STATUS          PORTS     NAMES
f2820b928daa   busybox   "sh"      26 seconds ago   Up 22 seconds             p2
ebc4db5f1054   busybox   "sh"      9 minutes ago    Up 9 minutes              test3
aa94b58ccee7   busybox   "sh"      23 minutes ago   Up 23 minutes             by
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED              STATUS              PORTS                                     NAMES
5c6f7ab23884   97c5a3076d39   "/usr/local/nginx/sb…"   About a minute ago   Up About a minute   0.0.0.0:49153->80/tcp, :::49153->80/tcp   nginx
ebc4db5f1054   busybox        "sh"                     16 minutes ago       Up 16 minutes                                                 test3
aa94b58ccee7   busybox        "sh"                     30 minutes ago       Up 30 minutes                                                 by
[root@localhost ~]# 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:49153                    0.0.0.0:*          
LISTEN       0             128                           [::]:22                          [::]:*          
LISTEN       0             128                           [::]:49153                       [::]:*          
[root@localhost ~]# 

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
[root@localhost ~]# curl http://127.0.0.1:49153
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    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 ~]# 

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

在这里插入图片描述

// iptables防火墙规则将随容器的创建自动生成,随容器的删除自动删除规则。
[root@localhost ~]# iptables -t nat -nvL  
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
   12   624 DOCKER     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ADDRTYPE match dst-type LOCAL

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 MASQUERADE  all  --  *      !docker0  172.17.0.0/16        0.0.0.0/0           
    0     0 MASQUERADE  tcp  --  *      *       172.17.0.4           172.17.0.4           tcp dpt:80

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 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           
    2   104 DNAT       tcp  --  !docker0 *       0.0.0.0/0            0.0.0.0/0            tcp dpt:49153 to:172.17.0.4:80
[root@localhost ~]# 

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • -p :
  • 将容器端口映射至指定的主机端口
[root@localhost ~]# docker images
REPOSITORY         TAG       IMAGE ID       CREATED        SIZE
pengyudong/php8    v4        d8e69532d64e   35 hours ago   1.52GB
pengyudong/php8    v3        c83b573c4aad   35 hours ago   1.52GB
pengyudong/php8    v2        92b554d83af7   35 hours ago   1.52GB
pengyudong/php8    v1        4e9615905123   35 hours ago   1.52GB
pengyudong/mysql   v2        698afd302784   36 hours ago   3.81GB
pengyudong/mysql   v1        e2e730f20579   36 hours ago   3.81GB
pengyudong/nginx   v2        97c5a3076d39   38 hours ago   576MB
[root@localhost ~]# docker run -it --name nginx --rm -p 80:80 97c5a3076d39
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS                               NAMES
4d6cd63cb61e   97c5a3076d39   "/usr/local/nginx/sb…"   50 seconds ago   Up 49 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp   nginx
aa94b58ccee7   busybox        "sh"                     35 minutes ago   Up 35 minutes                                       by
[root@localhost ~]# 

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

在这里插入图片描述

  • -p ::
  • 将指定的容器端口映射至主机指定的动态端口
[root@localhost ~]# docker run -it --name nginx --rm -p 192.168.8.137::80 97c5a3076d39
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS                         NAMES
fa1bc60aa2a8   97c5a3076d39   "/usr/local/nginx/sb…"   20 seconds ago   Up 19 seconds   192.168.8.137:49153->80/tcp   nginx
aa94b58ccee7   busybox        "sh"                     38 minutes ago   Up 38 minutes                                 by
[root@localhost ~]# 

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

在这里插入图片描述
-p ::
将指定的容器端口映射至主机指定的端口

[root@localhost ~]# docker run -it --name nginx --rm -p 192.168.8.137:80:80 97c5a3076d39
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS                      NAMES
dd58a0d26ab2   97c5a3076d39   "/usr/local/nginx/sb…"   14 seconds ago   Up 13 seconds   192.168.8.137:80->80/tcp   nginx
aa94b58ccee7   busybox        "sh"                     39 minutes ago   Up 39 minutes                              by
[root@localhost ~]# 

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

在这里插入图片描述
自定义docker0桥的网络属性信息
自定义docker0桥的网络属性信息需要修改/etc/docker/daemon.json配置文件

注意: 修改的网桥不能和自己的默认网卡网段一致

[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 mq state UP group default qlen 1000
    link/ether 00:0c:29:b9:12:52 brd ff:ff:ff:ff:ff:ff
    inet 192.168.8.137/24 brd 192.168.8.255 scope global dynamic noprefixroute ens160
       valid_lft 1413sec preferred_lft 1413sec
    inet6 fe80::a53a:d8ca:5571:35c8/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:ba:5b:e2:b6 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:baff:fe5b:e2b6/64 scope link 
       valid_lft forever preferred_lft forever
6: veth0@veth1: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether c6:e8:cf:83:d4:ef brd ff:ff:ff:ff:ff:ff
7: veth1@veth0: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether 5a:06:54:9c:c7:38 brd ff:ff:ff:ff:ff:ff
9: veth258dbb2@if8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default 
    link/ether 1a:66:7f:05:ee:2c brd ff:ff:ff:ff:ff:ff link-netnsid 2
    inet6 fe80::1866:7fff:fe05:ee2c/64 scope link 
       valid_lft forever preferred_lft forever
39: vethf76a77b@if38: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default 
    link/ether 32:de:d0:73:ee:99 brd ff:ff:ff:ff:ff:ff link-netnsid 3
    inet6 fe80::30de:d0ff:fe73:ee99/64 scope link 
       valid_lft forever preferred_lft forever
[root@localhost ~]# 

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
[root@localhost ~]# vim /etc/docker/daemon.json 
[root@localhost ~]# cat /etc/docker/daemon.json 
{
  "bip": "192.168.100.100/24",	
  "registry-mirrors": ["https://faq69nhk.mirror.aliyuncs.com"]
}
[root@localhost ~]# systemctl restart docker
[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 mq state UP group default qlen 1000
    link/ether 00:0c:29:b9:12:52 brd ff:ff:ff:ff:ff:ff
    inet 192.168.8.137/24 brd 192.168.8.255 scope global dynamic noprefixroute ens160
       valid_lft 970sec preferred_lft 970sec
    inet6 fe80::a53a:d8ca:5571:35c8/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:ba:5b:e2:b6 brd ff:ff:ff:ff:ff:ff
    inet 192.168.100.100/24 brd 192.168.100.255 scope global docker0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:baff:fe5b:e2b6/64 scope link 
       valid_lft forever preferred_lft forever
6: veth0@veth1: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether c6:e8:cf:83:d4:ef brd ff:ff:ff:ff:ff:ff
7: veth1@veth0: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether 5a:06:54:9c:c7:38 brd ff:ff:ff:ff:ff:ff
[root@localhost ~]# 

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

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"]

  • 1
  • 2

在客户端上向dockerd直接传递“-H|–host”选项指定要控制哪台主机上的docker容器

docker -H 192.168.10.145:2375 ps

  • 1
  • 2

docker创建自定义桥
创建一个额外的自定义桥,区别于docker0

[root@localhost ~]# docker network ls
NETWORK ID     NAME      DRIVER    SCOPE
94a13518eeb9   bridge    bridge    local
8977fc9aa760   host      host      local
a9f3e3786e5f   none      null      local
[root@localhost ~]# docker network create -d bridge --subnet "192.168.1.0/24" --gateway "192.168.1.1" br0b8537b9f73b04b85ebdb1a917126f712da6c7d43105c56e869dcb0debb7c06ae
[root@localhost ~]# docker network ls
NETWORK ID     NAME      DRIVER    SCOPE
b8537b9f73b0   br0       bridge    local
94a13518eeb9   bridge    bridge    local
8977fc9aa760   host      host      local
a9f3e3786e5f   none      null      local
[root@localhost ~]# 

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

使用新创建的自定义桥来创建容器:

[root@localhost ~]# docker run -it --name by --network br0  --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
41: eth0@if42: <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
/ # 

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

再创建一个容器,使用默认的bridge桥:

[root@localhost ~]# docker run -it --name by1 --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
43: eth0@if44: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue 
    link/ether 02:42:c0:a8:64:01 brd ff:ff:ff:ff:ff:ff
    inet 192.168.100.1/24 brd 192.168.100.255 scope global eth0
       valid_lft forever preferred_lft forever
/ # 

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

试想一下,此时的by与by1能否互相通信?如果不能该如何实现通信?
在这里插入图片描述
我们可以把桥连接过去,br0是我们上传创建的,把默认桥连接到刚刚创建的桥,

[root@localhost ~]# docker network ls
NETWORK ID     NAME      DRIVER    SCOPE
b8537b9f73b0   br0       bridge    local
94a13518eeb9   bridge    bridge    local
8977fc9aa760   host      host      local
a9f3e3786e5f   none      null      local
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED              STATUS              PORTS     NAMES
9af5be08b1b0   busybox   "sh"      About a minute ago   Up About a minute             by1
e30a0db4413c   busybox   "sh"      2 minutes ago        Up 2 minutes                  by
[root@localhost ~]# docker network connect br0 9af5be08b1b0

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
//在by1容器去ping by容器的ip
/ # 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
43: eth0@if44: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue 
    link/ether 02:42:c0:a8:64:01 brd ff:ff:ff:ff:ff:ff
    inet 192.168.100.1/24 brd 192.168.100.255 scope global eth0
       valid_lft forever preferred_lft forever
45: eth1@if46: <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
/ # 192.168.1.2
sh: 192.168.1.2: not found
/ # 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.096 ms
64 bytes from 192.168.1.2: seq=1 ttl=64 time=0.055 ms
^C
--- 192.168.1.2 ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.055/0.075/0.096 ms

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/719147
推荐阅读
相关标签
  

闽ICP备14008679号