当前位置 : 主页 > 操作系统 > centos >

docker inspect 操作详解

来源:互联网 收集:自由互联 发布时间:2023-03-17
目录 一、功能介绍 二、参数说明 三、docker inspect -f 更多用法 四、参考文档 一、功能介绍 docker inspect 会以 json 格式得到 docker 镜像/容器的元数据。 如下,查看mysql镜像的完整元数据:
目录
  • 一、功能介绍
  • 二、参数说明
  • 三、docker inspect -f 更多用法
  • 四、参考文档

一、功能介绍

docker inspect 会以 json 格式得到 docker 镜像/容器的元数据。

如下,查看mysql镜像的完整元数据:

docker inspect mysql:5.7
[
    {
        "Id": "sha256:1d7aba9171693947d53f474014821972bf25d72b7d143ce4af4c8d8484623417",
        "RepoTags": [
            "mysql:5.7"
        ],
        "RepoDigests": [
            "mysql@sha256:d9b934cdf6826629f8d02ea01f28b2c4ddb1ae27c32664b14867324b3e5e1291"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2021-09-03T07:25:25.419696974Z",
        "Container": "9979ac05c87381fb984ce556c94ac87ef0b0209eca50e9a0d81d28756557c2ce",
        "ContainerConfig": {
            "Hostname": "9979ac05c873",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "3306/tcp": {},
                "33060/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "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.35-1debian10"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "CMD [\"mysqld\"]"
            ],
            "Image": "sha256:cae8446f5d2e62008d7a1e3bc66ca14ced29fa4c4d0237cf4e6857fd5ca0c18d",
            "Volumes": {
                "/var/lib/mysql": {}
            },
            "WorkingDir": "",
            "Entrypoint": [
                "docker-entrypoint.sh"
            ],
            "OnBuild": null,
            "Labels": {}
        },
        "DockerVersion": "20.10.7",
        "Author": "",
        "Config": {
            "Hostname": "",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "3306/tcp": {},
                "33060/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "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.35-1debian10"
            ],
            "Cmd": [
                "mysqld"
            ],
            "Image": "sha256:cae8446f5d2e62008d7a1e3bc66ca14ced29fa4c4d0237cf4e6857fd5ca0c18d",
            "Volumes": {
                "/var/lib/mysql": {}
            },
            "WorkingDir": "",
            "Entrypoint": [
                "docker-entrypoint.sh"
            ],
            "OnBuild": null,
            "Labels": null
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 448211969,
        "VirtualSize": 448211969,
        "GraphDriver": {
            "Data": {
                "RootDir": "/var/lib/docker/overlay/e96512345b168f8f1f03e341bdc1e305687a8a54eefab1dfd5e21665ee01933a/root"
            },
            "Name": "overlay"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:d000633a56813933cb0ac5ee3246cf7a4c0205db6290018a169d7cb096581046",
                "sha256:731f7c21360385893ff752e1200bf901f7936f973801eb6f10dc81f249920994",
                "sha256:b2830993d63aac4a4c4c3cdea0ccae39c14d53e69d82d4a6541b35670431f244",
                "sha256:97a2a3481f0d61f26f855842ffb8170680a68659ab724042b3a000941a5a0a4e",
                "sha256:35e94dafc854af4a22dd101bc5f6b0b453c91d50ef9893228ae9b41d5fd99226",
                "sha256:2eaac5532d4479e5e821f724c854b8bc38527708ff484397b841561e21a8fc9a",
                "sha256:89638afc97cdd7709e24a927a87520751464fbb3af9b564e591f0a783b6276fc",
                "sha256:71cabb0b234c522858b8398181aac5ff1a98ec6be8ba71c4079a3cb093f6b4de",
                "sha256:b09979068448d00f4513f2bb8b5a4e3734bbbddc526ed5205fbd52f442727167",
                "sha256:67536a20af848ab1861ea5e5bf336e223a474e7c411bf21761b8b55f40bde9b9",
                "sha256:7449e60227868d770c9fd6437a85717bd6d09667e1e066190af887f687c62372"
            ]
        },
        "Metadata": {
            "LastTagTime": "0001-01-01T00:00:00Z"
        }
    }
]


如下为以mysql:5.7启动的容器的完整元数据:

docker run -itd mysql:5.7 /bin/sh
492c91fb2b8ff1a334fcd64ba3e46585701e7e5809f564bc2c65f3a655458db3

