Docker命令 - inspect

获取容器/镜像的元数据
比如 : 容器ID , 容器名称 , 容器状态 , 容器镜像 , 挂载情况 , 网络信息 。。。

语法

docker inspect [OPTIONS] NAME|ID [NAME|ID...]
## 获取容器的元数据
## docker inspect 容器名称 | 容器ID
docker inspect mysql
docker inspect a7257e0aa936
## 获取镜像的元数据
## docker inspect 镜像名称:镜像版本
docker inspect mysql:8.0.22

OPTIONS说明 :

OPTION说明用法
-f指定返回值的模板文件docker inspect -f=’{{.NetworkSettings.Networks.bridge.IPAddress}}’ mysql
-s显示总的文件大小待更新
–type为指定类型返回JSON待更新

docker inspect -f 等同于 docker inspect --format

容器所有数据

## 不加任何参数查看容器所有元数据
docker inspect mysql
[
    {
        "Id": "a7257e0aa9363f7a8778903898f977fda7496e30ca85512b89daa7ac517ee87c",
        "Created": "2020-05-09T02:10:41.964421619Z",
        "Path": "docker-entrypoint.sh",
        "Args": [
            "mysqld"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 4228,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2020-12-12T13:52:02.539194695Z",
            "FinishedAt": "2020-12-11T16:24:36.534280851+08:00"
        },
        "Image": "sha256:f965319e89deaebf165f58d08b1a217971bf411e9d9860e294634b72d09ab6d0",
        "ResolvConfPath": "/var/lib/docker/containers/a7257e0aa9363f7a8778903898f977fda7496e30ca85512b89daa7ac517ee87c/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/a7257e0aa9363f7a8778903898f977fda7496e30ca85512b89daa7ac517ee87c/hostname",
        "HostsPath": "/var/lib/docker/containers/a7257e0aa9363f7a8778903898f977fda7496e30ca85512b89daa7ac517ee87c/hosts",
        "LogPath": "/var/lib/docker/containers/a7257e0aa9363f7a8778903898f977fda7496e30ca85512b89daa7ac517ee87c/a7257e0aa9363f7a8778903898f977fda7496e30ca85512b89daa7ac517ee87c-json.log",
        "Name": "/mysql",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": [
                "/mydata/mysql/data:/var/lib/mysql",
                "/mydata/mysql/conf:/etc/mysql",
                "/mydata/mysql/log:/var/log/mysql"
            ],
            "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,
            "Capabilities": null,
            "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/6cb9d7b92657d104a6abdf80b68947a3d1f6746786261b03b159cbabad11813d-init/diff:/var/lib/docker/overlay2/a9c588fcc1ace7bd53aae773acd4c8d36fc00f9931ccc06a3980b269e89dd210/diff:/var/lib/docker/overlay2/38b498dc8be5e336a1cb849e776e3d1c7b55d0507754cd31511d86309ad0a445/diff:/var/lib/docker/overlay2/075c41318baa13757096f08c44641bd57139d2483a52342df6d6436322ef80b4/diff:/var/lib/docker/overlay2/fab5fa0be14ab7a8e5b0a0176c31c948ceab24724fe1292496ece3235fd77baa/diff:/var/lib/docker/overlay2/4d4963c2da9b6bcacd4f1bc8be621b5fec091e9274a4e0a3f4c40ff774bc417e/diff:/var/lib/docker/overlay2/8e3c8753506de5121093837502d4579280c8f8e9ebee535520faa61069808182/diff:/var/lib/docker/overlay2/5c0d73aca135c604d1bebb4ebaad90b5b2eb164c22e0e00a9fff0dbae83d88ef/diff:/var/lib/docker/overlay2/6137cd5896087d8a95c099695405fc63932c0406fcab22a10ac53e67e591914e/diff:/var/lib/docker/overlay2/a4639b1dd1639c4ed4e64f275ec277a47c27f479ba870491c2bac598a769be16/diff:/var/lib/docker/overlay2/6d6969cd5a7b977ba0ce0315ca0edfc6f972946d354a149288bd25725dbd0b08/diff:/var/lib/docker/overlay2/23668f1a0e4108ff6104e8a580f714a3092b70923852e5ee12a40ab958ad36b2/diff",
                "MergedDir": "/var/lib/docker/overlay2/6cb9d7b92657d104a6abdf80b68947a3d1f6746786261b03b159cbabad11813d/merged",
                "UpperDir": "/var/lib/docker/overlay2/6cb9d7b92657d104a6abdf80b68947a3d1f6746786261b03b159cbabad11813d/diff",
                "WorkDir": "/var/lib/docker/overlay2/6cb9d7b92657d104a6abdf80b68947a3d1f6746786261b03b159cbabad11813d/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [
            {
                "Type": "bind",
                "Source": "/mydata/mysql/conf",
                "Destination": "/etc/mysql",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            },
            {
                "Type": "bind",
                "Source": "/mydata/mysql/data",
                "Destination": "/var/lib/mysql",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            },
            {
                "Type": "bind",
                "Source": "/mydata/mysql/log",
                "Destination": "/var/log/mysql",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            }
        ],
        "Config": {
            "Hostname": "a7257e0aa936",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "3306/tcp": {},
                "33060/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "MYSQL_ROOT_PASSWORD=root",
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "GOSU_VERSION=1.12",
                "MYSQL_MAJOR=5.7",
                "MYSQL_VERSION=5.7.30-1debian10"
            ],
            "Cmd": [
                "mysqld"
            ],
            "Image": "mysql:5.7",
            "Volumes": {
                "/var/lib/mysql": {}
            },
            "WorkingDir": "",
            "Entrypoint": [
                "docker-entrypoint.sh"
            ],
            "OnBuild": null,
            "Labels": {}
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "aa4e09dcacb7ebc46bb6738626917ff46896d3ea373e31a3d3bba6478d247890",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "3306/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "3306"
                    }
                ],
                "33060/tcp": null
            },
            "SandboxKey": "/var/run/docker/netns/aa4e09dcacb7",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "90ebb686af6e928d3b2c7171b14e9b03cf9212d396c42d51dbf022eeb7211cbc",
            "Gateway": "172.18.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.18.0.4",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:12:00:04",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "2cff131b9cb953b97f8882f1647afa2c809d52aa115629452460e217a8904373",
                    "EndpointID": "90ebb686af6e928d3b2c7171b14e9b03cf9212d396c42d51dbf022eeb7211cbc",
                    "Gateway": "172.18.0.1",
                    "IPAddress": "172.18.0.4",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:12:00:04",
                    "DriverOpts": null
                }
            }
        }
    }
]

