VivekDev
VivekDev

Reputation: 25533

Is there a better way to format the docker output?

I feel there should be a better way here.

I run the following command to see the layers of an image(nginx in this case)

docker inspect nginx:latest --format "{{.RootFS.Layers}}"

And the output I get looks like this.

[sha256:f2cb0ecef392f2a630fa1205b874ab2e2aedf96de04d0b8838e4e728e28142da sha256:71f2244bc14dacf7f73128b4b89b1318f41a9421dffc008c2ba91bb6dc2716f1 sha256:55a77731ed2630d9c092258490b03be3491d5f245fe13a1c6cb4e21babfb15b7]

Of course that's an array. That looks worse in the command prompt without appropriate wrapping.

Can that be formatted better?

Tried the following variations (all that I knew) but did not help :(

docker inspect nginx:latest --format "{{json .RootFS.Layers}}"  // json
docker inspect nginx:latest --format "table {{.RootFS.Layers}}" // table with double quote
docker inspect nginx:latest --format 'table {{.RootFS.Layers}}' // table with single quote

Upvotes: 5

Views: 3372

Answers (2)

Marcelo Barros
Marcelo Barros

Reputation: 1048

From this comment on GitHub (credits on SimonHeimberg), I'd suggest using Python for this task. It also works on Mac.

docker inspect nginx:latest --format "{{json .RootFS.Layers}}" | python -m json.tool

Upvotes: 1

Yasen
Yasen

Reputation: 4504

jq for pretty print any json output

jq is a lightweight and flexible and powerful command-line JSON processor.

Try docker inspect nginx:latest | jq -r '.RootFS.Layers'

Format command and log output | Docker Documentation

join for pretty print with bare docker

As per join manual:

join concatenates a list of strings to create a single string. It puts a separator between each element in the list.

docker inspect --format '{{join .Args " , "}}' container

So, thx @char. Hi suggests this:

docker inspect --format '{{join .RootFS.Layers "\n"}}'

Upvotes: 2

Related Questions