使用docker network create命令来创建,只能创建docker内建的网络模式
使用docker plugin,创建自定义网络
使用docker命令创建网络
Docker中内置的网络模式包括如下几种:
bridge 我们基于该网络模式创建了mynet网络
host 本地网络模式
macvlan 这个模式貌似是最新加的
null 无网络
overlay 用于swarm集群中容器的跨主机网络访问
docker create network命令包含以下参数:
Flag shorthand -h has been deprecated, please use --help
Usage: docker network create [OPTIONS] NETWORK
Create a network
Options:
--attachable Enable manual container attachment
--aux-address map Auxiliary IPv4 or IPv6 addresses used by Network driver (default map[])
-d, --driver string Driver to manage the Network (default "bridge")
--gateway stringSlice IPv4 or IPv6 Gateway for the master subnet
--help Print usage
--internal Restrict external access to the network
--ip-range stringSlice Allocate container ip from a sub-range
--ipam-driver string IP Address Management Driver (default "default")
--ipam-opt map Set IPAM driver specific options (default map[])
--ipv6 Enable IPv6 networking
--label list Set metadata on a network (default [])
-o, --opt map Set driver specific options (default map[])
--subnet stringSlice Subnet in CIDR format that represents a network segment
创建overlay模式的全局网络,我们可以看到新创建的mynet1的scope是swarm,即集群范围可见的。
172.18.0.1:root@sz-pg-oam-docker-test-001:/root]# docker network create -d overlay mynet1
x81fu4ohqot2ufbpoa2u8vyx3
172.18.0.1:root@sz-pg-oam-docker-test-001:/root]# docker network ls
NETWORK ID NAME DRIVER SCOPE
ad3023f6d324 bridge bridge local
346c0fe30055 crane_default bridge local
4da289d8e48a docker_gwbridge bridge local
3d636dff00da host host local
tx49ev228p5l ingress overlay swarm
x81fu4ohqot2 mynet1 overlay swarm
cc14ee093707 none null local
172.18.0.1:root@sz-pg-oam-docker-test-001:/root]# docker network inspect mynet1
[
{
"Name": "mynet1",
"Id": "x81fu4ohqot2ufbpoa2u8vyx3",
"Created": "0001-01-01T00:00:00Z",
"Scope": "swarm",
"Driver": "overlay",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": []
},
"Internal": false,
"Attachable": false,
"Containers": null,
"Options": {
"com.docker.network.driver.overlay.vxlanid_list": "4097"
},
"Labels": null
}
]
注意,overlay模式的网络只能在swarm的manager节点上创建,如果在work节点上创建overlay网络会报错:
172.18.0.1:root@sz-pg-oam-docker-test-002:/root]# docker network create -d overlay mynet1
Error response from daemon: Cannot create a multi-host network from a worker node. Please create the network from a manager node.
如果不使用-d指定driver将默认创建本地bridge网络。
自定义网络
创建自定义网络需要设置网络的driver和ipam。