docker network —help

  1. Commands:
  2. connect Connect a container to a network
  3. create Create a network
  4. disconnect Disconnect a container from a network
  5. inspect Display detailed information on one or more networks
  6. ls List networks
  7. prune Remove all unused networks
  8. rm Remove one or more networks

docker network create —help

  1. Options:
  2. --attachable Enable manual container attachment
  3. --aux-address map Auxiliary IPv4 or IPv6 addresses used by Network driver (default map[])
  4. --config-from string The network from which to copy the configuration
  5. --config-only Create a configuration only network
  6. -d, --driver string Driver to manage the Network (default "bridge")
  7. --gateway strings IPv4 or IPv6 Gateway for the master subnet # 网关
  8. --ingress Create swarm routing-mesh network
  9. --internal Restrict external access to the network
  10. --ip-range strings Allocate container ip from a sub-range
  11. --ipam-driver string IP Address Management Driver (default "default")
  12. --ipam-opt map Set IPAM driver specific options (default map[])
  13. --ipv6 Enable IPv6 networking
  14. --label list Set metadata on a network
  15. -o, --opt map Set driver specific options (default map[])
  16. --scope string Control the network's scope
  17. --subnet strings Subnet in CIDR format that represents a network segment #子网

创建网络

  1. # --driver bridge
  2. # --subnet 192.168.0.0/16
  3. # --gateway 192.168.0.1
  4. docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 mynet
  5. 0096a971fd2c66400e53cbae5e53eceedc2f90d5685917e9534640a3535c0ef1
  6. # 查看网卡
  7. [root@--- ~]# ip addr
  8. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
  9. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  10. inet 127.0.0.1/8 scope host lo
  11. valid_lft forever preferred_lft forever
  12. 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
  13. link/ether 00:16:3e:0a:00:34 brd ff:ff:ff:ff:ff:ff
  14. inet 172.31.81.32/20 brd 172.31.95.255 scope global dynamic eth0
  15. valid_lft 291870572sec preferred_lft 291870572sec
  16. 3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
  17. link/ether 02:42:a4:2f:c5:62 brd ff:ff:ff:ff:ff:ff
  18. inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
  19. valid_lft forever preferred_lft forever
  20. 140: br-799426d70aa2: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
  21. link/ether 02:42:7f:01:1d:00 brd ff:ff:ff:ff:ff:ff
  22. inet 172.18.0.1/16 brd 172.18.255.255 scope global br-799426d70aa2 # test-network
  23. valid_lft forever preferred_lft forever
  24. 176: br-0096a971fd2c: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
  25. link/ether 02:42:d9:14:c1:d3 brd ff:ff:ff:ff:ff:ff
  26. inet 192.168.0.1/16 brd 192.168.255.255 scope global br-0096a971fd2c # mynet
  27. valid_lft forever preferred_lft forever
  28. [root@--- ~]# docker network ls
  29. NETWORK ID NAME DRIVER SCOPE
  30. feafa30d4051 bridge bridge local
  31. e8bf4fced9e2 host host local
  32. 0096a971fd2c mynet bridge local # 自定义bridge网桥
  33. 6263db0933b9 none null local
  34. 799426d70aa2 test-network bridge local
  35. [root@--- ~]# docker network inspect 0096a971fd2c
  36. [
  37. {
  38. "Name": "mynet",
  39. "Id": "0096a971fd2c66400e53cbae5e53eceedc2f90d5685917e9534640a3535c0ef1",
  40. "Created": "2021-10-04T10:18:09.113847736+08:00",
  41. "Scope": "local",
  42. "Driver": "bridge",
  43. "EnableIPv6": false,
  44. "IPAM": {
  45. "Driver": "default",
  46. "Options": {},
  47. "Config": [
  48. {
  49. "Subnet": "192.168.0.0/16", # mynet 子网
  50. "Gateway": "192.168.0.1" # mynet 网关
  51. }
  52. ]
  53. },
  54. "Internal": false,
  55. "Attachable": false,
  56. "Ingress": false,
  57. "ConfigFrom": {
  58. "Network": ""
  59. },
  60. "ConfigOnly": false,
  61. "Containers": {},
  62. "Options": {},
  63. "Labels": {}
  64. }
  65. ]

