「Docker」ユーザ定義ネットワークを使ってみる

Docker のコンテナ間信を調べている中で ユーザ定義ネットワーク なる存在があることを知りました。ちょっとごにょごにょやってみたのでその結果をメモに残しておきます。

network コマンドを使う — Docker-docs-ja 17.06.Beta ドキュメント

・ユーザ定義ネットワークの作成

docker network create で作成できます。今回は my-network という名前のネットワークを作成します。

[root@centos7 ~]# docker network create my-network
82e282194bb3028818f18ddf0b92a96e8bc07491c99b79649c0b924187885420


現在存在するネットワークの一覧は docker network ls で表示可能です。

[root@centos7 ~]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
c6f259d4cdda        bridge              bridge              local
0a1888dfd481        host                host                local
82e282194bb3        my-network          bridge              local
879e2b6d72c5        none                null                local


docker network inspect でネットワークの詳細情報が確認できます。今回は 「サブネット:172.18.0.0/16」、「ゲートウェイ:172.18.0.1」 という感じで作成されたみたいです(このあたりはオプションで作成時に指定可能なようです)。

[root@centos7 ~]# docker network inspect my-network
[
    {
        "Name": "my-network",
        "Id": "82e282194bb3028818f18ddf0b92a96e8bc07491c99b79649c0b924187885420",
        "Created": "2018-11-18T10:40:27.96862083+09:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "172.18.0.0/16",
                    "Gateway": "172.18.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Containers": {},
        "Options": {},
        "Labels": {}
    }
]


・ユーザ定義ネットワークにコンテナを接続する

作成したユーザ定義ネットワークにコンテナを接続してみます。 ひとまずなにも考えずにコンテナを起動してみます。ネットワーク接続前の IP アドレスは下記の通りです。172.18.0.0/16 のサブネットには所属してません。

