查看容器的各种数据,其中IPAddress就是容器IP
docker inspect 容器ID
anin@anin ~ % docker inspect mysql
[
{
"Id": "3af7b3a37f3fb902a13cff7c564e55b06212daf651cafaeaa331080e010b7f58",
"Created": "2022-04-01T01:45:06.436668555Z",
"Path": "/entrypoint.sh",
"Args": [
"mysqld"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 11387,
"ExitCode": 0,
"Error": "",
"StartedAt": "2022-04-01T01:45:06.683270972Z",
"FinishedAt": "0001-01-01T00:00:00Z",
"Health": {
"Status": "healthy",
"FailingStreak": 0,
"Log": [
{
"Start": "2022-04-01T03:45:30.022663175Z",
"End": "2022-04-01T03:45:30.106074634Z",
"ExitCode": 0,
"Output": "mysqld is alive\n"
},
{
"Start": "2022-04-01T03:46:00.131812592Z",
"End": "2022-04-01T03:46:00.231912634Z",
"ExitCode": 0,
"Output": "mysqld is alive\n"
},
{
"Start": "2022-04-01T03:46:30.255841759Z",
"End": "2022-04-01T03:46:30.350379509Z",
"ExitCode": 0,
"Output": "mysqld is alive\n"
},
{
"Start": "2022-04-01T03:47:00.35626955Z",
"End": "2022-04-01T03:47:00.433557342Z",
"ExitCode": 0,
"Output": "mysqld is alive\n"
},
{
"Start": "2022-04-01T03:47:30.452643884Z",
"End": "2022-04-01T03:47:30.542393551Z",
"ExitCode": 0,
"Output": "mysqld is alive\n"
}
]
}
},
"Image": "sha256:0f37da883ef808e4956d4a4e64743427973aab0b0a03e5b34013b4b590b0af71",
"ResolvConfPath": "/var/lib/docker/containers/3af7b3a37f3fb902a13cff7c564e55b06212daf651cafaeaa331080e010b7f58/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/3af7b3a37f3fb902a13cff7c564e55b06212daf651cafaeaa331080e010b7f58/hostname",
"HostsPath": "/var/lib/docker/containers/3af7b3a37f3fb902a13cff7c564e55b06212daf651cafaeaa331080e010b7f58/hosts",
"LogPath": "/var/lib/docker/containers/3af7b3a37f3fb902a13cff7c564e55b06212daf651cafaeaa331080e010b7f58/3af7b3a37f3fb902a13cff7c564e55b06212daf651cafaeaa331080e010b7f58-json.log",
"Name": "/mysql",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {
"3306/tcp": [
{
"HostIp": "",
"HostPort": "3306"
}
]
},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"CgroupnsMode": "private",
"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": null,
"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/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",
"MergedDir": "/var/lib/docker/overlay2/3761374d2f90d18c8b5e77aef7e072177462db9707b9f250189bfba515cc562a/merged",
"UpperDir": "/var/lib/docker/overlay2/3761374d2f90d18c8b5e77aef7e072177462db9707b9f250189bfba515cc562a/diff",
"WorkDir": "/var/lib/docker/overlay2/3761374d2f90d18c8b5e77aef7e072177462db9707b9f250189bfba515cc562a/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "3af7b3a37f3f",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"3306/tcp": {},
"33060/tcp": {},
"33061/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"MYSQL_ROOT_PASSWORD=123456",
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"MYSQL_UNIX_PORT=/var/lib/mysql/mysql.sock"
],
"Cmd": [
"mysqld"
],
"Healthcheck": {
"Test": [
"CMD-SHELL",
"/healthcheck.sh"
]
},
"Image": "mysql/mysql-server",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": [
"/entrypoint.sh"
],
"OnBuild": null,
"Labels": {}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "f7cf29e039f245623ff11c41a22f98b823e1b8f1c7d4dca32362f3ccced8e2dc",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"3306/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "3306"
}
],
"33060/tcp": null,
"33061/tcp": null
},
"SandboxKey": "/var/run/docker/netns/f7cf29e039f2",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "1062bee0d7e700d74a1a254b096d049060af5405bab153851dfc31717198df34",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:02",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "2b0a639fb4e4cf64629e06eeeb90f2f5c03173f4f4cca4eb83a04f8a8b8f0a93",
"EndpointID": "1062bee0d7e700d74a1a254b096d049060af5405bab153851dfc31717198df34",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02",
"DriverOpts": null
}
}
}
}
]
使用如下命令,直接筛选
docker inspect --format='{{.NetworkSettings.IPAddress}}' {容器名/id}