JSON 形式で Docker Desktop の状態を確認する
前のステップでは、docker info
のデフォルト出力形式を使用して Docker デーモンの状態を確認しました。デフォルト形式は人間が読みやすいですが、プログラム処理には適していません。このステップでは、スクリプトや自動化に適した JSON 形式で Docker 情報を取得する方法を学びます。
JSON 形式で出力を取得するには、docker info
コマンドに--format
フラグを追加し、形式をjson
として指定します。
ターミナルで以下のコマンドを実行してください:
docker info --format '{{json .}}'
このコマンドを分解してみましょう:
docker info
: Docker システム情報を取得する基本コマンド
--format '{{json .}}'
: 出力形式を指定するフラグ。'{{json .}}'
は Go テンプレート構文を使用しています。.
はdocker info
が返すデータ構造全体を表し、{{json .}}
はこの構造を JSON 文字列としてフォーマットします。
コマンドを実行すると、前回と同じ情報が単一の有効な JSON オブジェクトとして表示されます。出力は以下のようになります(正確な内容は Docker 環境によって異なります):
{
"ID": "...",
"Containers": 0,
"ContainersRunning": 0,
"ContainersPaused": 0,
"ContainersStopped": 0,
"Images": 0,
"Driver": "overlay2",
"SystemStatus": null,
"Plugins": {
"Volume": ["local"],
"Network": ["bridge", "host", "ipvlan", "macvlan", "null", "overlay"],
"Authorization": null,
"Log": [
"awslogs",
"fluentd",
"gcplogs",
"gelf",
"journald",
"json-file",
"local",
"logentries",
"splunk",
"syslog"
]
},
"MemoryLimit": true,
"SwapLimit": true,
"KernelMemory": true,
"CpuCfsPeriod": true,
"CpuCfsQuota": true,
"CpuShares": true,
"CpuSet": true,
"PidsLimit": true,
"OomKillDisable": true,
"IPv4Forwarding": true,
"BridgeNfIptables": true,
"BridgeNfIp6tables": true,
"Debug": false,
"NFd": -1,
"OomScoreAdj": -1,
"Goroutines": -1,
"SystemTime": "...",
"LoggingDriver": "json-file",
"CgroupDriver": "cgroupfs",
"CgroupVersion": "1",
"NEventsListener": -1,
"KernelVersion": "...",
"OperatingSystem": "...",
"OSType": "linux",
"Architecture": "x86_64",
"IndexServerAddress": "https://index.docker.io/v1/",
"RegistryConfig": {
"AllowNondistributableArtifactsAfterDate": "2020-12-25T00:00:00Z",
"InsecureRegistryCIDRs": ["127.0.0.0/8"],
"IndexConfigs": {
"docker.io": {
"Name": "docker.io",
"Mirrors": [],
"Secure": true,
"Official": true
}
},
"Mirrors": []
},
"NCPU": -1,
"MemTotal": -1,
"GenericResources": null,
"DockerRootDir": "/var/lib/docker",
"HttpProxy": "",
"HttpsProxy": "",
"NoProxy": "",
"ExperimentalBuild": false,
"LiveRestoreEnabled": false,
"Runtimes": { "runc": { "path": "runc" } },
"DefaultRuntime": "runc",
"Swarm": {
"NodeID": "",
"LocalNodeState": "inactive",
"ControlAvailable": false,
"Error": "",
"RemoteManagers": null
},
"ContainerdCommit": { "ID": "...", "Expected": "..." },
"RuncCommit": { "ID": "...", "Expected": "..." },
"InitCommit": { "ID": "...", "Expected": "..." },
"SecurityOptions": ["apparmor", "seccomp"],
"ProductLicense": "",
"DefaultAddressPools": null,
"Warnings": null
}
この JSON 出力はスクリプト言語や他のツールで簡単に解析できるため、Docker 関連のタスクを自動化するのに非常に便利です。