Docker:容器的四种网络类型 [十三]

一、None类型

简介:不为容器配置任何网络功能,--net=none

1、创建容器

docker run -it --network none busubox:latest

2、功能测试

[root@luoahong ~]# docker run -it --network none busybox:latest
/ # ifconfig
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:1 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

/ # free -m
              total        used        free      shared  buff/cache   available
Mem:           1984         730         125           0        1128        1135
Swap:          2047           0        2047
/ # ping www.baidu.com
ping: bad address 'www.baidu.com'
/ # ping ls
ping: bad address 'ls'

测试小结:

  1、网卡只有一块io

        2、只能执行本地命令,无法使用ping,ls

3、网络类型查看

[root@luoahong ~]# docker inspect 148da827024a
[
    {
        "Id": "148da827024a7e250d58684fedbd7467b1ec1950aa2f87f8f4c64f0094af905c",
        "Created": "2019-01-18T07:34:16.468365453Z",
        "Path": "sh",
        "Args": [],
        "State": {
          ..........
           "MacAddress": "",
            "Networks": {
                "none": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "03062097926d6dfd6b1296295154c960f7e7989a106f7a1cb318c1040e4fa327",
                    "EndpointID": "cfb8a5eb16445bd8bb32708848a481c4638b86927e08fef4151e987e440869ec",
                    "Gateway": "",
                    "IPAddress": "",
                    "IPPrefixLen": 0,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "",
                    "DriverOpts": null
                }
            }
        }
    }
]

二、Container

简介:与另一个运行中的容器共享Network Namespace,--net=container:containerID

docker run -it --network container:name centos:6.9
netstat -lntup  #是因为另外一个容器使用了80端口  共享网络端口不能冲突
ps -ef
docker ps -a
docker inspect ID  #信息全是空

ping 172.168.0.2

yum install tcpdump -y
tcpdump -i eth0 icmp

共享网络端口不能冲突

三、Host

简介:与主机共享Network Namespace,--net=host

1、创建容器

[root@luoahong ~]# docker run -it --network host centos:6.9

2、功能测试

1、容器命令测试

[root@luoahong /]# netstat -lntup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      -                   
tcp        0      0 :::22                       :::*                        LISTEN      -                   
tcp        0      0 :::32769                    :::*                        LISTEN      -                   
tcp        0      0 :::32770                    :::*                        LISTEN      -                   
tcp        0      0 :::32771                    :::*                        LISTEN      -                   
udp        0      0 0.0.0.0:68                  0.0.0.0:*                               -                   
udp        0      0 0.0.0.0:3255                0.0.0.0:*                               -                   
udp        0      0 127.0.0.1:323               0.0.0.0:*                               -                   
udp        0      0 :::28221                    :::*                                    -                   
udp        0      0 ::1:323                     :::*                                    -                   
[root@luoahong /]# hostname
luoahong
[root@luoahong /]# ifconfig
br-ad2616372f01 Link encap:Ethernet  HWaddr 02:42:14:4D:EC:B9  
          inet addr:172.18.0.1  Bcast:172.18.255.255  Mask:255.255.0.0
          inet6 addr: fe80::42:14ff:fe4d:ecb9/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:3772 errors:0 dropped:0 overruns:0 frame:0
          TX packets:4439 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:2284090 (2.1 MiB)  TX bytes:4238472 (4.0 MiB)

docker0   Link encap:Ethernet  HWaddr 02:42:F7:2F:D6:20  
          inet addr:172.17.0.1  Bcast:172.17.255.255  Mask:255.255.0.0
          inet6 addr: fe80::42:f7ff:fe2f:d620/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 b)  TX bytes:648 (648.0 b)

eth0      Link encap:Ethernet  HWaddr 00:0C:29:E7:B9:46  
          inet addr:192.168.231.136  Bcast:192.168.231.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fee7:b946/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:203307 errors:0 dropped:0 overruns:0 frame:0
          TX packets:88428 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:289713694 (276.2 MiB)  TX bytes:7698693 (7.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:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1 
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

vethac33a3b Link encap:Ethernet  HWaddr F2:9B:3D:42:9E:B4  
          inet6 addr: fe80::f09b:3dff:fe42:9eb4/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1871 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2128 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:1161525 (1.1 MiB)  TX bytes:1283465 (1.2 MiB)

vethc0fae95 Link encap:Ethernet  HWaddr EE:A2:D7:DD:0E:4C  
          inet6 addr: fe80::eca2:d7ff:fedd:e4c/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:21 errors:0 dropped:0 overruns:0 frame:0
          TX packets:34 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:26497 (25.8 KiB)  TX bytes:3284 (3.2 KiB)

veth43c45fd Link encap:Ethernet  HWaddr FA:06:6C:F7:4E:94  
          inet6 addr: fe80::f806:6cff:fef7:4e94/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:16 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 b)  TX bytes:1296 (1.2 KiB)

veth68aaa11 Link encap:Ethernet  HWaddr 3A:A1:00:65:05:AC  
          inet6 addr: fe80::38a1:ff:fe65:5ac/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:23 errors:0 dropped:0 overruns:0 frame:0
          TX packets:37 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:26610 (25.9 KiB)  TX bytes:3415 (3.3 KiB)

