目錄
- 一、功能介紹
- 二、參數說明
- 三、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