Reputation: 25533
I want the docker events output to be more readable. Am on windows 10 pro, and on a powershell I run this command.
docker events --format "{{json .}}"
In a different shell when I create a new container,
docker create mcr.microsoft.com/dotnet/core/sdk:3.1
I get some output in json format in the first shell. And that looks something like this.
{"status":"create","id":"7897095c22919bcdcf39612386bebed63296bc33be250445e6069bf4fe90ce37","from":"mcr.microsoft.com/dotnet/core/sdk:3.1","Type":"container","Action":"create","Actor":{"ID":"7897095c22919bcdcf39612386bebed63296bc33be250445e6069bf4fe90ce37","Attributes":{"image":"mcr.microsoft.com/dotnet/core/sdk:3.1","name":"objective_bhaskara"}},"scope":"local","time":1585135301,"timeNano":1585135301351718800}
My question is, is there a better way to format that? What should I do to the command
docker events --format "{{json .}}"
So that it will be formatted in a more readable way. Is there something to pipe that output so that it may look something like the following. I used some online formatter to get to this.
UPDATE
Its now resolved.
As per @Vijay's answer, I first installed jq. The steps are:
Ran power shell as admin.
Ran the command choco install jq
Opened a new command prompt NOT powershell. Somehow power shell did not work.
Issue a command to listen to docker events.
If the output has to be formatted, use the command. Also append jq as follows. Note the double quotes("). Single quotes(') did not work.
docker events --format "{{json .}}" | jq
docker run hello-world
Upvotes: 2
Views: 1438
Reputation: 23788
Using the first answer form Prettify json in powershell 3:
$Json = '{"status":"create","id":"7897095c22919bcdcf39612386bebed63296bc33be250445e6069bf4fe90ce37","from":"mcr.microsoft.com/dotnet/core/sdk:3.1","Type":"container","Action":"create","Actor":{"ID":"7897095c22919bcdcf39612386bebed63296bc33be250445e6069bf4fe90ce37","Attributes":{"image":"mcr.microsoft.com/dotnet/core/sdk:3.1","name":"objective_bhaskara"}},"scope":"local","time":1585135301,"timeNano":1585135301351718800}'
$PrettyJson = $Json | convertfrom-json | convertto-json -depth 100
$PrettyJson
Result:
{
"status": "create",
"id": "7897095c22919bcdcf39612386bebed63296bc33be250445e6069bf4fe90ce37",
"from": "mcr.microsoft.com/dotnet/core/sdk:3.1",
"Type": "container",
"Action": "create",
"Actor": {
"ID": "7897095c22919bcdcf39612386bebed63296bc33be250445e6069bf4fe90ce37",
"Attributes": {
"image": "mcr.microsoft.com/dotnet/core/sdk:3.1",
"name": "objective_bhaskara"
}
},
"scope": "local",
"time": 1585135301,
"timeNano": 1585135301351718800
}
Upvotes: 1
Reputation: 2076
You can just pipe in jq
to your docker events
command.
docker events --format '{{json .}}' | jq
Upvotes: 3