CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / Docker / docker inspect

docker inspect

Show the full configuration and state of Docker objects

Intermediatetroubleshootingjsoncontainers

docker inspect prints everything Docker knows about a container, image, network or volume as JSON: state and exit code, OOM kills, health checks, IP addresses, mounts, env vars, limits. With -f and a Go template you pull out exactly the field you need.

Syntaxdocker inspect [OPTIONS] NAME|ID [NAME|ID...]

Watch it run

You want to see, once, everything Docker records about a running container.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 2
docker inspect web | head -24

inspect always returns a JSON array, one element per object. Path plus Args is the process actually started (ENTRYPOINT + CMD). State holds the live facts; the full document runs to about 230 lines, with Config, HostConfig, Mounts and NetworkSettings below.

Anatomy of a command

$ ' '
commanddocker inspect

Return the low-level JSON for one or more objects: containers, images, networks, volumes.

How it works

inspect calls the matching API endpoint, such as GET /containers/<id>/json, and prints the response. For containers dockerd merges the stored config (/var/lib/docker/containers/<id>/config.v2.json and hostconfig.json) with live state: PID, start and finish times, exit code, OOMKilled, health-check history and network settings. Config is what was asked for: image, Cmd, Env, labels. HostConfig is how it runs: ports, mounts, limits, restart policy, log driver. NetworkSettings and Mounts show the result. -f runs the JSON through Go's text/template, with helpers such as json, range, index, join, upper and println. Because Env and labels are stored in plain text, anyone allowed to talk to the docker socket can read every secret passed with -e.

Options you'll actually use

-f, --format TEMPLATEPrint chosen fields with a Go template
{{json .Field}}Print a nested object as JSON (pipe it into jq)
{{range .List}}...{{end}}Loop over a list or map, e.g. .Mounts or networks
{{index .Map "key"}}Read a map key with special characters, like a label
--type TYPEcontainer, image, network, volume, ... when a name is ambiguous
-s, --sizeAdd SizeRw and SizeRootFs for containers

Common mistakes

docker inspect api | grep IPAddress

Matches several fields (and empty ones), and breaks when the container is on more than one network.

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}} {{end}}' api
docker inspect -f '{{.State.status}}' api

Template fields are case-sensitive and must match the JSON keys exactly; this fails with 'can't evaluate field status'.

docker inspect -f '{{.State.Status}}' api
Passing secrets with -e and assuming they're hidden

They're stored in plain text in Config.Env, visible through inspect and on disk.

Use mounted secret files or a secrets manager read at startup

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Print only the exit code and OOMKilled flag of the batch container, separated by a space. What killed it?

  2. 2

    In the sandbox, practise the json + jq pattern: turn ~/data/servers.txt into JSON objects and print only the names of servers that are not running.

  3. 3

    Write the command to print each mount of container db as 'source -> destination', one per line.

Related commands

docker psList containers and their statedocker logsRead a container's stdout and stderrdocker statsLive CPU, memory, network and I/O per containerdocker networkCreate and manage container networkskubectl describeExplain an object's state, conditions and events
Previousdocker rmNextdocker stats
Linux / Docker / docker inspect

docker inspect

Show the full configuration and state of Docker objects

Intermediatetroubleshootingjsoncontainers

docker inspect prints everything Docker knows about a container, image, network or volume as JSON: state and exit code, OOM kills, health checks, IP addresses, mounts, env vars, limits. With -f and a Go template you pull out exactly the field you need.

Syntaxdocker inspect [OPTIONS] NAME|ID [NAME|ID...]

Watch it run

You want to see, once, everything Docker records about a running container.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 2
docker inspect web | head -24

inspect always returns a JSON array, one element per object. Path plus Args is the process actually started (ENTRYPOINT + CMD). State holds the live facts; the full document runs to about 230 lines, with Config, HostConfig, Mounts and NetworkSettings below.

Anatomy of a command

$ ' '
commanddocker inspect

Return the low-level JSON for one or more objects: containers, images, networks, volumes.

How it works

inspect calls the matching API endpoint, such as GET /containers/<id>/json, and prints the response. For containers dockerd merges the stored config (/var/lib/docker/containers/<id>/config.v2.json and hostconfig.json) with live state: PID, start and finish times, exit code, OOMKilled, health-check history and network settings. Config is what was asked for: image, Cmd, Env, labels. HostConfig is how it runs: ports, mounts, limits, restart policy, log driver. NetworkSettings and Mounts show the result. -f runs the JSON through Go's text/template, with helpers such as json, range, index, join, upper and println. Because Env and labels are stored in plain text, anyone allowed to talk to the docker socket can read every secret passed with -e.

Options you'll actually use

-f, --format TEMPLATEPrint chosen fields with a Go template
{{json .Field}}Print a nested object as JSON (pipe it into jq)
{{range .List}}...{{end}}Loop over a list or map, e.g. .Mounts or networks
{{index .Map "key"}}Read a map key with special characters, like a label
--type TYPEcontainer, image, network, volume, ... when a name is ambiguous
-s, --sizeAdd SizeRw and SizeRootFs for containers

Common mistakes

docker inspect api | grep IPAddress

Matches several fields (and empty ones), and breaks when the container is on more than one network.

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}} {{end}}' api
docker inspect -f '{{.State.status}}' api

Template fields are case-sensitive and must match the JSON keys exactly; this fails with 'can't evaluate field status'.

docker inspect -f '{{.State.Status}}' api
Passing secrets with -e and assuming they're hidden

They're stored in plain text in Config.Env, visible through inspect and on disk.

Use mounted secret files or a secrets manager read at startup

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Print only the exit code and OOMKilled flag of the batch container, separated by a space. What killed it?

  2. 2

    In the sandbox, practise the json + jq pattern: turn ~/data/servers.txt into JSON objects and print only the names of servers that are not running.

  3. 3

    Write the command to print each mount of container db as 'source -> destination', one per line.

Related commands

docker psList containers and their statedocker logsRead a container's stdout and stderrdocker statsLive CPU, memory, network and I/O per containerdocker networkCreate and manage container networkskubectl describeExplain an object's state, conditions and events
Previousdocker rmNextdocker stats