[root@centos7 ~]# docker run -it centos:centos7 /bin/bash
[root@03342ebb59ac /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.4  netmask 255.255.0.0  broadcast 0.0.0.0
        inet6 fe80::42:acff:fe11:4  prefixlen 64  scopeid 0x20<link>
        ether 02:42:ac:11:00:04  txqueuelen 0  (Ethernet)
        RX packets 4841  bytes 13505076 (12.8 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 3476  bytes 232165 (226.7 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 0  (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


一度、Ctrl+p、Ctrl+q でコンテナを抜けて docker network connect でコンテナをネットワークに接続します。

[root@centos7 ~]# docker network connect my-network 03342ebb59ac


コンテナにアタッチして再度 IP アドレスを確認してみます。 172.18.0.4 という IP アドレスが割り当てられています。

[root@centos7 ~]# docker attach 03342ebb59ac
[root@03342ebb59ac /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.4  netmask 255.255.0.0  broadcast 0.0.0.0
        inet6 fe80::42:acff:fe11:4  prefixlen 64  scopeid 0x20<link>
        ether 02:42:ac:11:00:04  txqueuelen 0  (Ethernet)
        RX packets 4841  bytes 13505076 (12.8 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 3476  bytes 232165 (226.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.18.0.4  netmask 255.255.0.0  broadcast 0.0.0.0
        inet6 fe80::42:acff:fe12:4  prefixlen 64  scopeid 0x20<link>
        ether 02:42:ac:12:00:04  txqueuelen 0  (Ethernet)
        RX packets 8  bytes 648 (648.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

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 0  (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


・コンテナ間で通信してみる

同じ要領でもう一つコンテナを起動してネットワークに接続してみます。

[root@centos7 ~]# docker run -it centos:centos7 /bin/bash
[root@2872f9ea2f58 /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.5  netmask 255.255.0.0  broadcast 0.0.0.0
        inet6 fe80::42:acff:fe11:5  prefixlen 64  scopeid 0x20<link>
        ether 02:42:ac:11:00:05  txqueuelen 0  (Ethernet)
        RX packets 5662  bytes 13701449 (13.0 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4693  bytes 344177 (336.1 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 0  (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
[root@centos7 ~]# docker network connect my-network 2872f9ea2f58
[root@centos7 ~]# docker attach 2872f9ea2f58
[root@2872f9ea2f58 /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.5  netmask 255.255.0.0  broadcast 0.0.0.0
        inet6 fe80::42:acff:fe11:5  prefixlen 64  scopeid 0x20<link>
        ether 02:42:ac:11:00:05  txqueuelen 0  (Ethernet)
        RX packets 5663  bytes 13701491 (13.0 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4694  bytes 344219 (336.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.18.0.5  netmask 255.255.0.0  broadcast 0.0.0.0
        inet6 fe80::42:acff:fe12:5  prefixlen 64  scopeid 0x20<link>
        ether 02:42:ac:12:00:05  txqueuelen 0  (Ethernet)
        RX packets 8  bytes 648 (648.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

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 0  (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


今度は 172.18.0.5IPアドレスが割り当てられています。 こちらのコンテナから 172.18.0.4 に対して PING を飛ばしてみると無事疎通できてます。

[root@2872f9ea2f58 /]# ping 172.18.0.4
PING 172.18.0.4 (172.18.0.4) 56(84) bytes of data.
64 bytes from 172.18.0.4: icmp_seq=1 ttl=64 time=0.179 ms
64 bytes from 172.18.0.4: icmp_seq=2 ttl=64 time=0.104 ms
^C
--- 172.18.0.4 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.104/0.141/0.179/0.039 ms


コンテナID でもちゃんと名前解決して疎通できるようになってます。

[root@2872f9ea2f58 /]# ping 03342ebb59ac
PING 03342ebb59ac (172.18.0.4) 56(84) bytes of data.
64 bytes from 03342ebb59ac.my-network (172.18.0.4): icmp_seq=1 ttl=64 time=0.104 ms
64 bytes from 03342ebb59ac.my-network (172.18.0.4): icmp_seq=2 ttl=64 time=0.119 ms
64 bytes from 03342ebb59ac.my-network (172.18.0.4): icmp_seq=3 ttl=64 time=0.107 ms
^C
--- 03342ebb59ac ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.104/0.110/0.119/0.006 ms


ユーザ定義ネットワークに接続していないコンテナからは 172.18.0.0/\16 に対して疎通はできないようになってます。

[root@centos7 ~]# docker run -it centos:centos7 /bin/bash
[root@0e245d4f7aed /]# ping 172.18.0.4
PING 172.18.0.4 (172.18.0.4) 56(84) bytes of data.
^C
--- 172.18.0.4 ping statistics ---
22 packets transmitted, 0 received, 100% packet loss, time 21016ms


・コンテナ起動時にユーザ定義ネットワークのみに所属させる

普通に docker run した場合、デフォルトで bridge ネットワークに所属するようです(上述のifconfigで言うところの 172.17.0.0/16 のネットワーク)。 ユーザ定義ネットワークのみに所属させたい場合は docker run-net オプションでネットワークを指定すればよいようです。

[root@centos7 ~]# docker run --net=my-network -it centos:centos7 /bin/bash
[root@bdf18a5d88ca /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.18.0.6  netmask 255.255.0.0  broadcast 0.0.0.0
        inet6 fe80::42:acff:fe12:6  prefixlen 64  scopeid 0x20<link>
        ether 02:42:ac:12:00:06  txqueuelen 0  (Ethernet)
        RX packets 6214  bytes 13602475 (12.9 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4607  bytes 316146 (308.7 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 0  (Local Loopback)
        RX packets 69  bytes 5610 (5.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 69  bytes 5610 (5.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0


やっぱりネットワークって苦手です。 いろいろ怪しい部分がありそうな気がしますが、こんなところで。

以上です。

[環境情報]
CentOS 7
Docker version 1.13.2