Dockerコンテナのネットワーク確認〜作成〜所属〜削除

## ネットワーク構成情報確認
*$ docker network ls*
```
#インストール時のデフォルト設定
$ sudo docker network ls
NETWORK ID     NAME      DRIVER    SCOPE
51f59c777a19   bridge    bridge    local　　← 明示的な指定がなければ、コンテナ作成時にbridge接続が利用される
6bc8749daf2c   host      host      local
32de95d9e39f   none      null      local
```

## 各コンテナのネットワーク確認
*docker container inspect XXX*
```
#コマンド結果抜粋
$ sudo docker container inspect test2 | grep -A 16 "Networks"
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "51f59c777a199c644ebbc41bb49f942ac0ff5f9a3e89cf8fe40e336fc4b43c7e",
                    "EndpointID": "a25ada358938cc841b35af9ab29cc9a333441abe081b23b0e79dd20531f4506d",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:02",
                    "DriverOpts": null
                }
            }
```

## ネットワークの作成
*docker network create*
### ブリッジネットワーク作成
```
$ sudo docker network create --driver=bridge web-network
6b24c088c60bf091e12ca42a85e292b091e8e5ac27406512d3525845f02b8bd4

$ sudo docker network ls
NETWORK ID     NAME          DRIVER    SCOPE
51f59c777a19   bridge        bridge    local
6bc8749daf2c   host          host      local
32de95d9e39f   none          null      local
6b24c088c60b   web-network   bridge    local  ←新規作成されたネットワーク
```

### 作成したネットワークの確認
*$ docker network inspect network-name*
```
$ sudo docker network inspect web-network
[
    {
        "Name": "web-network",
        "Id": "6b24c088c60bf091e12ca42a85e292b091e8e5ac27406512d3525845f02b8bd4",
        "Created": "2022-07-17T20:55:14.327441575+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,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {},
        "Options": {},
        "Labels": {}
    }
]
```

## 作成済みネットワークへの接続
*$ docker network connect [option] network-name container-name*
```
$ sudo docker network connect web-network test2

$ sudo docker container start test2
test2
```
### 作成済みネットワークへの所属確認
```
$ sudo docker container inspect test2 | grep -A 16 "web-network"
                "web-network": {
                    "IPAMConfig": {},
                    "Links": null,
                    "Aliases": [
                        "1917c7f76a5f"
                    ],
                    "NetworkID": "6b24c088c60bf091e12ca42a85e292b091e8e5ac27406512d3525845f02b8bd4",
                    "EndpointID": "62c4bcd021fdb0f86c145650b1b798de38ff8d51eec39cfb3bfdc23649d46c48",
                    "Gateway": "172.18.0.1",
                    "IPAddress": "172.18.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:12:00:02",
                    "DriverOpts": {}
                }

#コンテナに接続してIP確認
$ sudo docker container exec -it test2 /bin/bash
[root@1917c7f76a5f /]# 

[root@1917c7f76a5f /]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default 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
9: eth0@if10: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever
11: eth1@if12: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:ac:12:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 172.18.0.2/16 brd 172.18.255.255 scope global eth1
       valid_lft forever preferred_lft forever

#googleへの接続確認
[root@1917c7f76a5f /]# ping -c 1 google.com
PING google.com (172.217.161.78) 56(84) bytes of data.
64 bytes from nrt20s09-in-f14.1e100.net (172.217.161.78): icmp_seq=1 ttl=114 time=13.7 ms

--- google.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 13.730/13.730/13.730/0.000 ms

[root@1917c7f76a5f /]# exit
exit
```

## 所属しているネットワークからの切断
*$ docker network disconnect network-name container-name*
```
$ sudo docker network disconnect web-network test2
$ sudo docker container inspect test2 | grep -A 16 "web-network"
$    ← 切断されたので表示されない

#切断後はデフォルトのbridge接続に戻っている
$ sudo docker container inspect test2 | grep -A 16 "Networks"
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "01cbf8917a82c4506710c8a0a064378cf7507dab293c0054a1963ecb91acf6e6",
                    "EndpointID": "5ea806ec3f1d466b83439f64d4bd172951626749d6697b6739125389f8f1c734",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:02",
                    "DriverOpts": null
                }
            }

```


## 作成したネットワークの削除
*$ docker network rm*
```
$ sudo docker network ls
NETWORK ID     NAME          DRIVER    SCOPE
01cbf8917a82   bridge        bridge    local
6bc8749daf2c   host          host      local
32de95d9e39f   none          null      local
6b24c088c60b   web-network   bridge    local

#web-network を削除
$ sudo docker network rm web-network
web-network

$ sudo docker network ls
NETWORK ID     NAME      DRIVER    SCOPE
01cbf8917a82   bridge    bridge    local
6bc8749daf2c   host      host      local
32de95d9e39f   none      null      local
```