Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docker inspect -f json pretty print #110

Closed
1 of 3 tasks
gtseres opened this issue Sep 22, 2017 · 4 comments
Closed
1 of 3 tasks

docker inspect -f json pretty print #110

gtseres opened this issue Sep 22, 2017 · 4 comments

Comments

@gtseres
Copy link

gtseres commented Sep 22, 2017

  • This is a bug report
  • This is a feature request
  • I searched existing issues before opening this one

Expected behavior

When executing docker <object> inspect -f='{{json <parameters>}}' for containers, networks, etc., the json object should be formatted just like in the case where we don't use the -f option; that is, pretty-printed, e.g.:

dev@ubuntu:~$ docker inspect -f '{{json .NetworkSettings.Networks}}' db | python -m json.tool
{
    "my_bridge": {
        "Aliases": [
            "73c0c43994de"
        ],
        "DriverOpts": null,
.....

Actual behavior

The JSON object is not pretty printed:

dev@ubuntu:~$ docker inspect -f '{{json .NetworkSettings.Networks}}' db
{"my_bridge":{"IPAMConfig":null,"Links":null,"Aliases":["73c0c43994de"],"NetworkID":"68988d6de5be2fffcaf5199269ca716539a8914c9afa1826d0bd915e5d4f7a1a","EndpointID":"6c57def51310362025abb554b3a9f7d396f99b47dd1687aa5a6aac61f7905d76","Gateway":"172.18.0.1","IPAddress":"172.18.0.2","IPPrefixLen":16,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":"02:42:ac:12:00:02","DriverOpts":null}}

I could not find any option for that either in the available template functions of inspect, or in the inspect command help:

dev@ubuntu:~$ docker help inspect

Usage:  docker inspect [OPTIONS] NAME|ID [NAME|ID...]

Return low-level information on Docker objects

Options:
  -f, --format string   Format the output using the given Go template
      --help            Print usage
  -s, --size            Display total file sizes if the type is container
      --type string     Return JSON for specified type

Steps to reproduce the behavior

Run docker inspect for an existing container or network, using the -f option:

docker run -itd --name test alpine /bin/sh
docker inspect -f '{{json .NetworkSettings.Networks}}' test
docker network inspect -f='{{json .Containers}}' bridge

Output of docker version:

Client:
 Version:      17.07.0-ce
 API version:  1.31
 Go version:   go1.8.3
 Git commit:   8784753
 Built:        Tue Aug 29 17:42:53 2017
 OS/Arch:      linux/amd64

Server:
 Version:      17.07.0-ce
 API version:  1.31 (minimum version 1.12)
 Go version:   go1.8.3
 Git commit:   8784753
 Built:        Tue Aug 29 17:41:43 2017
 OS/Arch:      linux/amd64
 Experimental: false

Output of docker info:

Containers: 2
 Running: 2
 Paused: 0
 Stopped: 0
Images: 3
Server Version: 17.07.0-ce
Storage Driver: aufs
 Root Dir: /var/lib/docker/aufs
 Backing Filesystem: extfs
 Dirs: 32
 Dirperm1 Supported: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
 Volume: local
 Network: bridge host macvlan null overlay
 Log: awslogs fluentd gcplogs gelf journald json-file logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 3addd840653146c90a254301d6c3a663c7fd6429
runc version: 2d41c047c83e09a6d61d464906feb2a2f3c52aa4
init version: 949e6fa
Security Options:
 apparmor
 seccomp
  Profile: default
Kernel Version: 4.4.0-87-generic
Operating System: Ubuntu 16.04.3 LTS
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 1.953GiB
Name: ubuntu
ID: 53D7:ARYI:42RT:RLB5:PZKK:V46R:MTKW:NHG7:3UIW:YBS7:I772:DMJZ
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Experimental: false
Insecure Registries:
 127.0.0.0/8
Live Restore Enabled: false

WARNING: No swap limit support

Additional environment details (AWS, VirtualBox, physical, etc.)
Running in a Virtualbox Ubuntu 16.04.3 LTS relesae

@SimonHeimberg
Copy link

work around (any hint for myself): pipe the output to a json-pretty-printer

docker inspect -f '{{json .NetworkSettings.Networks}}' db | python -m json.tool # from above
docker inspect -f '{{json .NetworkSettings.Networks}}' db | json_pp
# or any other

I like the current output on one line for docker ls commands (docker stack ls, docker container ls, docker container ps, ...).

@mikzuit
Copy link

mikzuit commented Mar 4, 2020

There is jq to which you can pipe your output. ej. like this below:

docker inspect -f '{{json .NetworkSettings}}' <container_name or id> | jq

or

docker inspect -f '{{json .NetworkSettings}}' <container_name or id> | jq '.Networks'

@thaJeztah
Copy link
Member

This was discussed at some point, and decided not to add in order to not overload the options for --format, and because there's great tools, such as jq mentioned above that allow formatting, colorising, and more manipulation of JSON output.

Closing this for now, but feel free to continue the conversation

@BigBoulard
Copy link

BigBoulard commented Sep 20, 2022

This was discussed at some point, and decided not to add in order to not overload the options for --format, and because there's great tools, such as jq mentioned above that allow formatting, colorising, and more manipulation of JSON output.

Closing this for now, but feel free to continue the conversation

I guess there's other reasons now because I can see that --pretty is available on the docker node inspect command. It's no big deal but would be a plus.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants