docker-1.10.0-rc1で固定IPを試す

dockerで固定IPを試す。

docker-1.10.0-rc1が1週間前にリリースされました。
待望の固定IP設定が出来るようなので試した見ようと思います。

# docker-1.10.0-rc1で固定IP

## Kernelの更新
dockerの1.9以上はKernelを3.16以上が必要らしいので。

```lang:現状
$ uname -r
3.10.0-229.14.1.el7.x86_64
```

### リポジトリ
```
$ sudo rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
$ sudo rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-2.el7.elrepo.noarch.rpm
```

### カーネルインストール
```
$ sudo yum --enablerepo=elrepo-kernel install kernel-ml
```

### grub
grubを１番目のカーネルで起動

```
$ awk -F\' '$1=="menuentry " {print $2}' /boot/grub2/grub.cfg
CentOS Linux (4.3.0-1.el7.elrepo.x86_64) 7 (Core)
CentOS Linux 7 (Core), with Linux 3.10.0-229.14.1.el7.x86_64
CentOS Linux 7 (Core), with Linux 0-rescue-0af04d3c78a943ae8f3cc26602e374f2

$ sudo grub2-set-default 0
$ sudo grub2-mkconfig -o /boot/grub2/grub.cfg
$ sudo reboot
```

### カーネルの確認

```
$ uname -r
4.3.0-1.el7.elrepo.x86_64
```

## インストール
CentOS7にyumで普通にdockerをインストールし、
テストリポジトリからdocker-1.10.0-rc1のバイナリを取得して既存のdockerと入れ替えます。

```
$ sudo yum install -y docker
$ curl -o docker-1.10.0-rc1 https://test.docker.com/builds/Linux/x86_64/docker-1.10.0-rc1
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 33.1M  100 33.1M    0     0   933k      0  0:00:36  0:00:36 --:--:--  541k
$ ls
docker-1.10.0-rc1
```

既存に上書きして起動。

```
$ sudo cp docker-1.10.0-rc1 /usr/bin/docker
$ sudo chmod +x /usr/bin/docker
$ sudo systemctl start docker
$ sudo systemctl enable docker
```

確認

```
$ sudo docker info
Containers: 0
 Running: 0
 Paused: 0
 Stopped: 0
Images: 0
Server Version: 1.10.0-rc1
Storage Driver: devicemapper
 Pool Name: docker-253:1-16848235-pool
 Pool Blocksize: 65.54 kB
 Base Device Size: 107.4 GB
 Backing Filesystem: xfs
 Data file: /dev/loop0
 Metadata file: /dev/loop1
 Data Space Used: 53.74 MB
 Data Space Total: 107.4 GB
 Data Space Available: 5.003 GB
 Metadata Space Used: 606.2 kB
 Metadata Space Total: 2.147 GB
 Metadata Space Available: 2.147 GB
 Udev Sync Supported: false
 Deferred Removal Enabled: false
 Deferred Deletion Enabled: false
 Deferred Deleted Device Count: 0
 Data loop file: /var/lib/docker/devicemapper/devicemapper/data
 WARNING: Usage of loopback devices is strongly discouraged for production use. Either use `--storage-opt dm.thinpooldev` or use `--storage-opt dm.no_warn_on_loop_devices=true` to suppress this warning.
 Metadata loop file: /var/lib/docker/devicemapper/devicemapper/metadata
 Library Version: 1.02.82 (2013-10-04)
Execution Driver: native-0.2
Logging Driver: json-file
Plugins: 
 Volume: local
 Network: bridge null host
Kernel Version: 4.3.2-1.el7.elrepo.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 1.954 GiB
Name: node01.protosoft
ID: DHKO:7OBL:TVCT:EC4N:SFY4:5YXC:65Y6:HWJW:2OCG:KHGC:E44Q:4FFA
WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled
```

1.10になりました。

## 固定IP指定
docker-1.10.0-rc1リリースノートに
> Add --ip and --ip6 on run and network connect to support custom IP addresses for a container in a network

と記載されていますので、さっそくやってみます。
まずはわかりやすくするためにも、ネットワークを作成します。