启动容器,并加入mynet网络

  1. # 1、 启动 mynet-centos01, mynet-tomcat01 并加入mynet网络
  2. docker run -it -P --name mynet-tomcat01 --net mynet tomcat
  3. docker run -it -P --name mynet-centos01 --net mynet centos
  4. # 2、查看mynet网桥信息
  5. docker network inspect 0096
  6. [
  7. {
  8. "Name": "mynet",
  9. "Id": "0096a971fd2c66400e53cbae5e53eceedc2f90d5685917e9534640a3535c0ef1",
  10. "Created": "2021-10-04T10:18:09.113847736+08:00",
  11. "Scope": "local",
  12. "Driver": "bridge",
  13. "EnableIPv6": false,
  14. "IPAM": {
  15. "Driver": "default",
  16. "Options": {},
  17. "Config": [
  18. {
  19. "Subnet": "192.168.0.0/16",
  20. "Gateway": "192.168.0.1"
  21. }
  22. ]
  23. },
  24. "Internal": false,
  25. "Attachable": false,
  26. "Ingress": false,
  27. "ConfigFrom": {
  28. "Network": ""
  29. },
  30. "ConfigOnly": false,
  31. "Containers": {
  32. "2fac920eebd505794c4a27ef4efdf531d32805e8663bded93a473bd28236bc27": {
  33. "Name": "mynet-centos01",
  34. "EndpointID": "3aff0752e641651a446ec490faa000812417855739086b2edb584ac2a95a26dd",
  35. "MacAddress": "02:42:c0:a8:00:02",
  36. "IPv4Address": "192.168.0.2/16",
  37. "IPv6Address": ""
  38. },
  39. "3dceda051252b6970fe2156bac7710ba6489a97aac68c49b1c85a2f130d0556f": {
  40. "Name": "mynet-tomcat01",
  41. "EndpointID": "9f52b19493d35c48fe9b33a319de5dbe54c50de1a5d049103cb4e39bfe645f1b",
  42. "MacAddress": "02:42:c0:a8:00:03",
  43. "IPv4Address": "192.168.0.3/16",
  44. "IPv6Address": ""
  45. }
  46. },
  47. "Options": {},
  48. "Labels": {}
  49. }
  50. ]
  51. # 3、进入mynet-centos01 测试连接 mynet-tomcat01
  52. # 发现通过容器ip和容器名都可以连接
  53. [root@--- ~]# docker exec -it 2fac920eebd5 /bin/sh
  54. sh-4.4# ping
  55. sh-4.4# ping 192.168.0.3 # 通过容器ip连接
  56. PING 192.168.0.3 (192.168.0.3) 56(84) bytes of data.
  57. 64 bytes from 192.168.0.3: icmp_seq=1 ttl=64 time=0.140 ms
  58. 64 bytes from 192.168.0.3: icmp_seq=2 ttl=64 time=0.110 ms
  59. ^C
  60. --- 192.168.0.3 ping statistics ---
  61. 4 packets transmitted, 4 received, 0% packet loss, time 3000ms
  62. rtt min/avg/max/mdev = 0.110/0.121/0.140/0.016 ms
  63. sh-4.4# ping mynet-tomcat01 # 通过容器名连接
  64. PING mynet-tomcat01 (192.168.0.3) 56(84) bytes of data.
  65. 64 bytes from mynet-tomcat01.mynet (192.168.0.3): icmp_seq=1 ttl=64 time=0.078 ms
  66. 64 bytes from mynet-tomcat01.mynet (192.168.0.3): icmp_seq=2 ttl=64 time=0.131 ms
  67. ^C
  68. --- mynet-tomcat01 ping statistics ---
  69. 3 packets transmitted, 3 received, 0% packet loss, time 2001ms
  70. rtt min/avg/max/mdev = 0.078/0.110/0.131/0.026 ms
  71. sh-4.4#

