网关、子网地址 Docker | 自定义网络

了解 docker network通过下面的命令来获取帮助

  1. docker network --help
Commands:connectConnect a container to a networkcreateCreate a networkdisconnectDisconnect a container from a networkinspectDisplay detailed information on one or more networkslsList networkspruneRemove all unused networksrmRemove one or more networks
  1. docker network create --help
Options:--attachableEnable manual container attachment--aux-address mapAuxiliary IPv4 or IPv6 addresses used by Network driver (default map[])--config-from stringThe network from which to copy the configuration--config-onlyCreate a configuration only network-d, --driver stringDriver to manage the Network (default "bridge")--gateway stringsIPv4 or IPv6 Gateway for the master subnet# 网关--ingressCreate swarm routing-mesh network--internalRestrict external access to the network--ip-range stringsAllocate container ip from a sub-range--ipam-driver stringIP Address Management Driver (default "default")--ipam-opt mapSet IPAM driver specific options (default map[])--ipv6Enable IPv6 networking--label listSet metadata on a network-o, --opt mapSet driver specific options (default map[])--scope stringControl the network's scope--subnet stringsSubnet in CIDR format that represents a network segment#子网创建网络
  • --driver bridge网络类型,默认bridge网桥
  • --subnet 192.168.0.0/16子网
  • --gateway 192.168.0.1网关
创建 mynet 网络docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 mynet0096a971fd2c66400e53cbae5e53eceedc2f90d5685917e9534640a3535c0ef1查看网卡[root@--- ~]# ip addr1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft forever2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000link/ether 00:16:3e:0a:00:34 brd ff:ff:ff:ff:ff:ffinet 172.31.81.32/20 brd 172.31.95.255 scope global dynamic eth0valid_lft 291870572sec preferred_lft 291870572sec3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group defaultlink/ether 02:42:a4:2f:c5:62 brd ff:ff:ff:ff:ff:ffinet 172.17.0.1/16 brd 172.17.255.255 scope global docker0valid_lft forever preferred_lft forever140: br-799426d70aa2: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group defaultlink/ether 02:42:7f:01:1d:00 brd ff:ff:ff:ff:ff:ffinet 172.18.0.1/16 brd 172.18.255.255 scope global br-799426d70aa2# test-networkvalid_lft forever preferred_lft forever176: br-0096a971fd2c: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group defaultlink/ether 02:42:d9:14:c1:d3 brd ff:ff:ff:ff:ff:ffinet 192.168.0.1/16 brd 192.168.255.255 scope global br-0096a971fd2c# mynetvalid_lft forever preferred_lft forever查看网络[root@--- ~]# docker network lsNETWORK IDNAMEDRIVERSCOPEfeafa30d4051bridgebridgelocale8bf4fced9e2hosthostlocal0096a971fd2cmynetbridgelocal# 自定义bridge网桥6263db0933b9nonenulllocal799426d70aa2test-networkbridgelocal查看 mynet 网络详细信息[root@--- ~]# docker network inspect 0096a971fd2c[{"Name": "mynet","Id": "0096a971fd2c66400e53cbae5e53eceedc2f90d5685917e9534640a3535c0ef1","Created": "2021-10-04T10:18:09.113847736+08:00","Scope": "local","Driver": "bridge",# mynet 网络类型"EnableIPv6": false,"IPAM": {"Driver": "default","Options": {},"Config": [{"Subnet": "192.168.0.0/16",# mynet 子网"Gateway": "192.168.0.1"# mynet 网关}]},"Internal": false,"Attachable": false,"Ingress": false,"ConfigFrom": {"Network": ""},"ConfigOnly": false,"Containers": {},"Options": {},"Labels": {}}]启动容器,并加入mynet网络启动 mynet-centos01 , mynet-tomcat01 并加入mynet网络[root@--- ~]# docker run -it -P --name mynet-tomcat01 --net mynet tomcat[root@--- ~]# docker run -it -P --name mynet-centos01 --net mynet centos

经验总结扩展阅读