DCA 12 - Docker Inspect Command

Jarret B

Well-Known Member
Staff member
Joined
May 22, 2017
Messages
336
Reaction score
360
Credits
11,504
Sometimes information is needed from a running service, container, swarm, or node. Information is usually retrieved with the 'inspect' command. We need to cover this in a little detail to help you better use the command.

In some cases, the 'inspect' command may not be useful all the time, you may only need it occasionally.

The 'inspect' Command

The basic syntax of the command is 'docker inspect [OPTIONS] NAME|ID [NAME|ID…]'. We can look at multiple running Docker services, containers, nodes, etc.

We can see what is running with the command 'docker <type> ls'. Here, '<type>' is the type that we want more information about, such as config, container, context, image, manifest, network, node, plugin, secret, service, stack, and volume.

On my system, I have a Swarm of three Nodes. If I run the command 'docker node ls', I can see that there are three Nodes: 'Docker1', 'Docker2', and 'Docker3'.

According to the syntax, I can then run the command 'docker inspect Docker1' to inspect the Node 'Docker1'.

I can run the 'inspect' command on any result from the 'ls' command on any type mentioned above.

If there are multiple types with the same name, such as a Node and a Container, then I need to specify the type. For example, if I had a Node named 'Docker1' and a Container named 'Docker1', then I must specify the type, like 'docker node inspect Docker1' or 'docker container inspect Docker1'.

We can use the parameter '-f' to specify the fields we need. This is more useful than 'grep' if we need multiple objects and any members of the object. We will cover this in a bit. It is easier to do this with examples.

Examples of 'inspect'

On my VirtualBox system running Docker, I have no images downloaded at the current time. I removed all images and have a 'blank' docker system.

I pulled two different images:

docker pull mariadb
docker pull httpd


These two images are for a Database Server and a Web Server.

Let's start the Database Server with the command:

docker run -d --name db -p 3306:3306 mariadb

Since the database will not be connected to a client, I do not need to set Ports, but I will so it shows up in the 'inspect' command.

Next, I need to start the web server with the command:

docker run -d --name web -p 80:80 httpd

I now have two containers running, with unique names so I do not need to specify the type.

Let's look at the inspect command for the database server using the command 'docker inspect db'.