```
$ sudo docker network create --subnet=192.168.0.0/16 --gateway=192.168.0.100 --ip-range=192.168.0.0/24 zum-network
d92a77f0c21ba0695eba78e6de4c4397f11678600c9e27f407e434cc997a2fd7
$ sudo docker network ls
NETWORK ID          NAME                DRIVER
dde63cc40841        none                null                
87d098f679b9        host                host                
d92a77f0c21b        zum-network         bridge              
6a18a454f8df        bridge              bridge              
$ sudo docker network inspect zum-network
[
    {
        "Name": "zum-network",
        "Id": "d92a77f0c21ba0695eba78e6de4c4397f11678600c9e27f407e434cc997a2fd7",
        "Scope": "local",
        "Driver": "bridge",
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "192.168.0.0/16",
                    "IPRange": "192.168.0.0/24",
                    "Gateway": "192.168.0.100"
                }
            ]
        },
        "Containers": {},
        "Options": {}
    }
]
```

作成したzum-networkに**192.168.0.10**で固定してみます。

```
$ sudo docker run -d --net=zum-network --ip=192.168.0.10 busybox top
d95301a72b7d091b563e64ab5ab2610f86821db9c3049a0394f1d63b73d33766
$ sudo docker network inspect zum-network
[
    {
        "Name": "zum-network",
        "Id": "d92a77f0c21ba0695eba78e6de4c4397f11678600c9e27f407e434cc997a2fd7",
        "Scope": "local",
        "Driver": "bridge",
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "192.168.0.0/16",
                    "IPRange": "192.168.0.0/24",
                    "Gateway": "192.168.0.100"
                }
            ]
        },
        "Containers": {
            "d95301a72b7d091b563e64ab5ab2610f86821db9c3049a0394f1d63b73d33766": {
                "Name": "serene_sammet",
                "EndpointID": "76a99b675165904e183f71ebe28b6dccb8f06243a0ab5c4bd31e6517128fee55",
                "MacAddress": "02:42:c0:a8:00:0a",
                "IPv4Address": "192.168.0.10/16",
                "IPv6Address": ""
            }
        },
        "Options": {}
    }
]
[vagrant@node01 ~]$ 
```

もうひとつ、同じネットワークに192.168.0.20でコンテナを追加します。

```
$ sudo docker run -d --net=zum-network --ip=192.168.0.20 busybox top
9a7f6ea57ebd40363a7309b35539a6470fb81a8a885a3b400fe11108c6f3b625
$ sudo docker network inspect zum-network
[
    {
        "Name": "zum-network",
        "Id": "d92a77f0c21ba0695eba78e6de4c4397f11678600c9e27f407e434cc997a2fd7",
        "Scope": "local",
        "Driver": "bridge",
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "192.168.0.0/16",
                    "IPRange": "192.168.0.0/24",
                    "Gateway": "192.168.0.100"
                }
            ]
        },
        "Containers": {
            "9a7f6ea57ebd40363a7309b35539a6470fb81a8a885a3b400fe11108c6f3b625": {
                "Name": "pedantic_torvalds",
                "EndpointID": "dbd97a62d3fd922ecde415622001aec5e41a24a10aa7a59fc9c259993e9eb2fc",
                "MacAddress": "02:42:c0:a8:00:14",
                "IPv4Address": "192.168.0.20/16",
                "IPv6Address": ""
            },
            "d95301a72b7d091b563e64ab5ab2610f86821db9c3049a0394f1d63b73d33766": {
                "Name": "serene_sammet",
                "EndpointID": "76a99b675165904e183f71ebe28b6dccb8f06243a0ab5c4bd31e6517128fee55",
                "MacAddress": "02:42:c0:a8:00:0a",
                "IPv4Address": "192.168.0.10/16",
                "IPv6Address": ""
            }
        },
        "Options": {}
    }
]
```

こんな感じでrun時にIPアドレスを指定できました。
とはいえ、これは単一ホスト内に閉じられた状態です。
この流れで、1.9から取り込まれたマルチホスト対応も合わせて検証してみようかと思ったり。