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.
7
  • 1
    You can use my Format-Json function I posted here Commented Mar 25, 2020 at 11:38
  • You can add | ConvertFrom-Json | ConvertTo-Json in that command as well. ConvertFrom-Json Commented Mar 25, 2020 at 11:43
  • 1
    Does this answer your question? Prettify json in powershell 3 Commented Mar 25, 2020 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 Commented Mar 25, 2020 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. Commented Mar 25, 2020 at 12:31

2 Answers 2

4

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

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

jq Documentation

Sign up to request clarification or add additional context in comments.

8 Comments

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.
yeah... once you do, it will just give you the expected output.
Once i pipe it through jq I get no output. So this does not give any output docker events --format '{{json .}}' | jq
Are you doing any docker stuff in parallel?
Two points. Single quote did not work. Only double quote. Also powershell did not work. Only humble command prompt.
|
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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.