2

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.
VivekDev
  • 20,868
  • 27
  • 132
  • 202
  • 1
    You can use my `Format-Json` function I posted [here](https://stackoverflow.com/a/56324939/9898643) – Theo Mar 25 '20 at 11:38
  • You can add `| ConvertFrom-Json | ConvertTo-Json` in that command as well. [ConvertFrom-Json](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/convertfrom-json?view=powershell-5.1) – stackoverflowusrone Mar 25 '20 at 11:43
  • 1
    Does this answer your question? [Prettify json in powershell 3](https://stackoverflow.com/questions/24789365/prettify-json-in-powershell-3) – iRon Mar 25 '20 at 12:07
  • The output is kinda streaming, so they are not working. No output comes once I use a pipe and then weather I use jq or ConvertFrom-Json etc – VivekDev Mar 25 '20 at 12:11
  • 1
    "*kinda streaming" is not a showstopper, you can stop a stream (and load the full `json` file into memory) by using brackets or assign the stream to a variable. – iRon Mar 25 '20 at 12:31
  • Don't know docker, but if you capture the json output in a variable and pipe that through to [Format-Json](https://stackoverflow.com/a/56324939/9898643) you should get a nice human readable result. `$json | Format-Json` – Theo Mar 25 '20 at 16:38
  • @VivekDev thanks for pointing that its NOT working in Powershell. Issue solved – Sergey Berezovskiy Feb 10 '21 at 13:27

2 Answers2

3

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

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

jq Documentation

vijay v
  • 1,888
  • 1
  • 12
  • 19
  • I think I need to install jq first. Let me check. I am getting jq : The term 'jq' is not recognized as the name of a cmdlet, function, script file, or operable program. – VivekDev Mar 25 '20 at 11:45
  • 1
    yeah... once you do, it will just give you the expected output. – vijay v Mar 25 '20 at 11:46
  • Installed, but no output comes, not sure why – VivekDev Mar 25 '20 at 12:11
  • @VivekDev no output for docker events or for piping jq ? – vijay v Mar 25 '20 at 12:15
  • 1
    Once i pipe it through jq I get no output. So this does not give any output docker events --format '{{json .}}' | jq – VivekDev Mar 25 '20 at 12:25
  • 1
    Are you doing any docker stuff in parallel? – vijay v Mar 25 '20 at 12:31
  • 1
    Two points. Single quote did not work. Only double quote. Also powershell did not work. Only humble command prompt. – VivekDev Mar 29 '20 at 04:15
  • Found this note about Powershell in the docker documentation https://docs.docker.com/config/formatting/. In a Windows shell (for example, PowerShell), you need to use single quotes, but escape the double quotes inside the params as follows: `docker inspect --format '{{join .Args \" , \"}}'`. – Andrew Mar 28 '22 at 07:23
1

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
}
iRon
  • 20,463
  • 10
  • 53
  • 79