In the previous step, we used the docker version
command to list the Docker Desktop modules. The default output is human-readable, but sometimes you might need the output in a structured format, like JSON, for scripting or further processing.
The docker version
command supports a --format
flag that allows you to specify the output format using Go's text/template package. To get the output in JSON format, you can use the json .
template.
Run the following command in your terminal:
docker version --format '{{json .}}'
This command will output the same information as docker version
, but formatted as a JSON object. The {{json .}}
part is the template that tells Docker to format the entire output (.
) as JSON.
The output will be a single line of JSON data, which might look something like this (formatted for readability):
{
"Client": {
"Version": "20.10.21",
"ApiVersion": "1.41",
"GoVersion": "go1.16.15",
"GitCommit": "f2213a1",
"Built": "Thu Oct 27 00:18:36 2022",
"OsArch": "linux/amd64",
"Context": "default",
"Experimental": true
},
"Server": {
"Engine": {
"Version": "20.10.21",
"ApiVersion": "1.41",
"MinAPIVersion": "1.12",
"GoVersion": "go1.16.15",
"GitCommit": "3056e8c",
"Built": "Thu Oct 27 00:17:23 2022",
"OsArch": "linux/amd64",
"Experimental": false
},
"Containerd": {
"Version": "1.6.8",
"GitCommit": "9cd358bba7fd9c7bb19904ba6d2f58fd60b1ca2b"
},
"Runc": {
"Version": "1.1.4",
"GitCommit": "v1.1.4-0-g5fd4c4d"
},
"DockerInit": {
"Version": "0.19.0",
"GitCommit": "de40ad0"
}
}
}
This JSON output contains the same information as the default output, but in a structured format that is easy for programs to parse. This is particularly useful when you want to extract specific pieces of information from the docker version
output in scripts.