码迷,mamicode.com
首页 > 其他好文 > 详细

七、docker的网络类型

时间:2020-12-10 11:24:54      阅读:5      评论:0      收藏:0      [点我收藏+]

标签:eth0   foreign   manage   连接   queue   同步   spl   prefix   detail   

之前学到docker中几大组成:容器、镜像、仓库、卷和网络,今天先了解一下docker的网络。命令可通过docker network查看,在没做docker做任务配置之前,默认的网络主要有三种:

[root@docker-1-26 ~]# docker network --help 

Usage:    docker network COMMAND

Manage networks

Commands:
  connect     Connect a container to a network  #将容器与某一网络连接起来
  create      Create a network    #创建一个网络
  disconnect  Disconnect a container from a network  #将容器与某一网络断开连接
  inspect     Display detailed information on one or more networks   #查看某一网络的详细信息
  ls          List networks    #列出现有网络
  prune       Remove all unused networks    #删除所有没用的网络
  rm          Remove one or more networks    #删除某一网络

Run docker network COMMAND --help for more information on a command.
[root@docker-1-26 ~]# docker network ls 
NETWORK ID          NAME                DRIVER              SCOPE
3db58638ba22        bridge              bridge              local      #bridge网络
2bb25566094c        host                host                local      #主机网络
66221610ad40        none                null                local      #null
[root@docker-1-26 ~]# 

先创建一个网络类型为none的容器

[root@docker-1-26 ~]# docker run -it --network none --privileged=true centos:edityum /usr/sbin/init

[root@docker-1-26 ~]# docker container inspect 125ef2908e5a         
"Networks": { "none": {      #网络类型为none "IPAMConfig": null, "Links": null, "Aliases": null, "NetworkID": "66221610ad40716fe7e97e33630438ef5258c5dd4b009558682ca4a6f39ed636", "EndpointID": "05976925f85e646804eb80c228286499c7a5ce6192efc5a34f0dc69b28a0b407", "Gateway": "", "IPAddress": "", "IPPrefixLen": 0, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "", "DriverOpts": null

[root@125ef2908e5a /]# ip add     #自然也不会有啥IP地址,这种网络没啥屁用
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
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@125ef2908e5a /]#

创建容易时,不指定网络类型时,默认采用bridge,bridge网络类型是docker专门设计的一种NAT网络类型

[root@docker-1-26 ~]# docker run -d kod_desk_php_httpd:v1.1 
45cad3dab343eb7cf814fe62e8cffc3a6543909dcbd3a3fc15eb0563a6b4f879
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "3db58638ba2208c8ed23d76e20953e64594fd0c94a49ce446581a2d0884a26e9",
                    "EndpointID": "667a0a7be93edd9ddf95c3b9759d727414c7ff8736a3a9bf131e2015391535fe",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.5",
                    "IPPrefixLen": 16,

container网络类型:该类型的网络与已经运行的容器共享network namespace

刚才创建了一个bridge网络类型的容器45cad3dab34,IP地址为172.16.0.5;现借助已有的容器,创建一个新容器

[root@docker-1-26 ~]# docker run -it --network container:45 busybox #创建一个busybox的容器,其网络类型为container,并与刚创建的45cad3dab34容器共享network namespace
/ # ip add 
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1
    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
15: eth0@if16: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue 
    link/ether 02:42:ac:11:00:05 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.5/16 brd 172.17.255.255 scope global eth0      #发现IP地址与之前的45cad3dab34容器一样
       valid_lft forever preferred_lft forever

/ # netstat -tnlup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name #正是因为有相同的network namespace,因此两个容器间的端口占用都是同步的!!
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN -
/ #

同理,host网络类型就是要求同宿主机具有相同的network namespace

[root@docker-1-26 ~]# docker run -it --network host busybox:latest 
/ # 
/ # hostname       #容器的用户名都与宿主机一致
docker-1-26
/ # / # ip add       #IP信息也有宿主机相同
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1
    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: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
    link/ether 00:0c:29:dc:55:5d brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.26/24 brd 10.0.0.255 scope global ens32
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fedc:555d/64 scope link 
       valid_lft forever preferred_lft forever
3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue 
    link/ether 02:42:c3:83:bb:d4 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:c3ff:fe83:bbd4/64 scope link 
       valid_lft forever preferred_lft forever
20: veth6cfbc6a@if19: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue master docker0 
    link/ether 36:83:97:44:83:a7 brd ff:ff:ff:ff:ff:ff
    inet6 fe80::3483:97ff:fe44:83a7/64 scope link 
       valid_lft forever preferred_lft forever
22: veth761f133@if21: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue master docker0 
    link/ether ea:56:fc:21:9f:6e brd ff:ff:ff:ff:ff:ff
    inet6 fe80::e856:fcff:fe21:9f6e/64 scope link 
       valid_lft forever preferred_lft forever
24: vethcc61046@if23: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue master docker0 
    link/ether 8a:93:52:14:d1:4a brd ff:ff:ff:ff:ff:ff
    inet6 fe80::8893:52ff:fe14:d14a/64 scope link 
       valid_lft forever preferred_lft forever
/ # 

 

七、docker的网络类型

标签:eth0   foreign   manage   连接   queue   同步   spl   prefix   detail   

原文地址:https://www.cnblogs.com/woshinidaye123/p/14092633.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!