0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Docker の実験(その14:ネットワークの概要)

Last updated at Posted at 2023-02-20

その13:イメージの移設・ビルド(レイヤ)キャッシュのつづき

最初の一歩:vm (Container Optimized-os)への入り方(zone は環境による)

$ gcloud auth login
$ gcloud compute ssh --project <project-id> --zone asia-northeast1-b <vm-name>

Docker で使用できるネットワークドライバには、以下がある。

bridge(デフォルト)
host
overlay
macvlan
none

$ docker network ls
NETWORK ID     NAME      DRIVER    SCOPE
5e9118fc9ae3   bridge    bridge    local
b05adf15289a   host      host      local
d3ac94ee3669   none      null      local

Google Cloud の Container Optimized-os では、bridge, host, none が使えるようだ。

docker0 というネットワークアダプタがあるのを確認する。

$ ifconfig
docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1460
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        inet6 fe80::42:3dff:fe1f:f74d  prefixlen 64  scopeid 0x20<link>
        ether 02:42:3d:1f:f7:4d  txqueuelen 0  (Ethernet)
        RX packets 101  bytes 16673 (16.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 106  bytes 520861 (508.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

これを見ると、172.17.0.0/16 ネットワークのようだ。

bridge ネットワークを確認して、subnet と Gateway を確認する。

$ docker inspect bridge
[
    {
        "Name": "bridge",
        "Id": "5e9118fc9ae3388d7311ad99eeec9140a5386381194d378941af2c39bbf8812f",
        "Created": "2023-02-18T12:13:31.170443126Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.17.0.0/16",
                    "Gateway": "172.17.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "67df4d40228057e55850e67d3e9a3bff7ce51fc2a4bfcbefc6ba9b1dbd402942": {
                "Name": "mynginx",
                "EndpointID": "6595b6b7b116d05d442e73221424e520156427025452a46c32d32b4882799d42",
                "MacAddress": "02:42:ac:11:00:02",
                "IPv4Address": "172.17.0.2/16",
                "IPv6Address": ""
            },
            "8229282bc476d6394eee8ad64ff9e9a40a3c678f6fef1ab060c21824c61514a0": {
                "Name": "my_ubuntu",
                "EndpointID": "3e094e4ff87e80616c80d1c14798108bfc65ea52c76ed3a670867d1ea36d6633",
                "MacAddress": "02:42:ac:11:00:04",
                "IPv4Address": "172.17.0.4/16",
                "IPv6Address": ""
            },
            "e6e93cd9f5c5f5b3bc68e5e6fd068d5eb49585d53d46190249e8b141f1408664": {
                "Name": "mycustomnginx",
                "EndpointID": "065ba8c9c8c0157b60fec5af2a301f6d069c898a1e1ea8d1a48c71c17747c1bf",
                "MacAddress": "02:42:ac:11:00:03",
                "IPv4Address": "172.17.0.3/16",
                "IPv6Address": ""
            }
        },
        "Options": {
            "com.docker.network.bridge.default_bridge": "true",
            "com.docker.network.bridge.enable_icc": "true",
            "com.docker.network.bridge.enable_ip_masquerade": "true",
            "com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
            "com.docker.network.bridge.name": "docker0",
            "com.docker.network.driver.mtu": "1460"
        },
        "Labels": {}
    }
]

Gateway が、172.17.0.1 になっていることが確認できる。

docker コンテナを作成する際に、上記ドライバを使ったネットワークを作成することができる。

host ネットワークを調査してみる。

$ docker network inspect host
[
    {
        "Name": "host",
        "Id": "b05adf15289a6f32483a6af3a9ca9fadd2e647548fd1d296bec9b91fe4f505dc",
        "Created": "2023-02-15T22:28:22.077515267Z",
        "Scope": "local",
        "Driver": "host",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": []
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {},
        "Options": {},
        "Labels": {}
    }
]

host ネットワークドライバを使って、コンテナをひとつ作ってみる。

$ docker container run -dt --name myhost --network host ubuntu
48f7a1225f89d78929ac1dc739fef12e674918cd3f236b1687a0cedd50a9800d
$ docker network inspect host
[
    {
        "Name": "host",
        "Id": "b05adf15289a6f32483a6af3a9ca9fadd2e647548fd1d296bec9b91fe4f505dc",
        "Created": "2023-02-15T22:28:22.077515267Z",
        "Scope": "local",
        "Driver": "host",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": []
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "48f7a1225f89d78929ac1dc739fef12e674918cd3f236b1687a0cedd50a9800d": {
                "Name": "myhost",
                "EndpointID": "bf92f4e61c4c4f3c7b2b3976f779eab236f273e4481b5a2e7ddb1af5496bae2a",
                "MacAddress": "",
                "IPv4Address": "",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]
$ docker container exec -it myhost bash
# apt-get update && apt-get install net-tools
# ifconfig
docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1460
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        inet6 fe80::42:3dff:fe1f:f74d  prefixlen 64  scopeid 0x20<link>
        ether 02:42:3d:1f:f7:4d  txqueuelen 0  (Ethernet)
        RX packets 101  bytes 16673 (16.6 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 106  bytes 520861 (520.8 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

IP アドレスを持っていないようだ。

なお、このように、ネットワークドライバを使い分けることにより、ユースケースに応じられるようだ。

その15:ブリッジネットワークにつづく。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?