日常使用命令

以下所有命令使用mysql为容器名执行 (根据自己需要修改容器名或容器ID)

## 获取容器ID
[root@centos ~]# docker inspect -f='{{.Id}}' mysql
a7257e0aa9363f7a8778903898f977fda7496e30ca85512b89daa7ac517ee87c
## 获取容器名字
[root@centos ~]# docker inspect -f='{{.Name}}' mysql
/mysql
## 获取容器创建时间
[root@centos ~]# docker inspect -f='{{.Created}}' mysql
2020-05-09T02:10:41.964421619Z
## 获取容器日志路径
[root@centos ~]# docker inspect -f='{{.LogPath}}' mysql
/var/lib/docker/containers/a7257e0aa9363f7a8778903898f977fda7496e30ca85512b89daa7ac517ee87c/a7257e0aa9363f7a8778903898f977fda7496e30ca85512b89daa7ac517ee87c-json.log
## 运行平台
[root@centos ~]# docker inspect -f='{{.Platform}}' mysql
linux
## 获取容器IP地址
[root@centos ~]# docker inspect -f='{{.NetworkSettings.Networks.bridge.IPAddress}}' mysql
172.18.0.4

其他查看方式

## -f 其实等同于 --format
[root@centos ~]# docker inspect -f='{{.NetworkSettings.Networks.bridge.IPAddress}}' mysql
172.18.0.4
[root@centos ~]# docker inspect --format='{{.NetworkSettings.Networks.bridge.IPAddress}}' mysql

## 上述命令是精确查找. 格式必须按照JSON的层次结构来 .


## 此方式为某个 {} 内寻找 , 比如下述命令未定义 bridge , 而是直接从 Networks 下找 IPAddress .
## 注意 : 前后必须加 range 和 {{end}}
## 并不是很好用  (✪✪✪✪✪不推荐✪✪✪✪✪)
[root@centos ~]# docker inspect -f='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' mysql
172.18.0.4


## grep 方式查看 (✪✪✪✪✪推荐使用✪✪✪✪✪) - 可以看的更全面
## 为什么不直接 docker inspect mysql , 是因为配置太多。找起来麻烦。 所以才精确查找。
## 但是我们也可以通过 grep 局部查找

## 查看容器网络信息 (关注红色关键字部分信息即可)
docker inspect mysql | grep Networks --color -a50
## 查看容器挂载信息 (关注红色关键字部分信息即可)
docker inspect mysql | grep Mounts --color -a50
## --color : 标记颜色 
## -a50 : 上下显示50行 (如果显示不全,可以根据需求修改)


Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