docker inspect 492c91fb2b8ff1a334fcd64ba3e46585701e7e5809f564bc2c65f3a655458db3
[
    {
        "Id": "492c91fb2b8ff1a334fcd64ba3e46585701e7e5809f564bc2c65f3a655458db3",
        "Created": "2022-06-13T09:01:22.321391083Z",
        "Path": "docker-entrypoint.sh",
        "Args": [
            "/bin/sh"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 48552,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2022-06-13T09:01:22.584406847Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:1d7aba9171693947d53f474014821972bf25d72b7d143ce4af4c8d8484623417",
        "ResolvConfPath": "/var/lib/docker/containers/492c91fb2b8ff1a334fcd64ba3e46585701e7e5809f564bc2c65f3a655458db3/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/492c91fb2b8ff1a334fcd64ba3e46585701e7e5809f564bc2c65f3a655458db3/hostname",
        "HostsPath": "/var/lib/docker/containers/492c91fb2b8ff1a334fcd64ba3e46585701e7e5809f564bc2c65f3a655458db3/hosts",
        "LogPath": "/var/lib/docker/containers/492c91fb2b8ff1a334fcd64ba3e46585701e7e5809f564bc2c65f3a655458db3/492c91fb2b8ff1a334fcd64ba3e46585701e7e5809f564bc2c65f3a655458db3-json.log",
        "Name": "/silly_antonelli",
        "RestartCount": 0,
        "Driver": "overlay",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {
                    "max-file": "10",
                    "max-size": "100m"
                }
            },
            "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": 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": [
                {
                    "Name": "NOFILE",
                    "Hard": 655350,
                    "Soft": 655350
                }
            ],
            "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/overlay/e96512345b168f8f1f03e341bdc1e305687a8a54eefab1dfd5e21665ee01933a/root",
                "MergedDir": "/var/lib/docker/overlay/3fbd4444a83b03d8684acd87e38bcaac658d4e038f6e27aaabc10b20152f1563/merged",
                "UpperDir": "/var/lib/docker/overlay/3fbd4444a83b03d8684acd87e38bcaac658d4e038f6e27aaabc10b20152f1563/upper",
                "WorkDir": "/var/lib/docker/overlay/3fbd4444a83b03d8684acd87e38bcaac658d4e038f6e27aaabc10b20152f1563/work"
            },
            "Name": "overlay"
        },
        "Mounts": [
            {
                "Type": "volume",
                "Name": "cb8a9a3d23814b91df3566e7b14b355cc381aa5719297907a68f6d99a1472294",
                "Source": "/var/lib/docker/volumes/cb8a9a3d23814b91df3566e7b14b355cc381aa5719297907a68f6d99a1472294/_data",
                "Destination": "/var/lib/mysql",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],
        "Config": {
            "Hostname": "492c91fb2b8f",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "3306/tcp": {},
                "33060/tcp": {}
            },
            "Tty": true,
            "OpenStdin": true,
            "StdinOnce": false,
            "Env": [
                "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.35-1debian10"
            ],
            "Cmd": [
                "/bin/sh"
            ],
            "Image": "mysql:5.7",
            "Volumes": {
                "/var/lib/mysql": {}
            },
            "WorkingDir": "",
            "Entrypoint": [
                "docker-entrypoint.sh"
            ],
            "OnBuild": null,
            "Labels": {}
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "a2f0fe86422968b0c5e1d0b7e4dd66a742dedfc8b01b56eb30649449969f4f12",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "3306/tcp": null,
                "33060/tcp": null
            },
            "SandboxKey": "/data/docker/exec/netns/a2f0fe864229",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "d41dfa3aadb23af2c459845e9ba11e87a13e7d96e170c879b6b14db76d443168",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.4",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:04",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "9cbe344a6886a489cd1b3d4cef5befc454d477d4c5ac59756d8bdf579739f20a",
                    "EndpointID": "d41dfa3aadb23af2c459845e9ba11e87a13e7d96e170c879b6b14db76d443168",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.4",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:04",
                    "DriverOpts": null
                }
            }
        }
    }
]

二、参数说明

2.1、-f

通常我们需要获取某一个具体的key,会用grep,如下,grep会获取到其他的数据,不够完整或者有冗余,还得进一步处理,但是 -f 可以解决这个问题:

docker inspect mysql:5.7 | grep Layers
            "Layers": [

docker inspect mysql:5.7 | grep -A 30 Layers
            "Layers": [
                "sha256:d000633a56813933cb0ac5ee3246cf7a4c0205db6290018a169d7cb096581046",
                "sha256:731f7c21360385893ff752e1200bf901f7936f973801eb6f10dc81f249920994",
                "sha256:b2830993d63aac4a4c4c3cdea0ccae39c14d53e69d82d4a6541b35670431f244",
                "sha256:97a2a3481f0d61f26f855842ffb8170680a68659ab724042b3a000941a5a0a4e",
                "sha256:35e94dafc854af4a22dd101bc5f6b0b453c91d50ef9893228ae9b41d5fd99226",
                "sha256:2eaac5532d4479e5e821f724c854b8bc38527708ff484397b841561e21a8fc9a",
                "sha256:89638afc97cdd7709e24a927a87520751464fbb3af9b564e591f0a783b6276fc",
                "sha256:71cabb0b234c522858b8398181aac5ff1a98ec6be8ba71c4079a3cb093f6b4de",
                "sha256:b09979068448d00f4513f2bb8b5a4e3734bbbddc526ed5205fbd52f442727167",
                "sha256:67536a20af848ab1861ea5e5bf336e223a474e7c411bf21761b8b55f40bde9b9",
                "sha256:7449e60227868d770c9fd6437a85717bd6d09667e1e066190af887f687c62372"
            ]
        },
        "Metadata": {
            "LastTagTime": "0001-01-01T00:00:00Z"
        }
    }
]
docker inspect -f '{{.RootFS.Layers}}' mysql:5.7 
[sha256:d000633a56813933cb0ac5ee3246cf7a4c0205db6290018a169d7cb096581046 sha256:731f7c21360385893ff752e1200bf901f7936f973801eb6f10dc81f249920994 sha256:b2830993d63aac4a4c4c3cdea0ccae39c14d53e69d82d4a6541b35670431f244 sha256:97a2a3481f0d61f26f855842ffb8170680a68659ab724042b3a000941a5a0a4e sha256:35e94dafc854af4a22dd101bc5f6b0b453c91d50ef9893228ae9b41d5fd99226 sha256:2eaac5532d4479e5e821f724c854b8bc38527708ff484397b841561e21a8fc9a sha256:89638afc97cdd7709e24a927a87520751464fbb3af9b564e591f0a783b6276fc sha256:71cabb0b234c522858b8398181aac5ff1a98ec6be8ba71c4079a3cb093f6b4de sha256:b09979068448d00f4513f2bb8b5a4e3734bbbddc526ed5205fbd52f442727167 sha256:67536a20af848ab1861ea5e5bf336e223a474e7c411bf21761b8b55f40bde9b9 sha256:7449e60227868d770c9fd6437a85717bd6d09667e1e066190af887f687c62372]


# 格式要求
'{{.一级key值.二级key值}}'

# 容器同样适用
docker inspect -f '{{.Config.Env}}' 492c91fb2b8ff1a334fcd64ba3e46585701e7e5809f564bc2c65f3a655458db3
[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.35-1debian10]

2.2、--type

指定具体类型 如:–type container 声明查看容器的元数据

