Inspecting Docker Objects

docker inspect

docker inspect is a command that low-level information on Docker objects like docker images, containers, networks, volumes, etc. By default, docker inspect will return results in a JSON array.

Syntax

docker docker_object inspect [OPTIONS] NAME|ID [NAME|ID...]

Options

--format: Format the output using the given Go template
--size: Display total file sizes if the type is container
--type: Return JSON for specified type

Example

First list the containers using command -

learning-ocean:~ gaurav$ docker container ls
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS         PORTS                    NAMES
ad9f71ce3a04   ubuntu:14.04   "/bin/bash"              23 minutes ago   Up 8 minutes                      unruffled_newton

Now, to inspect the container use below command and it will give whole the information about the docker container

learning-ocean:~ gaurav$ docker container inspect ad
[
    {
        "Id": "ad9f71ce3a049903157b481789f42eda16d3ce913deea80a1f2e3cf39faeab25",
        "Created": "2021-07-02T03:35:18.4706155Z",
        "Path": "/bin/bash",
        "Args": [],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 6609,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2021-07-02T03:49:22.7894963Z",
            "FinishedAt": "2021-07-02T03:47:57.5307593Z"
        },

        "Image": "sha256:13b66b487594a1f2b75396013bc05d29d9f527852d96c5577cc4f187559875d0",
        "ResolvConfPath": "/var/lib/docker/containers/ad9f71ce3a049903157b481789f42eda16d3ce913deea80a1f2e3cf39faeab25/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/ad9f71ce3a049903157b481789f42eda16d3ce913deea80a1f2e3cf39faeab25/hostname",
        "HostsPath": "/var/lib/docker/containers/ad9f71ce3a049903157b481789f42eda16d3ce913deea80a1f2e3cf39faeab25/hosts",
        "LogPath": "/var/lib/docker/containers/ad9f71ce3a049903157b481789f42eda16d3ce913deea80a1f2e3cf39faeab25/ad9f71ce3a049903157b481789f42eda16d3ce913deea80a1f2e3cf39faeab25-json.log",
        "Name": "/unruffled_newton",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {},
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "CapAdd": null,
            "CapDrop": null,
            "CgroupnsMode": "host",
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "private",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 67108864,
            "Runtime": "runc",
            "ConsoleSize": [
                0,
                0
            ],
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 0,
            "NanoCpus": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": [],
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": [],
            "DeviceCgroupRules": null,
            "DeviceRequests": null,
            "KernelMemory": 0,
            "KernelMemoryTCP": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": null,
            "OomKillDisable": false,
            "PidsLimit": null,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0,
            "MaskedPaths": [
                "/proc/asound",
                "/proc/acpi",
                "/proc/kcore",
                "/proc/keys",
                "/proc/latency_stats",
                "/proc/timer_list",
                "/proc/timer_stats",
                "/proc/sched_debug",
                "/proc/scsi",
                "/sys/firmware"
            ],
            "ReadonlyPaths": [
                "/proc/bus",
                "/proc/fs",
                "/proc/irq",
                "/proc/sys",
                "/proc/sysrq-trigger"
            ]
        },
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/16f8ef67e0ded1275bd61e3843534530eb6abe90fef4e43077d1d3ad8ad6aeae-init/diff:/var/lib/docker/overlay2/215d4933179c167f55efba5c796a9ed71440c9782a66b9e7ff45c446ca4d40be/diff:/var/lib/docker/overlay2/656324b424c4846a04700f4ca35ba836732f1b3e7eaf57ea0e7d58850c4f818e/diff:/var/lib/docker/overlay2/2715fed89f10846d3980684ef7d6d0248c9d1d58a6f7eda7fa8a3789d11bad00/diff",
                "MergedDir": "/var/lib/docker/overlay2/16f8ef67e0ded1275bd61e3843534530eb6abe90fef4e43077d1d3ad8ad6aeae/merged",
                "UpperDir": "/var/lib/docker/overlay2/16f8ef67e0ded1275bd61e3843534530eb6abe90fef4e43077d1d3ad8ad6aeae/diff",
                "WorkDir": "/var/lib/docker/overlay2/16f8ef67e0ded1275bd61e3843534530eb6abe90fef4e43077d1d3ad8ad6aeae/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "ad9f71ce3a04",
            "Domainname": "",
            "User": "",
            "AttachStdin": true,
            "AttachStdout": true,
            "AttachStderr": true,
            "Tty": true,
            "OpenStdin": true,
            "StdinOnce": true,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/bash"
            ],
            "Image": "ubuntu:14.04",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {}
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "db9990af62bd2d8c54f9b2184fc664c6894f18e949bf5da012a24c7226de90ed",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/db9990af62bd",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "3449a29cdbece97ddeb0a99f05c977c13d0acbdf8318e1e3b5bfbc1a4a47ceea",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.3",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "aa:bb:cc:dd:ee:ff",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "24af0d77a35b5589353510b90d277498397959317ef89b7d555432c37fab0ea2",
                    "EndpointID": "3449a29cdbece97ddeb0a99f05c977c13d0acbdf8318e1e3b5bfbc1a4a47ceea",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.3",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:03",
                    "DriverOpts": null
                }
            }
        }
    }
]
learning-ocean:~ gaurav$

To get only specific info about the container, you can run command from CLI passing the --format argument

Example:

Get an instance's log path

docker inspect --format='{{.LogPath}}' \$INSTANCE_ID

you can inspect an image, container and volume using the below command

learning-ocean:~ gaurav$ docker image inspect ubuntu:14.04
learning-ocean:~ gaurav$ docker volume inspect volumeid
learning-ocean:~ gaurav$ docker network inspect networkid