思考问题: 其它网络下的容器可以直接访问mynet网络下的容器吗? 答案:不可以 解决方法: 让别的网络下的容器加入到mynet网络下

image.png

实现容器互联

其它网络下的容器加入mynet网络,实现一个容器两个ip地址

网络集群

  1. # 1、加入网络
  2. docker network connect mynet centos01
  3. # 2、查看mynet网络
  4. [root@--- ~]# docker network inspect 0096
  5. [
  6. {
  7. "Name": "mynet",
  8. "Id": "0096a971fd2c66400e53cbae5e53eceedc2f90d5685917e9534640a3535c0ef1",
  9. "Created": "2021-10-04T10:18:09.113847736+08:00",
  10. "Scope": "local",
  11. "Driver": "bridge",
  12. "EnableIPv6": false,
  13. "IPAM": {
  14. "Driver": "default",
  15. "Options": {},
  16. "Config": [
  17. {
  18. "Subnet": "192.168.0.0/16",
  19. "Gateway": "192.168.0.1"
  20. }
  21. ]
  22. },
  23. "Internal": false,
  24. "Attachable": false,
  25. "Ingress": false,
  26. "ConfigFrom": {
  27. "Network": ""
  28. },
  29. "ConfigOnly": false,
  30. "Containers": {
  31. "2fac920eebd505794c4a27ef4efdf531d32805e8663bded93a473bd28236bc27": {
  32. "Name": "mynet-centos01",
  33. "EndpointID": "3aff0752e641651a446ec490faa000812417855739086b2edb584ac2a95a26dd",
  34. "MacAddress": "02:42:c0:a8:00:02",
  35. "IPv4Address": "192.168.0.2/16",
  36. "IPv6Address": ""
  37. },
  38. "3dceda051252b6970fe2156bac7710ba6489a97aac68c49b1c85a2f130d0556f": {
  39. "Name": "mynet-tomcat01",
  40. "EndpointID": "9f52b19493d35c48fe9b33a319de5dbe54c50de1a5d049103cb4e39bfe645f1b",
  41. "MacAddress": "02:42:c0:a8:00:03",
  42. "IPv4Address": "192.168.0.3/16",
  43. "IPv6Address": ""
  44. },
  45. "cb1922b95b9316d129b54f3545fad9729092926e10a1d5517f8928db42706151": {
  46. "Name": "centos01",
  47. "EndpointID": "0ba210710980230079b17d9bfa360bcee10a310ac7ee88c4e0174c4c6cc425cf",
  48. "MacAddress": "02:42:c0:a8:00:04",
  49. "IPv4Address": "192.168.0.4/16", # 新加入的centos01容器
  50. "IPv6Address": ""
  51. }
  52. },
  53. "Options": {},
  54. "Labels": {}
  55. }
  56. ]
  57. [root@--- ~]# docker ps
  58. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  59. 3dceda051252 tomcat "catalina.sh run" 18 minutes ago Up 18 minutes 0.0.0.0:49164->8080/tcp mynet-tomcat01
  60. 2fac920eebd5 centos "/bin/bash" 19 minutes ago Up 19 minutes mynet-centos01
  61. cb1922b95b93 centos "/bin/bash" 13 hours ago Up 2 hours centos01
  62. 29298987c51b mysql:5.7 "docker-entrypoint.s…" 13 hours ago Up 13 hours 3306/tcp, 33060/tcp mysql01
  63. # 3、查看centos01的容器信息
  64. # 可以看到Networks下面存在mynet、test-network两个网络连接信息
  65. # 一个容器两个ip地址
  66. [root@--- ~]# docker inspect cb19
  67. [
  68. {
  69. "Id": "cb1922b95b9316d129b54f3545fad9729092926e10a1d5517f8928db42706151",
  70. "Created": "2021-10-03T13:39:00.942877668Z",
  71. "Path": "/bin/bash",
  72. "Args": [],
  73. "State": {
  74. "Status": "running",
  75. "Running": true,
  76. "Paused": false,
  77. "Restarting": false,
  78. "OOMKilled": false,
  79. "Dead": false,
  80. "Pid": 759,
  81. "ExitCode": 0,
  82. "Error": "",
  83. "StartedAt": "2021-10-04T00:48:39.302485163Z",
  84. "FinishedAt": "2021-10-04T00:48:11.86395321Z"
  85. },
  86. "Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
  87. "ResolvConfPath": "/var/lib/docker/containers/cb1922b95b9316d129b54f3545fad9729092926e10a1d5517f8928db42706151/resolv.conf",
  88. "HostnamePath": "/var/lib/docker/containers/cb1922b95b9316d129b54f3545fad9729092926e10a1d5517f8928db42706151/hostname",
  89. "HostsPath": "/var/lib/docker/containers/cb1922b95b9316d129b54f3545fad9729092926e10a1d5517f8928db42706151/hosts",
  90. "LogPath": "/var/lib/docker/containers/cb1922b95b9316d129b54f3545fad9729092926e10a1d5517f8928db42706151/cb1922b95b9316d129b54f3545fad9729092926e10a1d5517f8928db42706151-json.log",
  91. "Name": "/centos01",
  92. "RestartCount": 0,
  93. "Driver": "overlay2",
  94. "Platform": "linux",
  95. "MountLabel": "",
  96. "ProcessLabel": "",
  97. "AppArmorProfile": "",
  98. "ExecIDs": [
  99. "d39fc0fc13115437b06798b6fc532ed583b3e892dbf7d774bd0fdb624df1f451",
  100. "d662b224c2328f3af7b6c50c1674180032e6a1aa52560f3b486b037389f0e222",
  101. "55cf9726e50882f5c2312ccaa3d474f4efd451920ef3305bf3204a8dcb4d7d28",
  102. "81f3a14ac90512b85e5e74778242b4c688976ee08283ca390726e230fea99e42",
  103. "ac96a35a3d5275b00ae18868374518fa47d7b0d95b753686510e3a7881198284"
  104. ],
  105. "HostConfig": {
  106. "Binds": null,
  107. "ContainerIDFile": "",
  108. "LogConfig": {
  109. "Type": "json-file",
  110. "Config": {}
  111. },
  112. "NetworkMode": "default",
  113. "PortBindings": {},
  114. "RestartPolicy": {
  115. "Name": "no",
  116. "MaximumRetryCount": 0
  117. },
  118. "AutoRemove": false,
  119. "VolumeDriver": "",
  120. "VolumesFrom": null,
  121. "CapAdd": null,
  122. "CapDrop": null,
  123. "CgroupnsMode": "host",
  124. "Dns": [],
  125. "DnsOptions": [],
  126. "DnsSearch": [],
  127. "ExtraHosts": null,
  128. "GroupAdd": null,
  129. "IpcMode": "private",
  130. "Cgroup": "",
  131. "Links": null,
  132. "OomScoreAdj": 0,
  133. "PidMode": "",
  134. "Privileged": false,
  135. "PublishAllPorts": false,
  136. "ReadonlyRootfs": false,
  137. "SecurityOpt": null,
  138. "UTSMode": "",
  139. "UsernsMode": "",
  140. "ShmSize": 67108864,
  141. "Runtime": "runc",
  142. "ConsoleSize": [
  143. 0,
  144. 0
  145. ],
  146. "Isolation": "",
  147. "CpuShares": 0,
  148. "Memory": 0,
  149. "NanoCpus": 0,
  150. "CgroupParent": "",
  151. "BlkioWeight": 0,
  152. "BlkioWeightDevice": [],
  153. "BlkioDeviceReadBps": null,
  154. "BlkioDeviceWriteBps": null,
  155. "BlkioDeviceReadIOps": null,
  156. "BlkioDeviceWriteIOps": null,
  157. "CpuPeriod": 0,
  158. "CpuQuota": 0,
  159. "CpuRealtimePeriod": 0,
  160. "CpuRealtimeRuntime": 0,
  161. "CpusetCpus": "",
  162. "CpusetMems": "",
  163. "Devices": [],
  164. "DeviceCgroupRules": null,
  165. "DeviceRequests": null,
  166. "KernelMemory": 0,
  167. "KernelMemoryTCP": 0,
  168. "MemoryReservation": 0,
  169. "MemorySwap": 0,
  170. "MemorySwappiness": null,
  171. "OomKillDisable": false,
  172. "PidsLimit": null,
  173. "Ulimits": null,
  174. "CpuCount": 0,
  175. "CpuPercent": 0,
  176. "IOMaximumIOps": 0,
  177. "IOMaximumBandwidth": 0,
  178. "MaskedPaths": [
  179. "/proc/asound",
  180. "/proc/acpi",
  181. "/proc/kcore",
  182. "/proc/keys",
  183. "/proc/latency_stats",
  184. "/proc/timer_list",
  185. "/proc/timer_stats",
  186. "/proc/sched_debug",
  187. "/proc/scsi",
  188. "/sys/firmware"
  189. ],
  190. "ReadonlyPaths": [
  191. "/proc/bus",
  192. "/proc/fs",
  193. "/proc/irq",
  194. "/proc/sys",
  195. "/proc/sysrq-trigger"
  196. ]
  197. },
  198. "GraphDriver": {
  199. "Data": {
  200. "LowerDir": "/var/lib/docker/overlay2/5bf033bfa1f6c088bd948a2d49f973f6062fc98d4acc5ee33ca4881137b55460-init/diff:/var/lib/docker/overlay2/d6e4be0166eac21b08ff7e34bb088bd7488086c5fd04603879424af35f8e6190/diff",
  201. "MergedDir": "/var/lib/docker/overlay2/5bf033bfa1f6c088bd948a2d49f973f6062fc98d4acc5ee33ca4881137b55460/merged",
  202. "UpperDir": "/var/lib/docker/overlay2/5bf033bfa1f6c088bd948a2d49f973f6062fc98d4acc5ee33ca4881137b55460/diff",
  203. "WorkDir": "/var/lib/docker/overlay2/5bf033bfa1f6c088bd948a2d49f973f6062fc98d4acc5ee33ca4881137b55460/work"
  204. },
  205. "Name": "overlay2"
  206. },
  207. "Mounts": [],
  208. "Config": {
  209. "Hostname": "cb1922b95b93",
  210. "Domainname": "",
  211. "User": "",
  212. "AttachStdin": true,
  213. "AttachStdout": true,
  214. "AttachStderr": true,
  215. "Tty": true,
  216. "OpenStdin": true,
  217. "StdinOnce": true,
  218. "Env": [
  219. "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
  220. ],
  221. "Cmd": [
  222. "/bin/bash"
  223. ],
  224. "Image": "centos",
  225. "Volumes": null,
  226. "WorkingDir": "",
  227. "Entrypoint": null,
  228. "OnBuild": null,
  229. "Labels": {
  230. "org.label-schema.build-date": "20210915",
  231. "org.label-schema.license": "GPLv2",
  232. "org.label-schema.name": "CentOS Base Image",
  233. "org.label-schema.schema-version": "1.0",
  234. "org.label-schema.vendor": "CentOS"
  235. }
  236. },
  237. "NetworkSettings": {
  238. "Bridge": "",
  239. "SandboxID": "ac455100992ba8412440bbb5e166e42c5b927876129526e4a7d9365751821bda",
  240. "HairpinMode": false,
  241. "LinkLocalIPv6Address": "",
  242. "LinkLocalIPv6PrefixLen": 0,
  243. "Ports": {},
  244. "SandboxKey": "/var/run/docker/netns/ac455100992b",
  245. "SecondaryIPAddresses": null,
  246. "SecondaryIPv6Addresses": null,
  247. "EndpointID": "",
  248. "Gateway": "",
  249. "GlobalIPv6Address": "",
  250. "GlobalIPv6PrefixLen": 0,
  251. "IPAddress": "",
  252. "IPPrefixLen": 0,
  253. "IPv6Gateway": "",
  254. "MacAddress": "",
  255. "Networks": {
  256. "mynet": {
  257. "IPAMConfig": {},
  258. "Links": null,
  259. "Aliases": [
  260. "cb1922b95b93"
  261. ],
  262. "NetworkID": "0096a971fd2c66400e53cbae5e53eceedc2f90d5685917e9534640a3535c0ef1",
  263. "EndpointID": "0ba210710980230079b17d9bfa360bcee10a310ac7ee88c4e0174c4c6cc425cf",
  264. "Gateway": "192.168.0.1",
  265. "IPAddress": "192.168.0.4",
  266. "IPPrefixLen": 16,
  267. "IPv6Gateway": "",
  268. "GlobalIPv6Address": "",
  269. "GlobalIPv6PrefixLen": 0,
  270. "MacAddress": "02:42:c0:a8:00:04",
  271. "DriverOpts": {}
  272. },
  273. "test-network": {
  274. "IPAMConfig": {},
  275. "Links": null,
  276. "Aliases": [
  277. "cb1922b95b93"
  278. ],
  279. "NetworkID": "799426d70aa28b73b4a777c85b338186eafadd1558b13c43e07a9fd9a8b545e7",
  280. "EndpointID": "c5cac97189a8f9994ccd73ae9df1e0d28aa16dfde923dc241e2f3a3a4fda01b5",
  281. "Gateway": "172.18.0.1",
  282. "IPAddress": "172.18.0.3",
  283. "IPPrefixLen": 16,
  284. "IPv6Gateway": "",
  285. "GlobalIPv6Address": "",
  286. "GlobalIPv6PrefixLen": 0,
  287. "MacAddress": "02:42:ac:12:00:03",
  288. "DriverOpts": {}
  289. }
  290. }
  291. }
  292. }
  293. ]

