033、如何自定义容器网络(2019-02-20 周三)
docker 网络除了 none、host、bridge这三个自动创建的网络,用户也可以根据业务需求创建 user-defined 网络
docker 提供了三种user-defined 网络驱动:bridge、overlay和macvlan。overlay和macvlan用于创建跨主机的网络,将在后面的章节进行学习。
创建自定义bridge网络
root@docker-lab:~# docker network ls
NETWORK ID NAME DRIVER SCOPE
b41bf72cd691 bridge bridge local
e5cb8d603efd host host local
f9dc6032baba none null local
root@docker-lab:~# brctl show
bridge name bridge id STP enabled interfaces
docker0 8000.02420e6cb404 no
root@docker-lab:~# docker network create --driver bridge my_net01
b4372dfaff116af33202d3c4d8ce887da85a08870a4b77811882f8cdd69a19bf
root@docker-lab:~# docker network ls
NETWORK ID NAME DRIVER SCOPE
b41bf72cd691 bridge bridge local
e5cb8d603efd host host local
b4372dfaff11 my_net01 bridge local
f9dc6032baba none null local
root@docker-lab:~# brctl show
bridge name bridge id STP enabled interfaces
br-b4372dfaff11 8000.02427eb674da no
docker0 8000.02420e6cb404 no
root@docker-lab:~# docker network inspect my_net01
[
{
"Name": "my_net01",
"Id": "b4372dfaff116af33202d3c4d8ce887da85a08870a4b77811882f8cdd69a19bf",
"Created": "2019-02-20T09:03:12.025450041+08: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": {}
}
]
root@docker-lab:~# ifconfig
br-b4372dfaff11 Link encap:Ethernet HWaddr 02:42:7e:b6:74:da
inet addr:172.18.0.1 Bcast:172.18.255.255 Mask:255.255.0.0
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
在创建网络的时候会自动分配一段ip地址,我们也可以在创建网络的时候指定
在创建容器的时候,我们还可以给容器指定IP,但必须是使用 --subnet 参数创建的网络才可以
root@docker-lab:~# docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 my_net02
9b5a95b43caef588095b39b4a5ddfd48ed008985f6e4b364db8239eeee18d5ba
root@docker-lab:~# ifconfig
br-9b5a95b43cae Link encap:Ethernet HWaddr 02:42:ee:8c:e8:3f
inet addr:192.168.0.1 Bcast:192.168.255.255 Mask:255.255.0.0
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
root@docker-lab:~# docker network inspect my_net02
[
{
"Name": "my_net02",
"Id": "9b5a95b43caef588095b39b4a5ddfd48ed008985f6e4b364db8239eeee18d5ba",
"Created": "2019-02-20T09:10:31.02951614+08:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "192.168.0.0/16",
"Gateway": "192.168.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {},
"Options": {},
"Labels": {}
}
]
root@docker-lab:~# docker run -it --network=my_net02 busybox
/ # ip address
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1
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
5496: eth0@if5497: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
link/ether 02:42:c0:a8:00:02 brd ff:ff:ff:ff:ff:ff
inet 192.168.0.2/16 brd 192.168.255.255 scope global eth0
valid_lft forever preferred_lft forever
root@docker-lab:~# docker run -it --network=my_net02 --ip 192.168.9.9 busybox
/ # ip address
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1
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
5498: eth0@if5499: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
link/ether 02:42:c0:a8:09:09 brd ff:ff:ff:ff:ff:ff
inet 192.168.9.9/16 brd 192.168.255.255 scope global eth0
valid_lft forever preferred_lft forever
/ #