docker networkを限界まで作る

docker-compose では default networkが自動で生成されるが、いくつまで生成されるのかを検証する

## 結論
デフォルトだと作成できるネットワークの上限は29個
30個目を作ろうとするとエラーになる

## 前提（環境）
Windows + docker-machine

$ `docker version`

```
Client:
 Version:       18.03.0-ce
 API version:   1.30 (downgraded from 1.37)
 Go version:    go1.9.4
 Git commit:    0520e24302
 Built: Fri Mar 23 08:31:36 2018
 OS/Arch:       windows/amd64
 Experimental:  false
 Orchestrator:  swarm

Server:
 Engine:
  Version:      17.06.2-ce
  API version:  1.30 (minimum version 1.12)
  Go version:   go1.8.3
  Git commit:   cec0b72
  Built:        Tue Sep  5 19:59:19 2017
  OS/Arch:      linux/amd64
  Experimental: false
```

$ `docker-machine version`

```
docker-machine.exe version 0.14.0, build 89b8332
```



## 実行コマンド
for文で `my-network-01` ～ `my-network-50` まで docker network を作る

以下を適当なファイルに書いて実行

```bash
#!/bin/bash -ex

for i in $(seq -f "%02g" 1 50)
do
  docker network create "my-network-$i"
done
```

## 実行結果
30個目を作るところでSTOP

```bash
# (略)

+ for i in $(seq -f "%02g" 1 50)
+ docker network create my-network-30
Error response from daemon: could not find an available, non-overlapping IPv4 address pool among the defaults to assign to the network
```

原因はIPv4 address poolが枯渇したため

> could not find an available, non-overlapping IPv4 address pool

### 実行結果全文

```bash
++ seq -f %02g 1 50
+ for i in $(seq -f "%02g" 1 50)
+ docker network create my-network-01
b918e9c784064e05ce54feef2321bc4fd94302ff54fcde1e26fb40a494e1ed28
+ for i in $(seq -f "%02g" 1 50)
+ docker network create my-network-02
2da326ae5ea8d4793f24336419cf6ee4113e2b423f2069d3409a0ac0c418332a
+ for i in $(seq -f "%02g" 1 50)
+ docker network create my-network-03
db820a4b533f028437fc8576157a8c7689a7e01b93aa71cfa4fd6e43f51bbc3e
+ for i in $(seq -f "%02g" 1 50)
+ docker network create my-network-04
f6c02879056aa6669254130a34413ea06f0043dfc18544f72d5f70c895803f1a
+ for i in $(seq -f "%02g" 1 50)
+ docker network create my-network-05
a462aae3264db95d7af6c81ece3c3689b8009a1ee321b612ee0c716705579484
+ for i in $(seq -f "%02g" 1 50)
+ docker network create my-network-06
132635363f4684c0b1a1e1cfe4117aa01140336077676945d8153d0ba8413bb9
+ for i in $(seq -f "%02g" 1 50)
+ docker network create my-network-07
4517a3d45e20a23382a4ee6e1c6451200e8281a47e9e98893c77bc84221ef44a
+ for i in $(seq -f "%02g" 1 50)
+ docker network create my-network-08
e9de9c5af6ac13f6faee503c2a0b7a7bacff73ff3177e553d8ed78a8f1ee972e
+ for i in $(seq -f "%02g" 1 50)
+ docker network create my-network-09
efe02fa89a2191f52d031e81d41edba1dda7c3cd6055b76bf9108dfc7f753dff
+ for i in $(seq -f "%02g" 1 50)
+ docker network create my-network-10
5b43cbf54432cb4c087385751b9e5b3bc3b8420314194d3b89ac51d0d958dc2e
+ for i in $(seq -f "%02g" 1 50)
+ docker network create my-network-11
6ad326bad0fa6c398fc311429ba3affa134cba188f229f26942ca89cec295883
+ for i in $(seq -f "%02g" 1 50)
+ docker network create my-network-12
44ccde35aae6377d3f5a9cc7fc485527a4604004463c169088e9311f6a795086
+ for i in $(seq -f "%02g" 1 50)
+ docker network create my-network-13
b66002844984ca06d3e60fa494a1dbe0cc2783a5c83c7515826360d8352cf19c
+ for i in $(seq -f "%02g" 1 50)
+ docker network create my-network-14
1e19e9ce65ed9ab55e3eb7da633de91a6305aa08b171b0515325f24186fa4b77
+ for i in $(seq -f "%02g" 1 50)
+ docker network create my-network-15
2f27db9f459724ef5f070c1afa87f7433f14b82049be87f82a8b850d9cdd11ce
+ for i in $(seq -f "%02g" 1 50)
+ docker network create my-network-16
1eab3793116f1430aa8dd81056d9a5fa15338fa5f228759f4a7d9025e10cfaf7
+ for i in $(seq -f "%02g" 1 50)
+ docker network create my-network-17
fb0388a3a7aee6ffad93d2ba9fce0464f2497bc7ee5052dcdbbc21702928c290
+ for i in $(seq -f "%02g" 1 50)
+ docker network create my-network-18
815b61418245a4def65fea32e094b4268f9b030766c6583e19e531c00dc4e858
+ for i in $(seq -f "%02g" 1 50)
+ docker network create my-network-19
74b4ec440f67ab775e91668af3dfc3b667bbb1c975de4c0fb59b5229710920c9
+ for i in $(seq -f "%02g" 1 50)
+ docker network create my-network-20
063651b8e89d7ba9ca45f4ff3d429f0109e0340cf58aa6b0768011115937a374
+ for i in $(seq -f "%02g" 1 50)
+ docker network create my-network-21
ae3d8ae48e597262cc1019ea5e2cfcdd799f49f2aa8cef2bd18fc6bcb7870030
+ for i in $(seq -f "%02g" 1 50)
+ docker network create my-network-22
1f01444a680cb106cee27ea183eb7bb7ee11538077d1f4bbc0215d9e770cc979
+ for i in $(seq -f "%02g" 1 50)
+ docker network create my-network-23
b32e45c37cb4c88480b26fc8caa9b5d696781d222c1983add458eaef45895ca5
+ for i in $(seq -f "%02g" 1 50)
+ docker network create my-network-24
aee4ff3c77b9d805e80e98a3d84cac1fe1c2c142848d0535ef63ca5967bba577
+ for i in $(seq -f "%02g" 1 50)
+ docker network create my-network-25
92acf796ade9f34c71b2d32c86ac5ded6ea29cc4b66be9b27e570a550054702c
+ for i in $(seq -f "%02g" 1 50)
+ docker network create my-network-26
f9416a180a0e4c852962786750fc805178aabfb545b31887067a3f7742aa1c89
+ for i in $(seq -f "%02g" 1 50)
+ docker network create my-network-27
95c4cfe4163f231781592da50fe563451fdf3d694d4b61e3f6912b963c8b46e5
+ for i in $(seq -f "%02g" 1 50)
+ docker network create my-network-28
b5cd9ad9f9fcd892aca750ea869778788556f910de1562d14ab6fecaf701dd4d
+ for i in $(seq -f "%02g" 1 50)
+ docker network create my-network-29
d5e8a9442d7780730be687662665ebfdf6a84d5397ee5dcefcfee591ff627fd1
+ for i in $(seq -f "%02g" 1 50)
+ docker network create my-network-30
Error response from daemon: could not find an available, non-overlapping IPv4 address pool among the defaults to assign to the network
```

