查看docker所有网络
[root@localhost ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
c6cccbeca026 bridge bridge local
ef2aaeb91b95 host host local
9b941aa3bfdf none null local
DRIVER
bridge :桥接模式(docker默认)
host:和宿主机共享网络
none:不配置网络
使用命令docker network create来创建网络。创建的网络中默认使用的是桥接模式,–subnet输入子网段,–gateway输入子网的网关地址。创建成功后,可以使用docker network ls查看到网络。
[root@localhost ~]# docker network create --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
[root@localhost ~]# docker network create --subnet 192.168.3.0/24 --gateway 192.168.3.1 mynet
3013d0f266f4e243fd7af37c8c8e3d60b6a75c1744a5aa8b8c6cb05a5d25220c
[root@localhost ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
c6cccbeca026 bridge bridge local
ef2aaeb91b95 host host local
3013d0f266f4 mynet bridge local
9b941aa3bfdf none null local
使用docker network inspect 网络查看详细信息。此处可以看到该网络的网段和网关。目前刚创建,并没有容器被使用。
[root@localhost ~]# docker network inspect mynet
[
{
"Name": "mynet",
"Id": "3013d0f266f4e243fd7af37c8c8e3d60b6a75c1744a5aa8b8c6cb05a5d25220c",
"Created": "2021-05-19T05:20:32.230092567-04:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {
},
"Config": [
{
"Subnet": "192.168.3.0/24",
"Gateway": "192.168.3.1"
}
]
},
"Internal": false,
"Attachable": false,
"Containers": {
},
"Options": {
},
"Labels": {
}
}
]
运行容器,使用自定义网络mynet。
[root@localhost ~]# docker run -dit --name centosnet1 --network mynet centos
2688d2a9c1487769e223d6f0d8c834197044ff2607e2896aa12d53a0dc0e8279
[root@localhost ~]# docker run -dit --name centosnet2 --network mynet centos
bdf99b0c14809435732551fa375ad77809f62fcdf5b01659ed741451cd41e223
[root@localhost ~]# docker network inspect mynet
[
{
"Name": "mynet",
"Id": "3013d0f266f4e243fd7af37c8c8e3d60b6a75c1744a5aa8b8c6cb05a5d25220c",
"Created":