Open1
docker network create で割り当てる IP アドレスを固定する
$ docker network create --driver=bridge --subnet=10.20.0.0/20 --ip-range=10.20.2.0/24 --gateway=10.20.3.1 network-name
確認
$ docker network inspect network-name
[
{
"Name": "network-name",
"Id": "69244ead33f8c228855ff4e8a6cb40a0f38e9037c797b149b8cfbf1b7bf2b958",
"Created": "2024-02-19T06:04:18.969095477Z",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "10.20.0.0/22",
"IPRange": "10.20.2.0/24",
"Gateway": "10.20.3.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {},
"Options": {},
"Labels": {}
}
]
$ ip addr
...(略)...
65: br-69244ead33f8: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
link/ether 02:42:21:6e:a2:5f brd ff:ff:ff:ff:ff:ff
inet 10.20.3.1/22 brd 10.20.3.255 scope global br-69244ead33f8
valid_lft forever preferred_lft forever
コンテナ立ち上げて確認
$ docker run --network=network-name --name cont-name -it ubuntu:latest
root@60c0ae5739c9:/# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.20.2.0 netmask 255.255.252.0 broadcast 10.20.3.255
ether 02:42:0a:14:02:00 txqueuelen 0 (Ethernet)
RX packets 6910 bytes 31407261 (31.4 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 4372 bytes 293851 (293.8 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ホストでも確認
$ docker network inspect network-name
[
{
"Name": "network-name",
"Id": "69244ead33f8c228855ff4e8a6cb40a0f38e9037c797b149b8cfbf1b7bf2b958",
"Created": "2024-02-19T06:04:18.969095477Z",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "10.20.0.0/22",
"IPRange": "10.20.2.0/24",
"Gateway": "10.20.3.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"60c0ae5739c9c408d3d7475853ead42e90102c693b3bd7101e957ab2cbb9e284": {
"Name": "cont-name",
"EndpointID": "95d9d55a494686b108eff346a664a7c80bf9a81745c7908a2adb1418e79bf88c",
"MacAddress": "02:42:0a:14:02:00",
"IPv4Address": "10.20.2.0/22",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]