In the previous step, you saw the default output of the docker info
command. This output is human-readable but can be difficult to parse programmatically. Docker provides the --format
flag to customize the output format. In this step, you will learn how to format the docker info
output as JSON.
The --format
flag uses Go's text/template package. To output the information as JSON, you can use the . | json
template.
Execute the following command in your terminal:
docker info --format '{{json .}}'
You should see the same information as before, but now formatted as a single JSON object:
{
"Client": {
"Context": "default",
"DebugMode": false,
"Plugins": {
"buildx": {
"Version": "v0.10.4",
"Build": "8842a472b58241d32996951f172f32259cd758e5"
},
"compose": {
"Version": "v2.17.2",
"Build": "6833407"
},
"scan": {
"Version": "v0.23.0",
"Build": "2c50987"
}
}
},
"Server": {
"Containers": 0,
"ContainersRunning": 0,
"ContainersPaused": 0,
"ContainersStopped": 0,
"Images": 0,
"ServerVersion": "20.10.21",
"StorageDriver": "overlay2",
"LoggingDriver": "json-file",
"CgroupDriver": "systemd",
"CgroupVersion": "2",
"Plugins": {
"Volume": ["local"],
"Network": ["bridge", "host", "ipvlan", "macvlan", "null", "overlay"],
"Log": [
"awslogs",
"fluentd",
"gcplogs",
"gelf",
"journald",
"json-file",
"local",
"logentries",
"splunk",
"syslog"
]
},
"Swarm": {
"NodeID": "",
"LocalNodeState": "inactive",
"ControlAvailable": false,
"Error": null,
"RemoteManagers": null
},
"Runtimes": {
"io.containerd.runc.v2": {
"path": "io.containerd.runc.v2"
},
"io.containerd.runtime.v1.linux": {
"path": "io.containerd.runtime.v1.linux"
},
"runc": {
"path": "runc"
}
},
"DefaultRuntime": "runc",
"InitBinary": "docker-init",
"ContainerdVersion": "9ba4b2555a59f71c4c1c1897e93a540b0c52b883",
"RuncVersion": "v1.1.4-0-g5fd4c4d",
"InitVersion": "de40ad0",
"SecurityOptions": ["apparmor", "seccomp", "cgroupns"],
"KernelVersion": "5.15.0-76-generic",
"OperatingSystem": "Ubuntu 22.04.2 LTS",
"OSType": "linux",
"Architecture": "x86_64",
"NCPU": 2,
"MemTotal": 4123631616,
"Name": "labex-vm",
"ID": "6111111111111111111111111111111111111111111111111111111111111111",
"DockerRootDir": "/var/lib/docker",
"DebugMode": false,
"ExperimentalBuild": false,
"InsecureRegistries": ["127.0.0.0/8"],
"LiveRestoreEnabled": false,
"Warnings": [
"No blkio weight, cpus, or mem limits set, Docker may behave unexpectedly without them."
]
}
}
Formatting the output as JSON is useful when you want to process the information with other tools or scripts.