Inspecting a running container

Other topics

Get container information

To get all the information for a container you can run:

docker inspect <container>

Get specific information from a container

You can get an specific information from a container by running:

docker inspect -f '<format>' <container>

For instance, you can get the Network Settings by running:

docker inspect -f '{{ .NetworkSettings }}' <container>

You can also get just the IP address:

docker inspect -f '{{ .NetworkSettings.IPAddress }}' <container>

The parameter -f means format and will receive a Go Template as input to format what is expected, but this won’t bring a beautiful return, so try:

docker inspect -f '{{ json .NetworkSettings }}' {{containerIdOrName}}

the json keyword will bring the return as a JSON.

So to finish, a little tip is to use python in there to format the output JSON:

docker inspect -f '{{ json .NetworkSettings }}' <container> | python -mjson.tool

And voila, you can query anything on the docker inspect and make it look pretty in your terminal.

It's also possible to use a utility called "jq" in order to help process docker inspect command output.

docker inspect -f '{{ json .NetworkSettings }}' aa1 | jq [.Gateway]

The above command will return the following output:

[
  "172.17.0.1"
]

This output is actually a list containing one element. Sometimes, docker inspect displays a list of several elements, and you may want to refer to a specific element. For example, if Config.Env contains several elements, you can refer to the first element of this list using index:

docker inspect --format '{{ index (index .Config.Env) 0 }}' <container>

The first element is indexed at zero, which means that the second element of this list is at index 1:

docker inspect --format '{{ index (index .Config.Env) 1 }}' <container>

Using len it is possible to get the number of elements of the list:

docker inspect --format ‘{{ len .Config.Env }}’ <container>

And using negative numbers, it's possible to refer to the last element of the list:

docker inspect –format “{{ index .Config.Cmd $[$(docker inspect –format ‘{{ len .Config.Cmd }}’ <container>)-1]}}” <container>

Some docker inspect information comes as a dictionary of key:value, here is an extract of a docker inspectof a jess/spotify running container

"Config": { "Hostname": "8255f4804dde", "Domainname": "", "User": "spotify", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": [ "DISPLAY=unix:0", "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "HOME=/home/spotify" ], "Cmd": [ "-stylesheet=/home/spotify/spotify-override.css" ], "Image": "jess/spotify", "Volumes": null, "WorkingDir": "/home/spotify", "Entrypoint": [ "spotify" ], "OnBuild": null, "Labels": {} },

so I an get the values of the whole Config section

docker inspect -f '{{.Config}}' 825

{8255f4804dde spotify false false false map[] false false false [DISPLAY=unix:0 PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin HOME=/home/spotify] [-stylesheet=/home/spotify/spotify-override.css] false jess/spotify map[] /home/spotify [spotify] false [] map[] }

but also a single field, like the value of Config.Image

docker inspect -f '{{index (.Config) "Image" }}' 825

jess/spotify

or Config.Cmd

docker inspect -f '{{.Config.Cmd}}' 825

[-stylesheet=/home/spotify/spotify-override.css]

Inspect an image

In order to inspect an image, you can use the image ID or the image name, consisting of repository and tag. Say, you have the CentOS 6 base image:

➜  ~ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              centos6             cf2c3ece5e41        2 weeks ago         194.6 MB

In this case you can run either of the following:

  • ➜ ~ docker inspect cf2c3ece5e41
  • ➜ ~ docker inspect centos:centos6

Both of these command will give you all information available in a JSON array:

[
    {
        "Id": "sha256:cf2c3ece5e418fd063bfad5e7e8d083182195152f90aac3a5ca4dbfbf6a1fc2a",
        "RepoTags": [
            "centos:centos6"
        ],
        "RepoDigests": [],
        "Parent": "",
        "Comment": "",
        "Created": "2016-07-01T22:34:39.970264448Z",
        "Container": "b355fe9a01a8f95072e4406763138c5ad9ca0a50dbb0ce07387ba905817d6702",
        "ContainerConfig": {
            "Hostname": "68a1f3cfce80",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) CMD [\"/bin/bash\"]"
            ],
            "Image": "sha256:cdbcc7980b002dc19b4d5b6ac450993c478927f673339b4e6893647fe2158fa7",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "build-date": "20160701",
                "license": "GPLv2",
                "name": "CentOS Base Image",
                "vendor": "CentOS"
            }
        },
        "DockerVersion": "1.10.3",
        "Author": "https://github.com/CentOS/sig-cloud-instance-images",
        "Config": {
            "Hostname": "68a1f3cfce80",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/bash"
            ],
            "Image": "sha256:cdbcc7980b002dc19b4d5b6ac450993c478927f673339b4e6893647fe2158fa7",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "build-date": "20160701",
                "license": "GPLv2",
                "name": "CentOS Base Image",
                "vendor": "CentOS"
            }
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 194606575,
        "VirtualSize": 194606575,
        "GraphDriver": {
            "Name": "aufs",
            "Data": null
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:2714f4a6cdee9d4c987fef019608a4f61f1cda7ccf423aeb8d7d89f745c58b18"
            ]
        }
    }
]

Printing specific informations

docker inspect supports Go Templates via the --format option. This allows for better integration in scripts, without resorting to pipes/sed/grep traditional tools.

Print a container internal IP:

docker inspect --format '{{ .NetworkSettings.IPAddress }}' 7786807d8084

This is useful for direct network access of load-balancers auto-configuration.

Print a container init PID:

docker inspect --format '{{ .State.Pid }}' 7786807d8084

This is useful for deeper inspection via /proc or tools like strace.

Advanced formating:

docker inspect --format 'Container {{ .Name }} listens on {{ .NetworkSettings.IPAddress }}:{{ range $index, $elem := .Config.ExposedPorts }}{{ $index }}{{ end }}' 5765847de886 7786807d8084

Will output:

Container /redis listens on 172.17.0.3:6379/tcp
Container /api listens on 172.17.0.2:4000/tcp

Debugging the container logs using docker inspect

docker inspect command can be used to debug the container logs.

The stdout and stderr of container can be checked to debug the container, whose location can be obtained using docker inspect.

Command : docker inspect <container-id> | grep Source

It gives the location of containers stdout and stderr.

Examining stdout/stderr of a running container

docker logs --follow <containerid>

This tails the output of the running container. This is useful if you did not set up a logging driver on the docker daemon.

Syntax:

  • docker inspect [OPTIONS] CONTAINER|IMAGE [CONTAINER|IMAGE...]

Contributors

Topic Id: 1336

Example Ids: 4355,4356,5489,5649,21942,22768

This site is not affiliated with any of the contributors.