Bridge Mode

  1. 在 Docker engine 的主機下 ifconfig,可以看到 docker0 網卡,IP 是 172.17.42.1。
    [root@dyw219 ~]# ifconfig docker0
    docker0   Link encap:Ethernet  HWaddr 56:84:7A:FE:97:99  
              inet addr:172.17.42.1  Bcast:0.0.0.0  Mask:255.255.0.0
              inet6 addr: fe80::5484:7aff:fefe:9799/64 Scope:Link
              UP BROADCAST MULTICAST  MTU:1500  Metric:1
              RX packets:845 errors:0 dropped:0 overruns:0 frame:0
              TX packets:646 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000 
              RX bytes:89928 (87.8 KiB)  TX bytes:49773 (48.6 KiB)
    
  2. 橋接模式 (Bridge Mode) 是 Docker 預設的模式,所以之前沒有指定網路模式的容器,都是橋接模式,現有的容器 25987c5a3e0a 是一個網頁服務器,將它啟動。
    [root@dyw219 ~]# docker ps -a | grep 25987c5a3e0a
    25987c5a3e0a centos/httpd:index "/run-httpd.sh" 17 hours ago Up 8 minutes 0.0.0.0:8080->80/tcp myweb
    [root@dyw219 ~]# docker start 25987c5a3e0a
    25987c5a3e0a
    
  3. docker inspect 可以檢查容器的資訊,其中包含網路,過濾 172 可以查到容器 25987c5a3e0a 的 IP 是 172.17.0.1,閘道 172.17.42.1 ( docker host),遮罩是 16 (255.255.0.0)。
    [root@dyw219 ~]# docker inspect 25987c5a3e0a | grep 172 -A1
            "Gateway": "172.17.42.1",
            "GlobalIPv6Address": "",
    --
            "IPAddress": "172.17.0.1",
            "IPPrefixLen": 16,
    --
                    "Gateway": "172.17.42.1",
                    "IPAddress": "172.17.0.1",
                    "IPPrefixLen": 16,
    
  4. 從 docker host (172.17.42.1) 成功 ping 容器 25987c5a3e0a (172.17.0.1)。
    [root@dyw219 ~]# ping -c2 172.17.0.1
    PING 172.17.0.1 (172.17.0.1) 56(84) bytes of data.
    64 bytes from 172.17.0.1: icmp_seq=1 ttl=64 time=0.083 ms
    64 bytes from 172.17.0.1: icmp_seq=2 ttl=64 time=0.068 ms
    
    --- 172.17.0.1 ping statistics ---
    2 packets transmitted, 2 received, 0% packet loss, time 999ms
    rtt min/avg/max/mdev = 0.068/0.075/0.083/0.011 ms
    
  5. 啟動或再 run 一個容器,現有的容器 0d30277f64dd 是一個 shell (/bin/bash),將它啟動。
    [root@dyw219 ~]# docker ps -a | grep 0d30277f64dd
    0d30277f64dd centos "/bin/bash" 34 hours ago Up 14 minutes         dreamy_tesla
    
    [root@dyw219 ~]# docker start 0d30277f64dd
    
  6. docker inspect 可以檢查容器的資訊,其中包含網路,過濾 172 可以查到容器 0d30277f64dd 的 IP 是 172.17.0.2,閘道 172.17.42.1 ( docker host),遮罩是 16 (255.255.0.0)。
    [root@dyw219 ~]# docker inspect 0d30277f64dd | grep 172 -A1
            "Gateway": "172.17.42.1",
            "GlobalIPv6Address": "",
    --
            "IPAddress": "172.17.0.2",
            "IPPrefixLen": 16,
    --
                    "Gateway": "172.17.42.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
    
  7. 從 docker host (172.17.42.1) 成功 ping 容器 0d30277f64dd (172.17.0.2)。
    [root@dyw219 ~]# ping -c2 172.17.0.2
    PING 172.17.0.2 (172.17.0.2) 56(84) bytes of data.
    64 bytes from 172.17.0.2: icmp_seq=1 ttl=64 time=0.133 ms
    64 bytes from 172.17.0.2: icmp_seq=2 ttl=64 time=0.066 ms
    
    --- 172.17.0.2 ping statistics ---
    2 packets transmitted, 2 received, 0% packet loss, time 999ms
    rtt min/avg/max/mdev = 0.066/0.099/0.133/0.034 ms
    
  8. 目前容器 25987c5a3e0a IP 是 172.17.0.1,容器 0d30277f64dd IP 是172.17.0.2。docker host 上會有兩張 veth 開頭網卡與容器的網卡對應。
    [root@dyw219 ~]# ifconfig | grep ^veth
    veth1ed7335 Link encap:Ethernet  HWaddr B2:1A:5E:A6:B6:B4  
    vethb9f7497 Link encap:Ethernet  HWaddr 22:31:B9:88:8B:45
    
  9. docker run 使用 nandrade1/php-apache image 啟動一個名為 dtest2 的容器於背景 (backgroup)。啟動後,再執行 docker inspect dtest2 查看其網路,使用 pipe grep 過濾 172 字串並導向到 /root/dtest2.ip。