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
/ #