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

一、None类型

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

1、创建容器

docker run -it --network none busubox:latest

2、功能测试

[[email protected] ~]# 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、网络类型查看

[[email protected] ~]# 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、创建容器

[[email protected] ~]# docker run -it --network host centos:6.9

2、功能测试

1、容器命令测试

[[email protected] /]# 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                     :::*                                    -
[[email protected] /]# hostname
luoahong
[[email protected] /]# 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、宿主机命令测试

[[email protected] ~]# 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
[[email protected] ~]#  hostname
luoahong
[[email protected] ~]# 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、功能测试

[[email protected] ~]# 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

时间: 2024-07-29 22:51:47

Docker:容器的四种网络类型 [十三]的相关文章

云计算进阶学习路线图课件:Docker容器的四种网络模式

Docker容器是一个开源的应用容器引擎,让开发者可以以统一的方式打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何安装Docker引擎的服务器上,也可以实现虚拟化.随着云计算的飞速发展以及企业需求的多样化,Docker容器技术成为云计算人才必备的技能之一.很多人想要快速掌握Docker容器技术,接下来就给大家讲解Docker容器的四种网络模式. 1.closed container 封闭式网络模式 没有网络协议栈的通信使用none模式,Docker容器拥有自己的Network Nam

[docker]docker的四种网络方式

声明: 本博客欢迎转发,但请保留原作者信息! 博客地址:http://blog.csdn.net/halcyonbaby 内容系本人学习.研究和总结,如有雷同,实属荣幸! bridge方式(默认) Host IP为186.100.8.117, 容器网络为172.17.0.0/16下边我们看下docker所提供的四种网络:创建容器:(由于是默认设置,这里没指定网络--net="bridge".另外可以看到容器内创建了eth0) [[email protected] ~]# docker

2.2 docker四种网络模式

Docker 网络管理 - 四种网络模式 * host模式   (容器的网络,寄居于宿主机.容器和宿主机网络一样.) 使用docker run时使用--net=host指定 docker使用的网络实际上和宿主机一样,在容器内看到的网卡ip是宿主机上的ip.相当于寄存于宿主机的网络.它有一定的局限性,如容器里开启了80端口,宿主机也开启了80端口,会产生冲突. 例如:docker run -it --rm --net=host centos bash   #退出容器,自动删除容器. * conta

docker: 四种网络模式

Docker 四种网络模式 四种网络模式摘自 Docker 网络详解及 pipework 源码解读与实践 docker run 创建 Docker 容器时,可以用 --net 选项指定容器的网络模式,Docker 有以下 4 种网络模式: host 模式,使用 --net=host 指定. container 模式,使用 --net=container:NAMEorID 指定. none 模式,使用 --net=none 指定. bridge 模式,使用 --net=bridge 指定,默认设置

快速理解VirtualBox的四种网络连接方式

VirtualBox中有4中网络连接方式: NAT Bridged Adapter Internal Host-only Adapter VMWare中有三种,其实他跟VMWare 的网络连接方式都是一样概念,只是比VMWare多了Internal方式. 要让自己(或别人)理解深刻,方法就是做比较和打比方,比较之间的不同和相同,拿熟知的事物打比方.先来一张图,通过这张图就很容易看出这4种方式的区别: (注:此图直接取至Finalbug的Blog,表示感谢) 再来用文字做详细的解释(其实归结起来就

Delphi中定义了四种布尔类型:Boolean,ByteBool,WordBool和LongBool。后面三种布尔类型是为了与其他语言兼容而引入的

bool是LongBool类型. Delphi中定义了四种布尔类型:Boolean,ByteBool,WordBool和LongBool.后面三种布尔类型是为了与其他语言兼容而引入的,一般情况下建议使用Boolean类型. 这四种类型的布尔值占用内存的数量如下: Boolean 1 Byte ByteBool 1 Byte WordBool 2 Bytes(1 Word) LongBool 4 Bytes(2 Words) 对于ByteBool,WordBool和LongBool三种类型True

【转】虚拟机四种网络连接模式比较

转载地址:http://blog.csdn.net/terryzero/article/details/6016130 虚拟机一直用,但选择网络时的四种模式总是搞不清楚,只知道选择bridge最好用.为了能更深入了了解,查询了些资料,总结如下 第一种 NAT模式 Vhost访问网络的所有数据都是由主机提供的,vhost并不真实存在于网络中,主机与网络中的任何机器都不能查看和访问到Vhost的存在. 虚拟机与主机:虚拟机可以通过网络访问到主机,主机无法通过网络访问到虚拟机. 虚拟机与其他主机:虚拟

MySQL有四种BLOB类型

先说明一下Blob的类型,直接从网上摘抄了!!!1.MySQL有四种BLOB类型: ·tinyblob:仅255个字符 ·blob:最大限制到65K字节 ·mediumblob:限制到16M字节 ·longblob:可达4GB2.除了类型对后面存取文件大小有限制,还要修改mysql的配置文件. Windows.linux基本一样通过修改文件my.ini或my.cnf文件,在文件中增加 max_allowed_packet=10M(就是最大10M,mysql默认似乎1MB,增加前先查找一下确保没有

VMware vSphere四种迁移类型的区别与适应场景

最近一直刚开始接触VMware vSphere这款虚拟化软件,每天的过程都是上午学新知识不理解,痛苦:下午实验各种出错,折磨:晚上回顾一天所学,五味陈杂,不过相比上午,下午已经好很多了.然后第二天依旧如此.虽然每天很受挫不过过得还挺充实. 昨天把四种迁移搞明白就很开心,这里给大家分享一下,愿你们看后有点收获,少走点弯路. 正文开始 下面这张图是这篇文字的核心内容.下文就是按这张图展开讲解的. 先简要解释一下四种迁移类型的基本概念 迁移–将虚拟机从一台主机或数据存储移到另一台主机或数据存储. 迁移