Dockerコンテナ起動（所属NW、hostname、IP設定）

## コンテナ起動（所属NW、hostname、IP設定）

### 1.コンテナが所属するnetwork(bridge)を作成
```
$ docker network create --subnet=192.168.100.0/24 grpA
329032179dbfd9078dec38b7aa57916b0742810a0206d5628e3b24271556619b

$ docker network ls
NETWORK ID     NAME      DRIVER    SCOPE
a864aff6e6ba   bridge    bridge    local
329032179dbf   grpA      bridge    local
6bc8749daf2c   host      host      local
32de95d9e39f   none      null      local
```

#### ホストマシンでNW追加の確認
```
$ ip a
(cut)
9: br-329032179dbf: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default 
    link/ether 02:42:6b:b8:ff:6c brd ff:ff:ff:ff:ff:ff
    inet 192.168.100.1/24 brd 192.168.100.255 scope global br-329032179dbf
       valid_lft forever preferred_lft forever
```

### 2.コンテナのホスト名と所属ネットワークグループとIPアドレスを設定
```
$ docker run -it --name cont_taka -h hostTaka --net=grpA --ip=192.168.100.11 ubuntu /bin/bash
root@hostTaka:/# 

#hostname確認
root@hostTaka:/# hostname
hostTaka

#ip確認(#apt update しないとapt-getコマンドで「E: Unable to locate package」エラー表示)
root@inetTaka:/# apt update
(cut)
root@inetTaka:/# apt-get install -y iproute2
(cut)
root@hostTaka:/# 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
24: eth0@if25: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:c0:a8:64:0b brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 192.168.100.11/24 brd 192.168.100.255 scope global eth0
       valid_lft forever preferred_lft forever

root@hostTaka:/# exit
exit
```

### 3.コンテナ起動
```
$ docker start cont_taka

#所属ネットワークグループの確認
$ docker network inspect grpA
[
    {
        "Name": "grpA",
        "Id": "329032179dbfd9078dec38b7aa57916b0742810a0206d5628e3b24271556619b",
        "Created": "2022-07-31T19:53:06.076203245+09:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "192.168.100.0/24"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "04423ea57d3c9f74c10070f7f5c7bcd75baf7a12193d91a125cdd3ff3e8f9773": {
                "Name": "cont_taka",
                "EndpointID": "6728da4001023f57c3c3d0733a6db95990042f8c99dee95dfc9d81e56eeaff85",
                "MacAddress": "02:42:c0:a8:64:0b",
                "IPv4Address": "192.168.100.11/24",　　　　#←ここに入る
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]
```

#### 3.1 ホストマシンから疎通確認
```
$ ping 192.168.100.11
PING 192.168.100.11 (192.168.100.11) 56(84) bytes of data.
64 bytes from 192.168.100.11: icmp_seq=1 ttl=64 time=0.134 ms
64 bytes from 192.168.100.11: icmp_seq=2 ttl=64 time=0.071 ms
64 bytes from 192.168.100.11: icmp_seq=3 ttl=64 time=0.078 ms
^C
--- 192.168.100.11 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2036ms
rtt min/avg/max/mdev = 0.071/0.094/0.134/0.028 ms
:~$ 
```