## ネットワークの状態
### 一覧

$ `docker network ls`

```text
NETWORK ID          NAME                DRIVER              SCOPE
6da9e25885c7        bridge              bridge              local
548c8ceaf804        host                host                local
b918e9c78406        my-network-01       bridge              local
2da326ae5ea8        my-network-02       bridge              local
db820a4b533f        my-network-03       bridge              local
f6c02879056a        my-network-04       bridge              local
a462aae3264d        my-network-05       bridge              local
132635363f46        my-network-06       bridge              local
4517a3d45e20        my-network-07       bridge              local
e9de9c5af6ac        my-network-08       bridge              local
efe02fa89a21        my-network-09       bridge              local
5b43cbf54432        my-network-10       bridge              local
6ad326bad0fa        my-network-11       bridge              local
44ccde35aae6        my-network-12       bridge              local
b66002844984        my-network-13       bridge              local
1e19e9ce65ed        my-network-14       bridge              local
2f27db9f4597        my-network-15       bridge              local
1eab3793116f        my-network-16       bridge              local
fb0388a3a7ae        my-network-17       bridge              local
815b61418245        my-network-18       bridge              local
74b4ec440f67        my-network-19       bridge              local
063651b8e89d        my-network-20       bridge              local
ae3d8ae48e59        my-network-21       bridge              local
1f01444a680c        my-network-22       bridge              local
b32e45c37cb4        my-network-23       bridge              local
aee4ff3c77b9        my-network-24       bridge              local
92acf796ade9        my-network-25       bridge              local
f9416a180a0e        my-network-26       bridge              local
95c4cfe4163f        my-network-27       bridge              local
b5cd9ad9f9fc        my-network-28       bridge              local
d5e8a9442d77        my-network-29       bridge              local
ffed067b8aaf        none                null                local
```

