VivekDev
VivekDev

Reputation: 25533

Formatting json output from docker events

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.

docker event output formatted

UPDATE

Its now resolved.

As per @Vijay's answer, I first installed jq. The steps are:

  1. Ran power shell as admin.

  2. Ran the command choco install jq

  3. Opened a new command prompt NOT powershell. Somehow power shell did not work.

  4. Issue a command to listen to docker events.

  5. 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

  1. Open another prompt and run the following command. This can be a powershell if you wish.

docker run hello-world

  1. You should now see formatted json output streaming real time.

Upvotes: 2

Views: 1438

Answers (2)

iRon
iRon

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

vijay v
vijay v

Reputation: 2076

You can just pipe in jq to your docker events command.

docker events --format '{{json .}}' | jq

jq Documentation

Upvotes: 3

Related Questions