查看容器的各种数据,其中IPAddress就是容器IP

  1. docker inspect 容器ID
  1. anin@anin ~ % docker inspect mysql
  2. [
  3. {
  4. "Id": "3af7b3a37f3fb902a13cff7c564e55b06212daf651cafaeaa331080e010b7f58",
  5. "Created": "2022-04-01T01:45:06.436668555Z",
  6. "Path": "/entrypoint.sh",
  7. "Args": [
  8. "mysqld"
  9. ],
  10. "State": {
  11. "Status": "running",
  12. "Running": true,
  13. "Paused": false,
  14. "Restarting": false,
  15. "OOMKilled": false,
  16. "Dead": false,
  17. "Pid": 11387,
  18. "ExitCode": 0,
  19. "Error": "",
  20. "StartedAt": "2022-04-01T01:45:06.683270972Z",
  21. "FinishedAt": "0001-01-01T00:00:00Z",
  22. "Health": {
  23. "Status": "healthy",
  24. "FailingStreak": 0,
  25. "Log": [
  26. {
  27. "Start": "2022-04-01T03:45:30.022663175Z",
  28. "End": "2022-04-01T03:45:30.106074634Z",
  29. "ExitCode": 0,
  30. "Output": "mysqld is alive\n"
  31. },
  32. {
  33. "Start": "2022-04-01T03:46:00.131812592Z",
  34. "End": "2022-04-01T03:46:00.231912634Z",
  35. "ExitCode": 0,
  36. "Output": "mysqld is alive\n"
  37. },
  38. {
  39. "Start": "2022-04-01T03:46:30.255841759Z",
  40. "End": "2022-04-01T03:46:30.350379509Z",
  41. "ExitCode": 0,
  42. "Output": "mysqld is alive\n"
  43. },
  44. {
  45. "Start": "2022-04-01T03:47:00.35626955Z",
  46. "End": "2022-04-01T03:47:00.433557342Z",
  47. "ExitCode": 0,
  48. "Output": "mysqld is alive\n"
  49. },
  50. {
  51. "Start": "2022-04-01T03:47:30.452643884Z",
  52. "End": "2022-04-01T03:47:30.542393551Z",
  53. "ExitCode": 0,
  54. "Output": "mysqld is alive\n"
  55. }
  56. ]
  57. }
  58. },
  59. "Image": "sha256:0f37da883ef808e4956d4a4e64743427973aab0b0a03e5b34013b4b590b0af71",
  60. "ResolvConfPath": "/var/lib/docker/containers/3af7b3a37f3fb902a13cff7c564e55b06212daf651cafaeaa331080e010b7f58/resolv.conf",
  61. "HostnamePath": "/var/lib/docker/containers/3af7b3a37f3fb902a13cff7c564e55b06212daf651cafaeaa331080e010b7f58/hostname",
  62. "HostsPath": "/var/lib/docker/containers/3af7b3a37f3fb902a13cff7c564e55b06212daf651cafaeaa331080e010b7f58/hosts",
  63. "LogPath": "/var/lib/docker/containers/3af7b3a37f3fb902a13cff7c564e55b06212daf651cafaeaa331080e010b7f58/3af7b3a37f3fb902a13cff7c564e55b06212daf651cafaeaa331080e010b7f58-json.log",
  64. "Name": "/mysql",
  65. "RestartCount": 0,
  66. "Driver": "overlay2",
  67. "Platform": "linux",
  68. "MountLabel": "",
  69. "ProcessLabel": "",
  70. "AppArmorProfile": "",
  71. "ExecIDs": null,
  72. "HostConfig": {
  73. "Binds": null,
  74. "ContainerIDFile": "",
  75. "LogConfig": {
  76. "Type": "json-file",
  77. "Config": {}
  78. },
  79. "NetworkMode": "default",
  80. "PortBindings": {
  81. "3306/tcp": [
  82. {
  83. "HostIp": "",
  84. "HostPort": "3306"
  85. }
  86. ]
  87. },
  88. "RestartPolicy": {
  89. "Name": "no",
  90. "MaximumRetryCount": 0
  91. },
  92. "AutoRemove": false,
  93. "VolumeDriver": "",
  94. "VolumesFrom": null,
  95. "CapAdd": null,
  96. "CapDrop": null,
  97. "CgroupnsMode": "private",
  98. "Dns": [],
  99. "DnsOptions": [],
  100. "DnsSearch": [],
  101. "ExtraHosts": null,
  102. "GroupAdd": null,
  103. "IpcMode": "private",
  104. "Cgroup": "",
  105. "Links": null,
  106. "OomScoreAdj": 0,
  107. "PidMode": "",
  108. "Privileged": false,
  109. "PublishAllPorts": false,
  110. "ReadonlyRootfs": false,
  111. "SecurityOpt": null,
  112. "UTSMode": "",
  113. "UsernsMode": "",
  114. "ShmSize": 67108864,
  115. "Runtime": "runc",
  116. "ConsoleSize": [
  117. 0,
  118. 0
  119. ],
  120. "Isolation": "",
  121. "CpuShares": 0,
  122. "Memory": 0,
  123. "NanoCpus": 0,
  124. "CgroupParent": "",
  125. "BlkioWeight": 0,
  126. "BlkioWeightDevice": [],
  127. "BlkioDeviceReadBps": null,
  128. "BlkioDeviceWriteBps": null,
  129. "BlkioDeviceReadIOps": null,
  130. "BlkioDeviceWriteIOps": null,
  131. "CpuPeriod": 0,
  132. "CpuQuota": 0,
  133. "CpuRealtimePeriod": 0,
  134. "CpuRealtimeRuntime": 0,
  135. "CpusetCpus": "",
  136. "CpusetMems": "",
  137. "Devices": [],
  138. "DeviceCgroupRules": null,
  139. "DeviceRequests": null,
  140. "KernelMemory": 0,
  141. "KernelMemoryTCP": 0,
  142. "MemoryReservation": 0,
  143. "MemorySwap": 0,
  144. "MemorySwappiness": null,
  145. "OomKillDisable": null,
  146. "PidsLimit": null,
  147. "Ulimits": null,
  148. "CpuCount": 0,
  149. "CpuPercent": 0,
  150. "IOMaximumIOps": 0,
  151. "IOMaximumBandwidth": 0,
  152. "MaskedPaths": [
  153. "/proc/asound",
  154. "/proc/acpi",
  155. "/proc/kcore",
  156. "/proc/keys",
  157. "/proc/latency_stats",
  158. "/proc/timer_list",
  159. "/proc/timer_stats",
  160. "/proc/sched_debug",
  161. "/proc/scsi",
  162. "/sys/firmware"
  163. ],
  164. "ReadonlyPaths": [
  165. "/proc/bus",
  166. "/proc/fs",
  167. "/proc/irq",
  168. "/proc/sys",
  169. "/proc/sysrq-trigger"
  170. ]
  171. },
  172. "GraphDriver": {
  173. "Data": {
  174. "LowerDir": "/var/lib/docker/overlay2/3761374d2f90d18c8b5e77aef7e072177462db9707b9f250189bfba515cc562a-init/diff:/var/lib/docker/overlay2/77cdc0f4ecf528daf3b31cebede67c6117d6f5c063ebb3447279701aebadbc41/diff:/var/lib/docker/overlay2/f65edbd59ea5225da00b3ec8e54fd26b480c069a0b6d77e6b40da7bb2f30a140/diff:/var/lib/docker/overlay2/07cbb0b2ed16380414926e0314c39f4117a4a738e1524ad6d0348cdd7858e414/diff:/var/lib/docker/overlay2/b869a658fdb6de009c7a516b4eee45376b60b59b88986d34a5d04f083104379c/diff:/var/lib/docker/overlay2/38bdf935c74c4317b8a5ac74e65cf1de511574e292f3b306b468aeace731ed0c/diff:/var/lib/docker/overlay2/48d9909ad0b4e5723e40899b65a9a7624e895a153a12aeafa843c5470bbb1eb1/diff:/var/lib/docker/overlay2/1ea37d4c7e85a6364c6db6b583550a9f9dfae31c7f390fe94fcc118f2e9414b8/diff",
  175. "MergedDir": "/var/lib/docker/overlay2/3761374d2f90d18c8b5e77aef7e072177462db9707b9f250189bfba515cc562a/merged",
  176. "UpperDir": "/var/lib/docker/overlay2/3761374d2f90d18c8b5e77aef7e072177462db9707b9f250189bfba515cc562a/diff",
  177. "WorkDir": "/var/lib/docker/overlay2/3761374d2f90d18c8b5e77aef7e072177462db9707b9f250189bfba515cc562a/work"
  178. },
  179. "Name": "overlay2"
  180. },
  181. "Mounts": [],
  182. "Config": {
  183. "Hostname": "3af7b3a37f3f",
  184. "Domainname": "",
  185. "User": "",
  186. "AttachStdin": false,
  187. "AttachStdout": false,
  188. "AttachStderr": false,
  189. "ExposedPorts": {
  190. "3306/tcp": {},
  191. "33060/tcp": {},
  192. "33061/tcp": {}
  193. },
  194. "Tty": false,
  195. "OpenStdin": false,
  196. "StdinOnce": false,
  197. "Env": [
  198. "MYSQL_ROOT_PASSWORD=123456",
  199. "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
  200. "MYSQL_UNIX_PORT=/var/lib/mysql/mysql.sock"
  201. ],
  202. "Cmd": [
  203. "mysqld"
  204. ],
  205. "Healthcheck": {
  206. "Test": [
  207. "CMD-SHELL",
  208. "/healthcheck.sh"
  209. ]
  210. },
  211. "Image": "mysql/mysql-server",
  212. "Volumes": null,
  213. "WorkingDir": "",
  214. "Entrypoint": [
  215. "/entrypoint.sh"
  216. ],
  217. "OnBuild": null,
  218. "Labels": {}
  219. },
  220. "NetworkSettings": {
  221. "Bridge": "",
  222. "SandboxID": "f7cf29e039f245623ff11c41a22f98b823e1b8f1c7d4dca32362f3ccced8e2dc",
  223. "HairpinMode": false,
  224. "LinkLocalIPv6Address": "",
  225. "LinkLocalIPv6PrefixLen": 0,
  226. "Ports": {
  227. "3306/tcp": [
  228. {
  229. "HostIp": "0.0.0.0",
  230. "HostPort": "3306"
  231. }
  232. ],
  233. "33060/tcp": null,
  234. "33061/tcp": null
  235. },
  236. "SandboxKey": "/var/run/docker/netns/f7cf29e039f2",
  237. "SecondaryIPAddresses": null,
  238. "SecondaryIPv6Addresses": null,
  239. "EndpointID": "1062bee0d7e700d74a1a254b096d049060af5405bab153851dfc31717198df34",
  240. "Gateway": "172.17.0.1",
  241. "GlobalIPv6Address": "",
  242. "GlobalIPv6PrefixLen": 0,
  243. "IPAddress": "172.17.0.2",
  244. "IPPrefixLen": 16,
  245. "IPv6Gateway": "",
  246. "MacAddress": "02:42:ac:11:00:02",
  247. "Networks": {
  248. "bridge": {
  249. "IPAMConfig": null,
  250. "Links": null,
  251. "Aliases": null,
  252. "NetworkID": "2b0a639fb4e4cf64629e06eeeb90f2f5c03173f4f4cca4eb83a04f8a8b8f0a93",
  253. "EndpointID": "1062bee0d7e700d74a1a254b096d049060af5405bab153851dfc31717198df34",
  254. "Gateway": "172.17.0.1",
  255. "IPAddress": "172.17.0.2",
  256. "IPPrefixLen": 16,
  257. "IPv6Gateway": "",
  258. "GlobalIPv6Address": "",
  259. "GlobalIPv6PrefixLen": 0,
  260. "MacAddress": "02:42:ac:11:00:02",
  261. "DriverOpts": null
  262. }
  263. }
  264. }
  265. }
  266. ]

使用如下命令,直接筛选

  1. docker inspect --format='{{.NetworkSettings.IPAddress}}' {容器名/id}