[
{
"Id": "5c28d5c8722ed4ff0e87665d3aabd331daf0dcc442a4f79a3f32a458d0777cf6",
"Created": "2023-09-19T16:36:39.411116781Z",
"Path": "docker-entrypoint.sh",
"Args": [
"-p",
"3306:3306"
],
"State": {
"Status": "exited",
"Running": false,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 0,
"ExitCode": 1,
"Error": "",
"StartedAt": "2023-09-19T16:36:41.777431646Z",
"FinishedAt": "2023-09-19T16:36:42.068815803Z"
},
"Image": "sha256:871a9153c1842e3b480cb1be691c64151bc96a5de2cc8039470d31e32ff0a177",
"ResolvConfPath": "/var/lib/docker/containers/5c28d5c8722ed4ff0e87665d3aabd331daf0dcc442a4f79a3f32a458d0777cf6/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/5c28d5c8722ed4ff0e87665d3aabd331daf0dcc442a4f79a3f32a458d0777cf6/hostname",
"HostsPath": "/var/lib/docker/containers/5c28d5c8722ed4ff0e87665d3aabd331daf0dcc442a4f79a3f32a458d0777cf6/hosts",
"LogPath": "/var/lib/docker/containers/5c28d5c8722ed4ff0e87665d3aabd331daf0dcc442a4f79a3f32a458d0777cf65c28d5c8722ed4ff0e87665d3aabd331daf0dcc442a4f79a3f32a458d077cf6-json.log",
"Name": "/db",
"RestartCount": 0,
"Driver": "devicemapper",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "docker-default",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"ConsoleSize": [
24,
127
],
"CapAdd": null,
"CapDrop": null,
"CgroupnsMode": "private",
"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",
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": [],
"BlkioDeviceReadBps": [],
"BlkioDeviceWriteBps": [],
"BlkioDeviceReadIOps": [],
"BlkioDeviceWriteIOps": [],
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DeviceRequests": null,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": null,
"OomKillDisable": null,
"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": {
"DeviceId": "277",
"DeviceName": "docker-8:3-1179670-8b43bf9883ff12c2bcf463bbb861d549093891c07e2140222e9eae1ed11ee02f",
"DeviceSize": "10737418240"
},
"Name": "devicemapper"
},
"Mounts": [
{
"Type": "volume",
"Name": "b0b6615894aff1126f5fc9a24adba058abbc7c20b9eb8b0871943490dbaf1f28",
"Source": "/var/lib/docker/volumes/b0b6615894aff1126f5fc9a24adba058abbc7c20b9eb8b0871943490dbaf1f28/_data",
"Destination": "/var/lib/mysql",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
],
"Config": {
"Hostname": "5c28d5c8722e",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"3306/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"GOSU_VERSION=1.14",
"LANG=C.UTF-8",
"MARIADB_VERSION=1:11.1.2+maria~ubu2204"
],
"Cmd": [
"-p",
"3306:3306"
],
"Image": "mariadb",
"Volumes": {
"/var/lib/mysql": {}
},
"WorkingDir": "",
"Entrypoint": [
"docker-entrypoint.sh"
],
"OnBuild": null,
"Labels": {
"org.opencontainers.image.authors": "MariaDB Community",
"org.opencontainers.image.base.name": "docker.io/library/ubuntu:jammy",
"org.opencontainers.image.description": "MariaDB Database for relational SQL",
"org.opencontainers.image.documentation": "https://hub.docker.com/_/mariadb/",
"org.opencontainers.image.licenses": "GPL-2.0",
"org.opencontainers.image.ref.name": "ubuntu",
"org.opencontainers.image.source": "https://github.com/MariaDB/mariadb-docker",
"org.opencontainers.image.title": "MariaDB Database",
"org.opencontainers.image.url": "https://github.com/MariaDB/mariadb-docker",
"org.opencontainers.image.vendor": "MariaDB Community",
"org.opencontainers.image.version": "11.1.2"
}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "1c0dceb9482cf69f6e4a2071299a91bde0f25bf947604a21295a3b14bb593ab0",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/1c0dceb9482c",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "",
"Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"MacAddress": "",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "96456039e6b3b42033aa3632583aa889cd89b81ee2b4cf7e3d128cdb7a164c9b",
"EndpointID": "",
"Gateway": "",
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "",
"DriverOpts": null
}
}
}
}
]


If you look at the beginning of the output, you can see an object named 'Id'. The object has no members, or indented objects below it that are considered members of the main object. Looking down a few lines, you'll see the object named 'Args', which has two members below it.

The syntax for the command to retrieve values from a specific member would be:

docker inspect <type> -f "{{.<object>.<member>}}"

You can get the information from multiple types if you want to compare the output.

To list the specific values for a member, let's use the container 'db'. If the object is 'Config' and the member is 'ExposedPorts', you can list the member using a command:

docker inspect db -f "{{.Config.ExposedPorts}}"

The output may be something like 'map[3306/tcp:{}]'. You can see that the 'ExposedPort' is TCP Port 3306.

If I wanted to see the 'ExposedPorts' on my 'db' and 'web' containers, then I could run:

docker inspect db web -f "{{.Config.ExposedPorts}}"

If you wanted to see all the members of 'Config' in 'web', then you could use a command like:

docker inspect web -f "{{.Config}}"

The output is the values of the members. Of course, this does not make it easy to tell what the values represent. So, to the values with the names of the members, add 'json' to the format in the command:

docker inspect web -f "{{json .Config}}"

Each member and value are printed inside a quote, but there is no new line between each member.

I called the following script 'sep.sh' and made it executable. To run the command, you put your docker command in single quotes 'docker inspect web -f "{{json .Config}}"'. The output will be a single line for each double-quoted line in the output.

#! /usr/bin/bash

val=$1
out=$(eval "$val")

for i in ${out//,/ }
do
echo "$i"
done


Conclusion

Getting information about the specific types in Docker can be useful. A lot of times, you may be finding the IP Address of a running node, service, container, etc.

Sometimes, you may need to find other information that is needed. Information about the different types on Docker is found with 'inspect', so be familiar with it.
 


Latest posts

Top