docker network —help
Commands:
connect Connect a container to a network
create Create a network
disconnect Disconnect a container from a network
inspect Display detailed information on one or more networks
ls List networks
prune Remove all unused networks
rm Remove one or more networks
docker network create —help
Options:
--attachable Enable manual container attachment
--aux-address map Auxiliary IPv4 or IPv6 addresses used by Network driver (default map[])
--config-from string The network from which to copy the configuration
--config-only Create a configuration only network
-d, --driver string Driver to manage the Network (default "bridge")
--gateway strings IPv4 or IPv6 Gateway for the master subnet # 网关
--ingress Create swarm routing-mesh network
--internal Restrict external access to the network
--ip-range strings 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
-o, --opt map Set driver specific options (default map[])
--scope string Control the network's scope
--subnet strings Subnet in CIDR format that represents a network segment #子网
创建网络
# --driver bridge
# --subnet 192.168.0.0/16
# --gateway 192.168.0.1
docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 mynet
0096a971fd2c66400e53cbae5e53eceedc2f90d5685917e9534640a3535c0ef1
# 查看网卡
[root@--- ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
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
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:16:3e:0a:00:34 brd ff:ff:ff:ff:ff:ff
inet 172.31.81.32/20 brd 172.31.95.255 scope global dynamic eth0
valid_lft 291870572sec preferred_lft 291870572sec
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
link/ether 02:42:a4:2f:c5:62 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
valid_lft forever preferred_lft forever
140: br-799426d70aa2: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
link/ether 02:42:7f:01:1d:00 brd ff:ff:ff:ff:ff:ff
inet 172.18.0.1/16 brd 172.18.255.255 scope global br-799426d70aa2 # test-network
valid_lft forever preferred_lft forever
176: br-0096a971fd2c: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
link/ether 02:42:d9:14:c1:d3 brd ff:ff:ff:ff:ff:ff
inet 192.168.0.1/16 brd 192.168.255.255 scope global br-0096a971fd2c # mynet
valid_lft forever preferred_lft forever
[root@--- ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
feafa30d4051 bridge bridge local
e8bf4fced9e2 host host local
0096a971fd2c mynet bridge local # 自定义bridge网桥
6263db0933b9 none null local
799426d70aa2 test-network bridge local
[root@--- ~]# docker network inspect 0096a971fd2c
[
{
"Name": "mynet",
"Id": "0096a971fd2c66400e53cbae5e53eceedc2f90d5685917e9534640a3535c0ef1",
"Created": "2021-10-04T10:18:09.113847736+08:00",
"Scope": "local",
"Driver": "bridge",
"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网络
# 1、 启动 mynet-centos01, mynet-tomcat01 并加入mynet网络
docker run -it -P --name mynet-tomcat01 --net mynet tomcat
docker run -it -P --name mynet-centos01 --net mynet centos
# 2、查看mynet网桥信息
docker network inspect 0096
[
{
"Name": "mynet",
"Id": "0096a971fd2c66400e53cbae5e53eceedc2f90d5685917e9534640a3535c0ef1",
"Created": "2021-10-04T10:18:09.113847736+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": {
"2fac920eebd505794c4a27ef4efdf531d32805e8663bded93a473bd28236bc27": {
"Name": "mynet-centos01",
"EndpointID": "3aff0752e641651a446ec490faa000812417855739086b2edb584ac2a95a26dd",
"MacAddress": "02:42:c0:a8:00:02",
"IPv4Address": "192.168.0.2/16",
"IPv6Address": ""
},
"3dceda051252b6970fe2156bac7710ba6489a97aac68c49b1c85a2f130d0556f": {
"Name": "mynet-tomcat01",
"EndpointID": "9f52b19493d35c48fe9b33a319de5dbe54c50de1a5d049103cb4e39bfe645f1b",
"MacAddress": "02:42:c0:a8:00:03",
"IPv4Address": "192.168.0.3/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
# 3、进入mynet-centos01 测试连接 mynet-tomcat01
# 发现通过容器ip和容器名都可以连接
[root@--- ~]# docker exec -it 2fac920eebd5 /bin/sh
sh-4.4# ping
sh-4.4# ping 192.168.0.3 # 通过容器ip连接
PING 192.168.0.3 (192.168.0.3) 56(84) bytes of data.
64 bytes from 192.168.0.3: icmp_seq=1 ttl=64 time=0.140 ms
64 bytes from 192.168.0.3: icmp_seq=2 ttl=64 time=0.110 ms
^C
--- 192.168.0.3 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3000ms
rtt min/avg/max/mdev = 0.110/0.121/0.140/0.016 ms
sh-4.4# ping mynet-tomcat01 # 通过容器名连接
PING mynet-tomcat01 (192.168.0.3) 56(84) bytes of data.
64 bytes from mynet-tomcat01.mynet (192.168.0.3): icmp_seq=1 ttl=64 time=0.078 ms
64 bytes from mynet-tomcat01.mynet (192.168.0.3): icmp_seq=2 ttl=64 time=0.131 ms
^C
--- mynet-tomcat01 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2001ms
rtt min/avg/max/mdev = 0.078/0.110/0.131/0.026 ms
sh-4.4#
思考问题: 其它网络下的容器可以直接访问mynet网络下的容器吗? 答案:不可以 解决方法: 让别的网络下的容器加入到mynet网络下
实现容器互联
其它网络下的容器加入mynet网络,实现一个容器两个ip地址
网络集群
# 1、加入网络
docker network connect mynet centos01
# 2、查看mynet网络
[root@--- ~]# docker network inspect 0096
[
{
"Name": "mynet",
"Id": "0096a971fd2c66400e53cbae5e53eceedc2f90d5685917e9534640a3535c0ef1",
"Created": "2021-10-04T10:18:09.113847736+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": {
"2fac920eebd505794c4a27ef4efdf531d32805e8663bded93a473bd28236bc27": {
"Name": "mynet-centos01",
"EndpointID": "3aff0752e641651a446ec490faa000812417855739086b2edb584ac2a95a26dd",
"MacAddress": "02:42:c0:a8:00:02",
"IPv4Address": "192.168.0.2/16",
"IPv6Address": ""
},
"3dceda051252b6970fe2156bac7710ba6489a97aac68c49b1c85a2f130d0556f": {
"Name": "mynet-tomcat01",
"EndpointID": "9f52b19493d35c48fe9b33a319de5dbe54c50de1a5d049103cb4e39bfe645f1b",
"MacAddress": "02:42:c0:a8:00:03",
"IPv4Address": "192.168.0.3/16",
"IPv6Address": ""
},
"cb1922b95b9316d129b54f3545fad9729092926e10a1d5517f8928db42706151": {
"Name": "centos01",
"EndpointID": "0ba210710980230079b17d9bfa360bcee10a310ac7ee88c4e0174c4c6cc425cf",
"MacAddress": "02:42:c0:a8:00:04",
"IPv4Address": "192.168.0.4/16", # 新加入的centos01容器
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
[root@--- ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3dceda051252 tomcat "catalina.sh run" 18 minutes ago Up 18 minutes 0.0.0.0:49164->8080/tcp mynet-tomcat01
2fac920eebd5 centos "/bin/bash" 19 minutes ago Up 19 minutes mynet-centos01
cb1922b95b93 centos "/bin/bash" 13 hours ago Up 2 hours centos01
29298987c51b mysql:5.7 "docker-entrypoint.s…" 13 hours ago Up 13 hours 3306/tcp, 33060/tcp mysql01
# 3、查看centos01的容器信息
# 可以看到Networks下面存在mynet、test-network两个网络连接信息
# 一个容器两个ip地址
[root@--- ~]# docker inspect cb19
[
{
"Id": "cb1922b95b9316d129b54f3545fad9729092926e10a1d5517f8928db42706151",
"Created": "2021-10-03T13:39:00.942877668Z",
"Path": "/bin/bash",
"Args": [],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 759,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-10-04T00:48:39.302485163Z",
"FinishedAt": "2021-10-04T00:48:11.86395321Z"
},
"Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
"ResolvConfPath": "/var/lib/docker/containers/cb1922b95b9316d129b54f3545fad9729092926e10a1d5517f8928db42706151/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/cb1922b95b9316d129b54f3545fad9729092926e10a1d5517f8928db42706151/hostname",
"HostsPath": "/var/lib/docker/containers/cb1922b95b9316d129b54f3545fad9729092926e10a1d5517f8928db42706151/hosts",
"LogPath": "/var/lib/docker/containers/cb1922b95b9316d129b54f3545fad9729092926e10a1d5517f8928db42706151/cb1922b95b9316d129b54f3545fad9729092926e10a1d5517f8928db42706151-json.log",
"Name": "/centos01",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": [
"d39fc0fc13115437b06798b6fc532ed583b3e892dbf7d774bd0fdb624df1f451",
"d662b224c2328f3af7b6c50c1674180032e6a1aa52560f3b486b037389f0e222",
"55cf9726e50882f5c2312ccaa3d474f4efd451920ef3305bf3204a8dcb4d7d28",
"81f3a14ac90512b85e5e74778242b4c688976ee08283ca390726e230fea99e42",
"ac96a35a3d5275b00ae18868374518fa47d7b0d95b753686510e3a7881198284"
],
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"CgroupnsMode": "host",
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "private",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "runc",
"ConsoleSize": [
0,
0
],
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": [],
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DeviceRequests": null,
"KernelMemory": 0,
"KernelMemoryTCP": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": null,
"OomKillDisable": false,
"PidsLimit": null,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0,
"MaskedPaths": [
"/proc/asound",
"/proc/acpi",
"/proc/kcore",
"/proc/keys",
"/proc/latency_stats",
"/proc/timer_list",
"/proc/timer_stats",
"/proc/sched_debug",
"/proc/scsi",
"/sys/firmware"
],
"ReadonlyPaths": [
"/proc/bus",
"/proc/fs",
"/proc/irq",
"/proc/sys",
"/proc/sysrq-trigger"
]
},
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/5bf033bfa1f6c088bd948a2d49f973f6062fc98d4acc5ee33ca4881137b55460-init/diff:/var/lib/docker/overlay2/d6e4be0166eac21b08ff7e34bb088bd7488086c5fd04603879424af35f8e6190/diff",
"MergedDir": "/var/lib/docker/overlay2/5bf033bfa1f6c088bd948a2d49f973f6062fc98d4acc5ee33ca4881137b55460/merged",
"UpperDir": "/var/lib/docker/overlay2/5bf033bfa1f6c088bd948a2d49f973f6062fc98d4acc5ee33ca4881137b55460/diff",
"WorkDir": "/var/lib/docker/overlay2/5bf033bfa1f6c088bd948a2d49f973f6062fc98d4acc5ee33ca4881137b55460/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "cb1922b95b93",
"Domainname": "",
"User": "",
"AttachStdin": true,
"AttachStdout": true,
"AttachStderr": true,
"Tty": true,
"OpenStdin": true,
"StdinOnce": true,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/bash"
],
"Image": "centos",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20210915",
"org.label-schema.license": "GPLv2",
"org.label-schema.name": "CentOS Base Image",
"org.label-schema.schema-version": "1.0",
"org.label-schema.vendor": "CentOS"
}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "ac455100992ba8412440bbb5e166e42c5b927876129526e4a7d9365751821bda",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/ac455100992b",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "",
"Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"MacAddress": "",
"Networks": {
"mynet": {
"IPAMConfig": {},
"Links": null,
"Aliases": [
"cb1922b95b93"
],
"NetworkID": "0096a971fd2c66400e53cbae5e53eceedc2f90d5685917e9534640a3535c0ef1",
"EndpointID": "0ba210710980230079b17d9bfa360bcee10a310ac7ee88c4e0174c4c6cc425cf",
"Gateway": "192.168.0.1",
"IPAddress": "192.168.0.4",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:c0:a8:00:04",
"DriverOpts": {}
},
"test-network": {
"IPAMConfig": {},
"Links": null,
"Aliases": [
"cb1922b95b93"
],
"NetworkID": "799426d70aa28b73b4a777c85b338186eafadd1558b13c43e07a9fd9a8b545e7",
"EndpointID": "c5cac97189a8f9994ccd73ae9df1e0d28aa16dfde923dc241e2f3a3a4fda01b5",
"Gateway": "172.18.0.1",
"IPAddress": "172.18.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:12:00:03",
"DriverOpts": {}
}
}
}
}
]
test-network网卡下的centos01访问mynet网卡下的mynet-centos01、mynet-tomcat01
容器ip和容器名都可以连接
[root@--- ~]# docker exec -it cb19 /bin/bash
[root@cb1922b95b93 /]# ping 192.168.0.3
PING 192.168.0.3 (192.168.0.3) 56(84) bytes of data.
64 bytes from 192.168.0.3: icmp_seq=1 ttl=64 time=0.186 ms
64 bytes from 192.168.0.3: icmp_seq=2 ttl=64 time=0.123 ms
^C
--- 192.168.0.3 ping statistics ---
7 packets transmitted, 7 received, 0% packet loss, time 6000ms
rtt min/avg/max/mdev = 0.090/0.127/0.186/0.029 ms
[root@cb1922b95b93 /]# ping mynet-centos01
PING mynet-centos01 (192.168.0.2) 56(84) bytes of data.
64 bytes from mynet-centos01.mynet (192.168.0.2): icmp_seq=1 ttl=64 time=0.105 ms
64 bytes from mynet-centos01.mynet (192.168.0.2): icmp_seq=2 ttl=64 time=0.130 ms
^C
--- mynet-centos01 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2001ms
rtt min/avg/max/mdev = 0.105/0.116/0.130/0.013 ms
[root@cb1922b95b93 /]# ping mynet-tomcat01
PING mynet-tomcat01 (192.168.0.3) 56(84) bytes of data.
64 bytes from mynet-tomcat01.mynet (192.168.0.3): icmp_seq=1 ttl=64 time=0.085 ms
64 bytes from mynet-tomcat01.mynet (192.168.0.3): icmp_seq=2 ttl=64 time=0.102 ms
^C
--- mynet-tomcat01 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 0.085/0.093/0.102/0.012 ms
[root@cb1922b95b93 /]#