docker inspect --type container 492c91fb2b8ff1a334fcd64ba3e46585701e7e5809f564bc2c65f3a655458db3
[
    {
        "Id": "492c91fb2b8ff1a334fcd64ba3e46585701e7e5809f564bc2c65f3a655458db3",
        "Created": "2022-06-13T09:01:22.321391083Z",
        "Path": "docker-entrypoint.sh",
        "Args": [
            "/bin/sh"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 48552,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2022-06-13T09:01:22.584406847Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
...
docker inspect --type image mysql:5.7 
[
    {
        "Id": "sha256:1d7aba9171693947d53f474014821972bf25d72b7d143ce4af4c8d8484623417",
        "RepoTags": [
            "mysql:5.7"
        ],
        "RepoDigests": [
            "mysql@sha256:d9b934cdf6826629f8d02ea01f28b2c4ddb1ae27c32664b14867324b3e5e1291"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2021-09-03T07:25:25.419696974Z",
        "Container": "9979ac05c87381fb984ce556c94ac87ef0b0209eca50e9a0d81d28756557c2ce",
        "ContainerConfig": {
            "Hostname": "9979ac05c873",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "3306/tcp": {},
                "33060/tcp": {}
            },
...

2.3、-s

显示总的文件大小

docker inspect -s 492c91fb2b8ff1a334fcd64ba3e46585701e7e5809f564bc2c65f3a655458db3 | grep Size
            "ShmSize": 67108864,
            "ConsoleSize": [
        "SizeRw": 37,
        "SizeRootFs": 448212006,

docker inspect  492c91fb2b8ff1a334fcd64ba3e46585701e7e5809f564bc2c65f3a655458db3 | grep Size
            "ShmSize": 67108864,
            "ConsoleSize": [

三、docker inspect -f 更多用法

简单地说,-f 的实参是个 Go 模版,并在容器/镜像的元数据上以该 Go 模版作为输入,最终返回模版指定的数据。Go 模版是一种模板引擎,让数据以指定的模式输出。这个概念对于 Web 开发者是非常熟悉的,Web 领域有很多模版引擎,比如 Jinga2(用于 Python 和 Flask)、Mustache、JSP 等等,看下面的简单示例:

docker inspect -f "The image's repotags is {{.RepoTags}}" mysql:5.7 
The image's repotags is [mysql:5.7]

3.1、Go 模版 详解

模版指令
{{ }} 语法用于处理模版指令,大括号外的任何字符都将直接输出。

上下文
“.” 表示“当前上下文”。大多数情况下表示了容器元数据的整个数据结构,但在某些情况下可以重新规定上下文,比如使用 with 函数:

docker inspect -f '{{.State.Pid}}' 492c91fb2b8ff1a334fcd64ba3e46585701e7e5809f564bc2c65f3a655458db3
48552

docker inspect -f '{{with .State}} {{.Pid}} {{end}}' 492c91fb2b8ff1a334fcd64ba3e46585701e7e5809f564bc2c65f3a655458db3
 48552

$

可以使用 $ 来获取根上下文,只能获取一级key值

docker inspect -f '{{$.Name}} has pid {{with .State}} {{.Pid}} {{end}}' 492c91fb2b8ff1a334fcd64ba3e46585701e7e5809f564bc2c65f3a655458db3
/silly_antonelli has pid  48552 

注意,单独使用 “.” 本身也是可以的,将输出未格式化的完整元数据:

3.2、数据类型

inspect 数据可以由浮点数、字符串和布尔组成,可以使用 Go 模版内置函数进行比较判断。虽然 Go 模版支持整数,但目前 inspect 数据中的数值类型都是浮点数,而整数应该对于大多数场景更方便(详见该Issue)。使用字符串时可以使用双引号。

数据中不存在的值是不可以用来比较的:

docker inspect -f '{{.ExecIDs}}' 492c91fb2b8ff1a334fcd64ba3e46585701e7e5809f564bc2c65f3a655458db3
[]

docker inspect 492c91fb2b8ff1a334fcd64ba3e46585701e7e5809f564bc2c65f3a655458db3 | grep ExecIDs
        "ExecIDs": null,

docker inspect -f '{{eq .ExecIDs .ExecIDs}}' 492c91fb2b8ff1a334fcd64ba3e46585701e7e5809f564bc2c65f3a655458db3

Template parsing error: template: :1:2: executing "" at <eq .ExecIDs .ExecIDs>: error calling eq: invalid type for comparison

3.3、数据结构

inspect 数据使用 map 以及数组保存。Map 结构可以通过 . 的链式来访问 map 内部数据:

docker inspect -f '{{.State.ExitCode}}' 492c91fb2b8ff1a334fcd64ba3e46585701e7e5809f564bc2c65f3a655458db3
0

3.3.1、如果需要获取的属性名称包含 “/”(比如下列示例数据)或者以数字开头,则不能直接通过级联调用获取信息。因为属性名称中的点号会被解析成级联信息,进而导致返回错误结果。即便使用引号将其包含也会提示语法格式错误。此时,需要通过 index 来读取指定属性信息。前面卷的例子可以这样写:

docker inspect -f '{{.Config.Volumes./var/lib/mysql}}' 492c91fb2b8ff1a334fcd64ba3e46585701e7e5809f564bc2c65f3a655458db3
Template parsing error: template: :1: bad character U+002F '/'

# 双引号也不行
docker inspect -f '{{.NetworkSettings.Ports.3306/tcp}}' 492c91fb2b8ff1a334fcd64ba3e46585701e7e5809f564bc2c65f3a655458db3
Template parsing error: template: :1: unexpected ".3306" in operand

#docker inspect -f '{{.NetworkSettings.Ports."3306/tcp"}}' 492c91fb2b8ff1a334fcd64ba3e46585701e7e5809f564bc2c65f3a655458db3
Template parsing error: template: :1: bad character U+0022 '"'


# 正确写法   !!!注意 index 与之后的数据之间包含空格,与之后双引号括起来的也有空格,否则报错
docker inspect -f '{{index .NetworkSettings.Ports "3306/tcp"}}' 492c91fb2b8ff1a334fcd64ba3e46585701e7e5809f564bc2c65f3a655458db3
<no value>

docker inspect -f '{{index .Config.Volumes "/var/lib/mysql"}}' 492c91fb2b8ff1a334fcd64ba3e46585701e7e5809f564bc2c65f3a655458db3
{}

#报错如下
docker inspect -f '{{index.NetworkSettings.Ports "3306/tcp"}}' 492c91fb2b8ff1a334fcd64ba3e46585701e7e5809f564bc2c65f3a655458db3

Template parsing error: template: :1:2: executing "" at <index>: wrong number of args for index: want at least 1 got 0

3.3.2、如果返回结果是一个 map, slice, array 或 string,则可以使用 index 加索引序号(从零开始计数)来读取属性值

docker inspect -f '{{.HostConfig.ReadonlyPaths}}' 492c91fb2b8ff1a334fcd64ba3e46585701e7e5809f564bc2c65f3a655458db3
[/proc/bus /proc/fs /proc/irq /proc/sys /proc/sysrq-trigger]

docker inspect -f '{{index .HostConfig.ReadonlyPaths 0}}' 492c91fb2b8ff1a334fcd64ba3e46585701e7e5809f564bc2c65f3a655458db3
/proc/bus


# 另一种类型,包含多个键值对的获取
想要获取EndpointID的值:
"NetworkSettings": {
            "Bridge": "",
            "SandboxID": "a2f0fe86422968b0c5e1d0b7e4dd66a742dedfc8b01b56eb30649449969f4f12",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "3306/tcp": null,
                "33060/tcp": null
            },
            "SandboxKey": "/data/docker/exec/netns/a2f0fe864229",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "d41dfa3aadb23af2c459845e9ba11e87a13e7d96e170c879b6b14db76d443168",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.4",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:04",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "9cbe344a6886a489cd1b3d4cef5befc454d477d4c5ac59756d8bdf579739f20a",
                    "EndpointID": "d41dfa3aadb23af2c459845e9ba11e87a13e7d96e170c879b6b14db76d443168",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.4",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:04",
                    "DriverOpts": null
                }
            }
        }


docker inspect -f '{{index .NetworkSettings.Networks.bridge 4}}' 492c91fb2b8ff1a334fcd64ba3e46585701e7e5809f564bc2c65f3a655458db3

Template parsing error: template: :1:2: executing "" at <index .NetworkSettings.Networks.bridge 4>: error calling index: value has type int; should be string

docker inspect -f '{{index .NetworkSettings.Networks.bridge "4"}}' 492c91fb2b8ff1a334fcd64ba3e46585701e7e5809f564bc2c65f3a655458db3
<no value>

docker inspect -f '{{index .NetworkSettings.Networks.bridge "EndpointID"}}' 492c91fb2b8ff1a334fcd64ba3e46585701e7e5809f564bc2c65f3a655458db3
d41dfa3aadb23af2c459845e9ba11e87a13e7d96e170c879b6b14db76d443168

3.4、函数

除了 index 函数,其他很多函数也很常用。比如逻辑函数 and、or 可以返回布尔结果。注意,函数是不能放在中间:

docker inspect -f '{{true and true}}' 492c91fb2b8ff1a334fcd64ba3e46585701e7e5809f564bc2c65f3a655458db3

Template parsing error: template: :1:2: executing "" at <true>: can't give argument to non-function true

docker inspect -f '{{and true true}}' 492c91fb2b8ff1a334fcd64ba3e46585701e7e5809f564bc2c65f3a655458db3
true

下面是一些常用的比较函数:

eq (等于)
ne (不等于)
lt (小于)
le (小于等于)
gt (大于)
ge (大于等于)

我们可以用这些函数来比较字符串、浮点数或整数:

docker inspect -f '{{and true true}}' 492c91fb2b8ff1a334fcd64ba3e46585701e7e5809f564bc2c65f3a655458db3
true

docker inspect -f '{{eq "abc" "abc"}}'  492c91fb2b8ff1a334fcd64ba3e46585701e7e5809f564bc2c65f3a655458db3
true

docker inspect -f '{{ge 1 3}}' 492c91fb2b8ff1a334fcd64ba3e46585701e7e5809f564bc2c65f3a655458db3
false

docker inspect -f '{{lt 4.5 4.6}}' 492c91fb2b8ff1a334fcd64ba3e46585701e7e5809f564bc2c65f3a655458db3
true

docker inspect -f '{{ne 4.5 4.5}}' 492c91fb2b8ff1a334fcd64ba3e46585701e7e5809f564bc2c65f3a655458db3
false

要注意的是操作数类型必须匹配,数字比较时也需要类型匹配:

docker inspect -f '{{ne 4 4.5}}' 492c91fb2b8ff1a334fcd64ba3e46585701e7e5809f564bc2c65f3a655458db3

Template parsing error: template: :1:2: executing "" at <ne 4 4.5>: error calling ne: incompatible types for comparison

docker inspect -f '{{lt 4.5 4.6}}' 492c91fb2b8ff1a334fcd64ba3e46585701e7e5809f564bc2c65f3a655458db3
true

docker inspect -f '{{gt .State.Pid 1}}' 492c91fb2b8ff1a334fcd64ba3e46585701e7e5809f564bc2c65f3a655458db3
true

docker inspect -f '{{gt .State.Pid 1.0}}' 492c91fb2b8ff1a334fcd64ba3e46585701e7e5809f564bc2c65f3a655458db3

Template parsing error: template: :1:2: executing "" at <gt .State.Pid 1.0>: error calling gt: incompatible types for comparison

比较特殊的是,它支持多个参数进行与比较,此时,它会将第一个参数和其余参数依次比较,返回下式的结果:

{{if eq true .Var1 .Var2 .Var3}}{{end}}
# 效果等同于:
arg1==arg2 || arg1==arg3 || arg1==arg4

另外,可以使用 json 函数来生成 JSON 输出:

docker inspect -f '{{json .NetworkSettings.Ports}}' 492c91fb2b8ff1a334fcd64ba3e46585701e7e5809f564bc2c65f3a655458db3
{"3306/tcp":null,"33060/tcp":null}

3.5、if 语句

条件语句 if 可以和前面的比较函数一起使用:

$ docker inspect -f '{{if eq .State.ExitCode 0}} 
Normal Exit
{{else if eq .State.ExitCode 1}} 
Not a Normal Exit 
{{else}} 
Still Not a Normal Exit 
{{end}}' 492c91fb2b8ff1a334fcd64ba3e46585701e7e5809f564bc2c65f3a655458db3
 
Normal Exit    //假设ExitCode为0时的结果

优化显示后为:

$ docker inspect -f '{{if eq .State.ExitCode 0}} 
Normal Exit
{{else if eq .State.ExitCode 1}} 
Not a Normal Exit 
{{else}} 
Still Not a Normal Exit 
{{end}}' 492c91fb2b8ff1a334fcd64ba3e46585701e7e5809f564bc2c65f3a655458db3
 
Normal Exit    //假设ExitCode为0时的结果

注意,{{end}} 语句必须有,else if 和 else 按需使用。

3.6、range迭代数据

range 用于遍历结构内返回值的所有数据。支持的类型包括 array, slice, map 和 channel。使用要点:

对应的值长度为 0 时,range 不会执行。
结构内部如要使用外部的变量,需要在前面加 引用,比如Var2。
range 也支持 else 操作。效果是:当返回值为空或长度为 0 时执行 else 内的内容。

docker inspect --format '{{.State.Pid}}{{.State.ExitCode}}' $INSTANCE_ID
240390
 
docker inspect --format '{{print .State.Pid .State.ExitCode}}' $INSTANCE_ID
24039 0
 
docker inspect --format '{{.State.Pid}}{{println " 从这换行"}}{{.State.ExitCode}}' $INSTANCE_ID
24039 从这换行
0
 
docker inspect --format '{{printf "Pid:%d ExitCode:%d" .State.Pid .State.ExitCode}}' $INSTANCE_ID
Pid:24039 ExitCode:0
123456789101112

3.7、打印信息

docker --format 默认调用 go语言的 print 函数对模板中的字符串进行输出。而 go语言还有另外几种相似的内置函数,对比说明如下:

print: 将传入的对象转换为字符串并写入到标准输出中。如果后跟多个参数,输出结果之间会自动填充空格进行分隔。
println: 功能和 print 类似,但会在结尾添加一个换行符。也可以直接使用 {{println}} 来换行。
printf: 与 shell 等环境一致,可配合占位符用于格式化输出。

docker inspect --format '{{.State.Pid}}{{.State.ExitCode}}' $INSTANCE_ID
240390
 
docker inspect --format '{{print .State.Pid .State.ExitCode}}' $INSTANCE_ID
24039 0
 
docker inspect --format '{{.State.Pid}}{{println " 从这换行"}}{{.State.ExitCode}}' $INSTANCE_ID
24039 从这换行
0
 
docker inspect --format '{{printf "Pid:%d ExitCode:%d" .State.Pid .State.ExitCode}}' $INSTANCE_ID
Pid:24039 ExitCode:0
123456789101112

3.8、管道

管道 即 pipeline ,与 shell 中类似,可以是上下文的变量输出,也可以是函数通过管道传递的返回值。

{{.Con | markdown | addlinks}}
{{.Name | printf "%s"}}

3.9、Docker内置函数

3.9.1、 json

Docker 默认以字符串显示返回结果。而该函数可以将结果格式化为压缩后的 json 格式数据。

# 获取 Config 字段对应的 json 数据
docker inspect --format='{{json .Config}}' $INSTANCE_ID

3.9.2、 join

用指定的字符串将返回结果连接后一起展示。操作对象必须是字符串数组

# 输出容器配置的所有 Entrypoint 参数,以 " , " 分隔:
docker inspect --format '{{join .Config.Entrypoint " , "}}' $INSTANCE_ID

3.9.3、 lower

将返回结果中的字母全部转换为小写。操作对象必须是字符串。

docker inspect --format "{{lower .Name}}" $INSTANCE_ID

3.9.4、upper

将返回结果中的字母全部转换为大写。操作对象必须是字符串。

docker inspect --format "{{lower .Name}}" $INSTANCE_ID

3.9.5、 title

将返回结果的首字母转换为大写。操作对象必须是字符串,而且不能是纯数字。

docker inspect --format "{{title .State.Status}}" $INSTANCE_ID

3.9.6、split

使用指定分隔符将返回结果拆分为字符串列表。操作对象必须是字符串且不能是纯数字。同时,字符串中必须包含相应的分隔符,否则会直接忽略操作。

docker inspect --format '{{split .HostsPath "/"}}' $INSTANCE_ID

3.10、常用docker inspect --format 输出示例

$ docker ps
CONTAINER ID        IMAGE                            COMMAND                  CREATED             STATUS              PORTS               NAMES
2ed603e52896        172.16.60.214:5000/nginx   "/bin/sh -c '/usr/..."   13 minutes ago      Up 13 minutes                           docker-test111
24e6607534f1        172.16.60.214:5000/nginx   "/bin/sh -c '/usr/..."   13 minutes ago      Up 13 minutes                           docker-test11
19be6b264b6e        172.16.60.214:5000/nginx   "/bin/sh -c '/usr/..."   13 minutes ago      Up 13 minutes                           docker-test1
  
1) 获取容器的IP (后面使用容器名或容器ID都可以)
$ docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -q)
192.10.160.193
173.20.19.128
17.16.10.128
  
$ docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' docker-test1
17.16.10.128
  
2) 获取容器的MAC地址
$ docker inspect --format='{{range .NetworkSettings.Networks}}{{.MacAddress}}{{end}}' $(docker ps -a -q)
ee:ee:ee:ee:ee:ee
ee:ee:ee:ee:ee:ee
ee:ee:ee:ee:ee:ee
  
[root@node1 ~]# docker inspect --format='{{range .NetworkSettings.Networks}}{{.MacAddress}}{{end}}' docker-test1
ee:ee:ee:ee:ee:ee
  
3) 获取容器Name
$ docker inspect --format='{{.Name}}' $(docker ps -aq)
/docker-test111
/docker-test11
/docker-test1
/calico-node
  
$ docker inspect --format='{{.Name}}' $(docker ps -aq)|cut -d"/" -f2
docker-test111
docker-test11
docker-test1
  
$ docker inspect --format='{{.Name}}' docker-test1
/docker-test1
  
$ docker inspect --format='{{.Name}}' docker-test1|cut -d"/" -f2
docker-test1
  
4) 获取容器Hostname
$ docker inspect --format '{{ .Config.Hostname }}' $(docker ps -q)
2ed603e52896
24e6607534f1
19be6b264b6e
  