vethc477df7 Link encap:Ethernet  HWaddr AA:CF:39:D8:B3:0E  
          inet6 addr: fe80::a8cf:39ff:fed8:b30e/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:3772 errors:0 dropped:0 overruns:0 frame:0
          TX packets:4439 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:2284090 (2.1 MiB)  TX bytes:4238472 (4.0 MiB)

2、宿主机命令测试

[root@luoahong ~]# netstat -lntup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1166/sshd           
tcp6       0      0 :::22                   :::*                    LISTEN      1166/sshd           
tcp6       0      0 :::32769                :::*                    LISTEN      2629/docker-proxy   
tcp6       0      0 :::32770                :::*                    LISTEN      2871/docker-proxy   
tcp6       0      0 :::32771                :::*                    LISTEN      2884/docker-proxy   
udp        0      0 0.0.0.0:68              0.0.0.0:*                           1110/dhclient       
udp        0      0 0.0.0.0:3255            0.0.0.0:*                           1110/dhclient       
udp        0      0 127.0.0.1:323           0.0.0.0:*                           648/chronyd         
udp6       0      0 :::28221                :::*                                1110/dhclient       
udp6       0      0 ::1:323                 :::*                                648/chronyd         
[root@luoahong ~]#  hostname
luoahong
[root@luoahong ~]# ifconfig
br-ad2616372f01: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.18.0.1  netmask 255.255.0.0  broadcast 172.18.255.255
        inet6 fe80::42:14ff:fe4d:ecb9  prefixlen 64  scopeid 0x20<link>
        ether 02:42:14:4d:ec:b9  txqueuelen 0  (Ethernet)
        RX packets 3772  bytes 2284090 (2.1 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4439  bytes 4238472 (4.0 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        inet6 fe80::42:f7ff:fe2f:d620  prefixlen 64  scopeid 0x20<link>
        ether 02:42:f7:2f:d6:20  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 8  bytes 648 (648.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.231.136  netmask 255.255.255.0  broadcast 192.168.231.255
        inet6 fe80::20c:29ff:fee7:b946  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:e7:b9:46  txqueuelen 1000  (Ethernet)
        RX packets 203331  bytes 289715676 (276.2 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 88441  bytes 7705439 (7.3 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

veth43c45fd: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::f806:6cff:fef7:4e94  prefixlen 64  scopeid 0x20<link>
        ether fa:06:6c:f7:4e:94  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 16  bytes 1296 (1.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

veth68aaa11: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::38a1:ff:fe65:5ac  prefixlen 64  scopeid 0x20<link>
        ether 3a:a1:00:65:05:ac  txqueuelen 0  (Ethernet)
        RX packets 23  bytes 26610 (25.9 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 37  bytes 3415 (3.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

vethac33a3b: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::f09b:3dff:fe42:9eb4  prefixlen 64  scopeid 0x20<link>
        ether f2:9b:3d:42:9e:b4  txqueuelen 0  (Ethernet)
        RX packets 1871  bytes 1161525 (1.1 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2128  bytes 1283465 (1.2 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

vethc0fae95: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::eca2:d7ff:fedd:e4c  prefixlen 64  scopeid 0x20<link>
        ether ee:a2:d7:dd:0e:4c  txqueuelen 0  (Ethernet)
        RX packets 21  bytes 26497 (25.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 34  bytes 3284 (3.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

vethc477df7: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::a8cf:39ff:fed8:b30e  prefixlen 64  scopeid 0x20<link>
        ether aa:cf:39:d8:b3:0e  txqueuelen 0  (Ethernet)
        RX packets 3772  bytes 2284090 (2.1 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4439  bytes 4238472 (4.0 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

测试小结:

  1、主机名和宿主机是一样

        2、netstat -lntup的结果也是一样

        3、ifconfig也是一样,几乎和宿主机分辨不清楚

        4、但是容器看不到主机的程序

3、网络类型查看

   "MacAddress": "",
            "Networks": {
                "host": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "30b37ec09ff25e54c6b8208779335bfe7b01729fc830db0c2c70434ca6817eb2",
                    "EndpointID": "94f41095acd2ba3d85210cc3667fc6e4324d3cacb9c1243a0747b4e46774a811",
                    "Gateway": "",
                    "IPAddress": "",
                    "IPPrefixLen": 0,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "",
                    "DriverOpts": null
                }
            }
        }
    }
]

四、Bridge

简介:Docker设计的NAT网络模型

1、创建容器

docker run -d httpd:latest

 

2、功能测试

[root@luoahong ~]# ifconfig
docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:1a:8d:3d:28  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.228.135  netmask 255.255.255.0  broadcast 192.168.228.255
        inet6 fe80::20c:29ff:fe66:d93a  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:66:d9:3a  txqueuelen 1000  (Ethernet)
        RX packets 235  bytes 25094 (24.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 206  bytes 28905 (28.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

测试小结:docker0就是nat网络,不用过多解释

3、网络类型查看

"Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "d766f2526542a828e4b6e302fcc592a1a945bce83df236b9f24efbbea3eff4f4",
                    "EndpointID": "b2f80a78210a384f6ec5a8acc7f502d55f0858470f04e279a091aadc1912e82d",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:02",
                    "DriverOpts": null
                }
            }
        }
    }
]
原文地址:https://www.cnblogs.com/luoahong/p/10288932.html