test-network网卡下的centos01访问mynet网卡下的mynet-centos01、mynet-tomcat01

容器ip和容器名都可以连接

  1. [root@--- ~]# docker exec -it cb19 /bin/bash
  2. [root@cb1922b95b93 /]# ping 192.168.0.3
  3. PING 192.168.0.3 (192.168.0.3) 56(84) bytes of data.
  4. 64 bytes from 192.168.0.3: icmp_seq=1 ttl=64 time=0.186 ms
  5. 64 bytes from 192.168.0.3: icmp_seq=2 ttl=64 time=0.123 ms
  6. ^C
  7. --- 192.168.0.3 ping statistics ---
  8. 7 packets transmitted, 7 received, 0% packet loss, time 6000ms
  9. rtt min/avg/max/mdev = 0.090/0.127/0.186/0.029 ms
  10. [root@cb1922b95b93 /]# ping mynet-centos01
  11. PING mynet-centos01 (192.168.0.2) 56(84) bytes of data.
  12. 64 bytes from mynet-centos01.mynet (192.168.0.2): icmp_seq=1 ttl=64 time=0.105 ms
  13. 64 bytes from mynet-centos01.mynet (192.168.0.2): icmp_seq=2 ttl=64 time=0.130 ms
  14. ^C
  15. --- mynet-centos01 ping statistics ---
  16. 3 packets transmitted, 3 received, 0% packet loss, time 2001ms
  17. rtt min/avg/max/mdev = 0.105/0.116/0.130/0.013 ms
  18. [root@cb1922b95b93 /]# ping mynet-tomcat01
  19. PING mynet-tomcat01 (192.168.0.3) 56(84) bytes of data.
  20. 64 bytes from mynet-tomcat01.mynet (192.168.0.3): icmp_seq=1 ttl=64 time=0.085 ms
  21. 64 bytes from mynet-tomcat01.mynet (192.168.0.3): icmp_seq=2 ttl=64 time=0.102 ms
  22. ^C
  23. --- mynet-tomcat01 ping statistics ---
  24. 2 packets transmitted, 2 received, 0% packet loss, time 1001ms
  25. rtt min/avg/max/mdev = 0.085/0.093/0.102/0.012 ms
  26. [root@cb1922b95b93 /]#