$ docker inspect --format '{{ .Config.Hostname }}' docker-test1
19be6b264b6e
  
5) Hostname Name IP
$ docker inspect --format 'Hostname:{{ .Config.Hostname }}  Name:{{.Name}} IP:{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -q)
Hostname:2ed603e52896  Name:/docker-test111 IP:192.10.160.193
Hostname:24e6607534f1  Name:/docker-test11 IP:173.20.19.128
Hostname:19be6b264b6e  Name:/docker-test1 IP:17.16.10.128
  
$ docker inspect --format 'Hostname:{{ .Config.Hostname }}  Name:{{.Name}} IP:{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' docker-test1
Hostname:19be6b264b6e  Name:/docker-test1 IP:17.16.10.128
  
6) 获取容器的log path
$ docker inspect --format='{{.LogPath}}' `docker ps -a -q`
$ docker inspect --format='{{.LogPath}}' docker-test1
  
7) 获取容器的image镜像名称
$ docker inspect --format='{{.Config.Image}}' `docker ps -a -q`
172.16.60.214:5000/nginx
172.16.60.214:5000/nginx
172.16.60.214:5000/nginx
quay.io/calico/node:v2.6.10
$ docker inspect --format='{{.Config.Image}}' docker-test1
172.16.60.214:5000/nginx
  
