Docker13_5:Docker网络联通docker network connect
docker network connect
docker network connect [OPTIONS] NETWORK CONTAINER
命令参数
参数 | 含义 |
---|---|
–alias | 为容器添加网络范围的别名 |
–driver-opt | 网络驱动程序选项 |
–ip | IPv4 address (e.g., 172.30.100.104) |
–ip6 | IPv6 address (e.g., 2001:db8::33) |
–link | 添加到另一个容器的链接 |
–link-local-ip | 为容器添加一个链接本地地址 |
测试
docker ps查看运行中的容器,我这边tomcat-mynet-02、tomcat-mynet-01是用的mynet网络,tomcat902、tomcat901是默认的docker0。
[root@VM-0-3-centos pdx_haokai]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0944bb6991f8 mytomcat9.0.39:0.1 "/bin/sh -c '$CATALI…" 2 days ago Up 2 days 0.0.0.0:49157->8080/tcp, :::49157->8080/tcp tomcat-mynet-02
54198e4aa659 mytomcat9.0.39:0.1 "/bin/sh -c '$CATALI…" 2 days ago Up 2 days 0.0.0.0:49156->8080/tcp, :::49156->8080/tcp tomcat-mynet-01
f0a146f6457d mytomcat9.0.39:0.1 "/bin/sh -c '$CATALI…" 3 days ago Up 3 days 0.0.0.0:49155->8080/tcp, :::49155->8080/tcp tomcat902
cd561b9bde5c mytomcat9.0.39:0.1 "/bin/sh -c '$CATALI…" 7 days ago Up 7 days 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp tomcat901
docker inspect 容器id查看容器网络信息
[root@VM-0-3-centos pdx_haokai]# docker inspect f0a146f6457d
[
{
"Id": "f0a146f6457d19fcfbba3b1dd1b5a00a60485effcdf551b7debe7a50b4bb5fc9",
"Created": "2022-01-19T02:52:17.037632531Z",
"Path": "/bin/sh",
"Args": [
"-c",
"$CATALINA_HOME/bin/startup.sh && tail -F $CATALINA_HOME/bin/logs/catalina.out"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 6135,
"ExitCode": 0,
"Error": "",
"StartedAt": "2022-01-19T02:52:17.497078461Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:40f4d929ef013d685275bc16e9e370e8c05ac22435493b8f2eeed64f1f01ddf4",
"ResolvConfPath": "/var/lib/docker/containers/f0a146f6457d19fcfbba3b1dd1b5a00a60485effcdf551b7debe7a50b4bb5fc9/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/f0a146f6457d19fcfbba3b1dd1b5a00a60485effcdf551b7debe7a50b4bb5fc9/hostname",
"HostsPath": "/var/lib/docker/containers/f0a146f6457d19fcfbba3b1dd1b5a00a60485effcdf551b7debe7a50b4bb5fc9/hosts",
"LogPath": "/var/lib/docker/containers/f0a146f6457d19fcfbba3b1dd1b5a00a60485effcdf551b7debe7a50b4bb5fc9/f0a146f6457d19fcfbba3b1dd1b5a00a60485effcdf551b7debe7a50b4bb5fc9-json.log",
"Name": "/tomcat902",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"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": [
"/tomcat901:/tomcat902/tomcat901"
],
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": true,
"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/549af69cacdb64cd31f7a9bdb794a7afe67c4dd8f9b020abc64400d42fc66c66-init/diff:/var/lib/docker/overlay2/d38fe41669a3d06ae2a3791718d680508858d99f6f4101e8be42f0a01497a49b/diff:/var/lib/docker/overlay2/970424a120d1191d72c8a4938187363c30cb14a178b45dde46fee73312dbe916/diff:/var/lib/docker/overlay2/b0bdb16e84f82e2a8c9f1375700d35aa3a0ceb6c46336162a144eca00aebddf8/diff:/var/lib/docker/overlay2/cfe31fec684fcf294e149d80379fe2ee78c1a1b90098d4a9e8cd518d649ae246/diff:/var/lib/docker/overlay2/a666bdae4036f5b6b58a0240619270696eb1a7e391452167ebfb723f7104359e/diff:/var/lib/docker/overlay2/d8ffcd572cd0db6ef7277c380db4cd7ffd38d3277c50c31744425363e4b8a171/diff:/var/lib/docker/overlay2/94f7a2828f0b49b7286798ace3983a05fa3ca6e1ac276f0cfbe93474b2691c74/diff",
"MergedDir": "/var/lib/docker/overlay2/549af69cacdb64cd31f7a9bdb794a7afe67c4dd8f9b020abc64400d42fc66c66/merged",
"UpperDir": "/var/lib/docker/overlay2/549af69cacdb64cd31f7a9bdb794a7afe67c4dd8f9b020abc64400d42fc66c66/diff",
"WorkDir": "/var/lib/docker/overlay2/549af69cacdb64cd31f7a9bdb794a7afe67c4dd8f9b020abc64400d42fc66c66/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "f0a146f6457d",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"8080/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/haokai/jdk1.8.0_261/bin:/usr/local/haokai/apache-tomcat-9.0.39/lib:/usr/local/haokai/apache-tomcat-9.0.39/bin",
"MYPATH=/usr/local/haokai",
"JAVA_HOME=/usr/local/haokai/jdk1.8.0_261",
"CLASSPATH=/usr/local/haokai/jdk1.8.0_261/lib/dt.jar:/usr/local/haokai/jdk1.8.0_261/lib/tools.jar",
"CATALINA_HOME=/usr/local/haokai/apache-tomcat-9.0.39",
"CATALINA_BASE=/usr/local/haokai/apache-tomcat-9.0.39"
],
"Cmd": [
"/bin/sh",
"-c",
"$CATALINA_HOME/bin/startup.sh && tail -F $CATALINA_HOME/bin/logs/catalina.out"
],
"Image": "mytomcat9.0.39:0.1",
"Volumes": null,
"WorkingDir": "/usr/local/haokai",
"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": "6192e6a03bba7291935edc9aa5eb586a221fc17e60aea0e821d4fa2d62f35dad",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"8080/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "49155"
},
{
"HostIp": "::",
"HostPort": "49155"
}
]
},
"SandboxKey": "/var/run/docker/netns/6192e6a03bba",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "68584b009434e4bf0a115109cdf9649f84d2b34dc53130f9345fbe327469135c",
"Gateway": "172.18.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.18.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:12:00:03",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "ed8612b1d525cda71eda7b4ae7022dae803725e93ddb2af999709819268bad07",
"EndpointID": "68584b009434e4bf0a115109cdf9649f84d2b34dc53130f9345fbe327469135c",
"Gateway": "172.18.0.1",
"IPAddress": "172.18.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:12:00:03",
"DriverOpts": null
}
}
}
}
]
[root@VM-0-3-centos pdx_haokai]#
查看mynet的网络
[root@VM-0-3-centos pdx_haokai]# docker inspect 0944bb6991f8
[
{
"Id": "0944bb6991f8affc4de8305185491055ce5bb7e91ea09658ce0bba12e73435af",
"Created": "2022-01-19T13:40:19.036191048Z",
"Path": "/bin/sh",
"Args": [
"-c",
"$CATALINA_HOME/bin/startup.sh && tail -F $CATALINA_HOME/bin/logs/catalina.out"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 30065,
"ExitCode": 0,
"Error": "",
"StartedAt": "2022-01-19T13:40:19.531955061Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:40f4d929ef013d685275bc16e9e370e8c05ac22435493b8f2eeed64f1f01ddf4",
"ResolvConfPath": "/var/lib/docker/containers/0944bb6991f8affc4de8305185491055ce5bb7e91ea09658ce0bba12e73435af/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/0944bb6991f8affc4de8305185491055ce5bb7e91ea09658ce0bba12e73435af/hostname",
"HostsPath": "/var/lib/docker/containers/0944bb6991f8affc4de8305185491055ce5bb7e91ea09658ce0bba12e73435af/hosts",
"LogPath": "/var/lib/docker/containers/0944bb6991f8affc4de8305185491055ce5bb7e91ea09658ce0bba12e73435af/0944bb6991f8affc4de8305185491055ce5bb7e91ea09658ce0bba12e73435af-json.log",
"Name": "/tomcat-mynet-02",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "mynet",
"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": true,
"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/a28017383469c9fcc2602606245f77158051e550b243badccb39f9c6bbaffdf5-init/diff:/var/lib/docker/overlay2/d38fe41669a3d06ae2a3791718d680508858d99f6f4101e8be42f0a01497a49b/diff:/var/lib/docker/overlay2/970424a120d1191d72c8a4938187363c30cb14a178b45dde46fee73312dbe916/diff:/var/lib/docker/overlay2/b0bdb16e84f82e2a8c9f1375700d35aa3a0ceb6c46336162a144eca00aebddf8/diff:/var/lib/docker/overlay2/cfe31fec684fcf294e149d80379fe2ee78c1a1b90098d4a9e8cd518d649ae246/diff:/var/lib/docker/overlay2/a666bdae4036f5b6b58a0240619270696eb1a7e391452167ebfb723f7104359e/diff:/var/lib/docker/overlay2/d8ffcd572cd0db6ef7277c380db4cd7ffd38d3277c50c31744425363e4b8a171/diff:/var/lib/docker/overlay2/94f7a2828f0b49b7286798ace3983a05fa3ca6e1ac276f0cfbe93474b2691c74/diff",
"MergedDir": "/var/lib/docker/overlay2/a28017383469c9fcc2602606245f77158051e550b243badccb39f9c6bbaffdf5/merged",
"UpperDir": "/var/lib/docker/overlay2/a28017383469c9fcc2602606245f77158051e550b243badccb39f9c6bbaffdf5/diff",
"WorkDir": "/var/lib/docker/overlay2/a28017383469c9fcc2602606245f77158051e550b243badccb39f9c6bbaffdf5/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "0944bb6991f8",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"8080/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/haokai/jdk1.8.0_261/bin:/usr/local/haokai/apache-tomcat-9.0.39/lib:/usr/local/haokai/apache-tomcat-9.0.39/bin",
"MYPATH=/usr/local/haokai",
"JAVA_HOME=/usr/local/haokai/jdk1.8.0_261",
"CLASSPATH=/usr/local/haokai/jdk1.8.0_261/lib/dt.jar:/usr/local/haokai/jdk1.8.0_261/lib/tools.jar",
"CATALINA_HOME=/usr/local/haokai/apache-tomcat-9.0.39",
"CATALINA_BASE=/usr/local/haokai/apache-tomcat-9.0.39"
],
"Cmd": [
"/bin/sh",
"-c",
"$CATALINA_HOME/bin/startup.sh && tail -F $CATALINA_HOME/bin/logs/catalina.out"
],
"Image": "mytomcat9.0.39:0.1",
"Volumes": null,
"WorkingDir": "/usr/local/haokai",
"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": "7b72cda1644ab0e76a2d4e6025258358a390b008a63b0f1dfbb2c4b8eeb2567d",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"8080/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "49157"
},
{
"HostIp": "::",
"HostPort": "49157"
}
]
},
"SandboxKey": "/var/run/docker/netns/7b72cda1644a",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "",
"Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"MacAddress": "",
"Networks": {
"mynet": {
"IPAMConfig": null,
"Links": null,
"Aliases": [
"0944bb6991f8"
],
"NetworkID": "a8a455ea111c8b3458d0ae24e76dc47b8eb64c65ef42ccb7ef4412559ce78e69",
"EndpointID": "f01053fd50e6e4497a74f943ce7190a978ada2269c93c89106ecd6db3828cf99",
"Gateway": "192.168.0.1",
"IPAddress": "192.168.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:c0:a8:00:03",
"DriverOpts": null
}
}
}
}
]
[root@VM-0-3-centos pdx_haokai]#
执行ping测试
[root@VM-0-3-centos pdx_haokai]# docker exec -it tomcat-mynet-02 ping tomcat902
ping: tomcat902: Name or service not known
[root@VM-0-3-centos pdx_haokai]#
添加connect连接后,再执行ping
[root@VM-0-3-centos pdx_haokai]# docker network connect mynet tomcat901
[root@VM-0-3-centos pdx_haokai]# docker exec -it tomcat-mynet-02 ping tomcat901
PING tomcat901 (192.168.0.4) 56(84) bytes of data.
64 bytes from tomcat901.mynet (192.168.0.4): icmp_seq=1 ttl=64 time=0.071 ms
64 bytes from tomcat901.mynet (192.168.0.4): icmp_seq=2 ttl=64 time=0.064 ms
64 bytes from tomcat901.mynet (192.168.0.4): icmp_seq=3 ttl=64 time=0.048 ms
^C
--- tomcat901 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.048/0.061/0.071/0.009 ms
[root@VM-0-3-centos pdx_haokai]#
查看mynet网络变化,发现是把这个tomcat902容器添加到了mynet网络里,这个官方也称作,一个容器,两个IP地址
[root@VM-0-3-centos pdx_haokai]# docker network ls
NETWORK ID NAME DRIVER SCOPE
ed8612b1d525 bridge bridge local
efbd70fbac53 host host local
a8a455ea111c mynet bridge local
8b375c8baa85 none null local
[root@VM-0-3-centos pdx_haokai]# docker network inspect a8a455ea111c
[
{
"Name": "mynet",
"Id": "a8a455ea111c8b3458d0ae24e76dc47b8eb64c65ef42ccb7ef4412559ce78e69",
"Created": "2022-01-19T21:30:34.564255758+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": {
"0944bb6991f8affc4de8305185491055ce5bb7e91ea09658ce0bba12e73435af": {
"Name": "tomcat-mynet-02",
"EndpointID": "f01053fd50e6e4497a74f943ce7190a978ada2269c93c89106ecd6db3828cf99",
"MacAddress": "02:42:c0:a8:00:03",
"IPv4Address": "192.168.0.3/16",
"IPv6Address": ""
},
"54198e4aa6594c27aff1ab617e79278be56036567cf413664d39491114e8c262": {
"Name": "tomcat-mynet-01",
"EndpointID": "6579b71c42ec82639f1a00583beee78adceb39ab56274a0c5a33d07c47a5a6f5",
"MacAddress": "02:42:c0:a8:00:02",
"IPv4Address": "192.168.0.2/16",
"IPv6Address": ""
},
"cd561b9bde5cf2bafc271a4035b2f145c8784679189faffcef6e31b85c18691a": {
"Name": "tomcat901",
"EndpointID": "ac6b8b3863d29324cd1320dab7d428c9976d17a1286a3db585d691579bab4fdd",
"MacAddress": "02:42:c0:a8:00:04",
"IPv4Address": "192.168.0.4/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
[root@VM-0-3-centos pdx_haokai]#
结论
结论:如果需要跨网络操作别的容器,就使用docker network connect来打通网络。比如docker 搭建redis集群。