14-容器网络之host和none

14-容器网络之host和none

在之前的小节,我们有看到过 hostnone

通过 docker network ls 查看。

none 网络

  1. 删除 test1 容器

    docker stop test1 && docker rm test1
    
  2. 创建 test1 容器并连接到none网络

    docker run -d --name test1 --network none busybox /bin/sh -c "while true; do sleep 3600; done"
    

    使用 docker ps 查看是否启动运行。

  3. 查看 none 网络

    docker network inspect none
    
    "Containers": {
        "a8eff69c2dc062ba63578359533f49cf8e619aed1762d393714f93ffcfba34d5": {
            "Name": "test1",
            "EndpointID": "d961a29c4293398cfa8dfa58ea70d88ca3071063c6e3773e0102c492bdf387a2",
            "MacAddress": "",
            "IPv4Address": "",
            "IPv6Address": ""
        }
    }
    

    我们发现 test1 容器并没有被分配 ip 地址。

  4. 进入 test1 容器查看

    docker exec -it test1 /bin/sh
    
    / # 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
    

    我们发现这个 test1 容器并没有其它网络接口,只有 lo 接口。那么我们可以理解为这个 test1 它所在的 net namespace 是一个孤立的网络。

    我们只能通过exec 进入这个容器。主要用在哪里呢?一般是创建高安全的容器,比如用来存储密码等,然后我们只能通过exec进入容器,一般来说比较安全。

  5. 详细介绍 lo 网络接口

    lo 代表 127.0.0.1,也就是 localhost,是 loopback interface 缩写,含义为 回环接口

    当你从一台linux主机向自身发送数据包时,实际上的数据包是通过虚拟的lo接口来发送接受的,而不会通过你的物理网卡 eth0/eth1…

    Loopback接口是虚拟接口,是一种纯软件性质的虚拟接口。任何送到该接口的网络数据报文都会被认为是送往设备自身的。

host网络

  1. 删除 test1 容器

    docker stop test1 && docker rm test1
    
  2. 创建 test1 容器并连接到host网络

    docker run -d --name test1 --network host busybox /bin/sh -c "while true; do sleep 3600; done"
    

    使用 docker ps 查看是否启动运行。

  3. 查看 host 网络

    docker network inspect host
    
    "Containers": {
        "3d9350ed5a103364ba26efa1b94556baa5e988d14c50494024e049aae8d35c97": {
            "Name": "test1",
            "EndpointID": "ad353df20fc3ee12777d208edb65555d04fd87be672b4a4eb5284eeeb9a63d32",
            "MacAddress": "",
            "IPv4Address": "",
            "IPv6Address": ""
        }
    }
    

    我们可以看到 test1 容器也是没有 ip 地址的。

  4. 进入 test1 容器查看

    docker exec -it test1 /bin/sh
    

    查看网络

    / # ip a
    

    发现这个返回的内容和在容器外面执行 ip a 是一样的。

    exit
    $: ip a
    

    这说明连接 host network的容器test1,它没有自己的独立的 net namespace,而是使用了 主机所在的 host 共享一个 net namespace

    这种方式创建的一个容器会有什么问题呢?因为共享的网络命名空间,那就意味着可能会出现端口冲突。

端口冲突案例

  1. 创建一个连接host网络的 web1 容器

    docker run -d --network host --name web1 nginx
    

    查看运行情况docker ps,正常运行。

  2. 创建一个连接host网络的 web2 容器

    docker run -d --network host --name web2 nginx
    

    查看运行情况

    [vagrant@10 flask-web]$ docker ps
    CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS                    NAMES
    3ae662c4b4ef        nginx                 "nginx -g 'daemon of…"   2 seconds ago       Up 1 second                                  web2
    

    我们发现刚刚创建的web1 容器不见了,这是为什么呢?原因就是端口冲突了。那我们来验证一下。

  3. 查看web1 容器运行日志

    docker logs web1
    2019/02/13 12:18:17 [emerg] 1#1: bind() to 0.0.0.0:80 failed (98: Address already in use)
    nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
    

    从报错信息中我们看到,80 端口已经被占用,所以web1容器停止运行。

  4. 删除web1和web2

    docker stop web1 && docker rm web1
    docker stop web2 && docker rm web2
    
发布了145 篇原创文章 · 获赞 357 · 访问量 44万+

猜你喜欢

转载自blog.csdn.net/wf19930209/article/details/87381189
今日推荐