8) 获取容器绑定的端口(port bindings)
$ docker inspect --format='{{range $p, $conf := .NetworkSettings.Ports}} {{$p}} -> {{(index $conf 0).HostPort}} {{end}}' `docker ps -a -q`
$ docker inspect --format='{{range $p, $conf := .NetworkSettings.Ports}} {{$p}} -> {{(index $conf 0).HostPort}} {{end}}' docker-test1
 
9) 获取service实例的Ip
$ docker service ps my-test 
ID              NAME          IMAGE                         NODE                    DESIRED   STATE  CURRENT STATE    ERROR  PORTS
t71gqufekv2o  my-test.1  172.16.60.214:5000/nginx:latest  swarm-node2              Running    Running 16 seconds ago        
9cuq2yf10d60 my-test.2  172.16.60.214:5000/nginx:latest  swarm-manager-node  Running   Running 16 seconds ago        
srt2yo817kpv  my-test.3  172.16.60.214:5000/nginx:latest  swarm-node1               Running    Running 16 seconds ago  
 
$ docker inspect ` docker service ps my-test -q` --format '{{range .NetworksAttachments}}{{.Addresses}}{{end}}' | cut -d '[' -f2|cut -d ']' -f1
10.255.0.7/16
10.255.0.8/16
10.255.0.9/16
 
10) 获取service示例的container ID  (获取的是ID的全称,一般只要取ID前面12个字符就可以了)
$ docker inspect ` docker service ps my-test -q` --format '{{ .Status.ContainerStatus.ContainerID }}'
c6c18a74a465163757fe928fec9e633223200f92d1c59e5d2d77eabfaa5ae93a
5f558bb014ea3d3eef5c8d4bd70e2e3048d7fc6725538303be960ac658d93b32
dde578bf60190a63ed5c8c4a9f5a3044566a159e8debe8717342e263c6199f26

四、参考文档

1、https://blog.csdn.net/u013288190/article/details/108955218

2、https://blog.csdn.net/m0_45406092/article/details/103671832

3、https://ld246.com/article/1427784659823

到此这篇关于docker inspect 详解的文章就介绍到这了,更多相关docker inspect内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!

上一篇:Docker(黑马spring cloud笔记)详解
下一篇:没有了
网友评论