### 各ネットワークのIPレンジ

`docker network inspect` コマンドで 各ネットワークの Subnet を確認

```bash
docker network inspect $(docker network ls -q) | grep -E "Subnet|Name"
```

```
        "Name": "bridge",
                    "Subnet": "172.17.0.0/16",
        "Name": "host",
        "Name": "my-network-01",
                    "Subnet": "172.18.0.0/16",
        "Name": "my-network-02",
                    "Subnet": "172.19.0.0/16",
        "Name": "my-network-03",
                    "Subnet": "172.20.0.0/16",
        "Name": "my-network-04",
                    "Subnet": "172.21.0.0/16",
        "Name": "my-network-05",
                    "Subnet": "172.22.0.0/16",
        "Name": "my-network-06",
                    "Subnet": "172.23.0.0/16",
        "Name": "my-network-07",
                    "Subnet": "172.24.0.0/16",
        "Name": "my-network-08",
                    "Subnet": "172.25.0.0/16",
        "Name": "my-network-09",
                    "Subnet": "172.26.0.0/16",
        "Name": "my-network-10",
                    "Subnet": "172.27.0.0/16",
        "Name": "my-network-11",
                    "Subnet": "172.28.0.0/16",
        "Name": "my-network-12",
                    "Subnet": "172.29.0.0/16",
        "Name": "my-network-13",
                    "Subnet": "172.30.0.0/16",
        "Name": "my-network-14",
                    "Subnet": "172.31.0.0/16",
        "Name": "my-network-15",
                    "Subnet": "192.168.0.0/20",
        "Name": "my-network-16",
                    "Subnet": "192.168.16.0/20",
        "Name": "my-network-17",
                    "Subnet": "192.168.32.0/20",
        "Name": "my-network-18",
                    "Subnet": "192.168.48.0/20",
        "Name": "my-network-19",
                    "Subnet": "192.168.64.0/20",
        "Name": "my-network-20",
                    "Subnet": "192.168.80.0/20",
        "Name": "my-network-21",
                    "Subnet": "192.168.112.0/20",
        "Name": "my-network-22",
                    "Subnet": "192.168.128.0/20",
        "Name": "my-network-23",
                    "Subnet": "192.168.144.0/20",
        "Name": "my-network-24",
                    "Subnet": "192.168.160.0/20",
        "Name": "my-network-25",
                    "Subnet": "192.168.176.0/20",
        "Name": "my-network-26",
                    "Subnet": "192.168.192.0/20",
        "Name": "my-network-27",
                    "Subnet": "192.168.208.0/20",
        "Name": "my-network-28",
                    "Subnet": "192.168.224.0/20",
        "Name": "my-network-29",
                    "Subnet": "192.168.240.0/20",
        "Name": "none",
```

#### 備考
- クラスB(`172.16.0.0/12`) と クラスC(`192.168.0.0/16`) のプライベートIPアドレスが使用されている
- クラスB(`172.16.0.0/12`) は `/16` で `172.18.0.0/16` ～ `172.31.0.0/16` の 14個
    - `172.16.0.0/16`: 未使用（詳細不明）
    - `172.17.0.0/16`: bridge
- クラスC(`192.168.0.0/16`) は `/20` で `192.168.0.0/20` ～ `192.168.240.0/20` の 15個
    - ※ `256 / 16 = 16` だが、 1つ飛ばされているレンジが存在するため `-1` して 15個
    - 飛ばされたレンジ: `192.168.96.0/20`: `docker-machine` の IP(`192.168.99.100`)が含まれているからだと考えられる


$ `docker-machine ip`

```
192.168.99.100
```

## 後片付け
このままだと新規で `docker-compose up` ができなくて困るので、未使用ネットワークを削除する

$ `docker network prune`

```
WARNING! This will remove all networks not used by at least one container.
Are you sure you want to continue? [y/N] y
```

```
Deleted Networks:
my-network-01
my-network-20
my-network-21
my-network-22
my-network-15
my-network-18
my-network-23
my-network-26
my-network-27
my-network-28
my-network-02
my-network-03
my-network-04
my-network-05
my-network-09
my-network-25
my-network-10
my-network-11
my-network-24
my-network-29
my-network-16
my-network-07
my-network-08
my-network-13
my-network-14
my-network-06
my-network-12
my-network-17
my-network-19
```

$ `docker network ls`

```
NETWORK ID          NAME                DRIVER              SCOPE
6da9e25885c7        bridge              bridge              local
548c8ceaf804        host                host                local
ffed067b8aaf        none                null                local
```