docker inspect: find what you need in the JSON
Get single values out of docker inspect with Go templates and jq: IPs, ports, mounts, env, health, log path, and audit one-liners for privileged containers and docker.sock mounts.
What it inspects
docker inspect prints the full configuration and state as a JSON array, one element per object. It works on containers, images, networks and volumes; with an ambiguous name, pick the type.
| Object | Command |
|---|---|
| Container | docker inspect web |
| Several at once | docker inspect web db |
| All running containers | docker inspect $(docker ps -q) |
| Image | docker image inspect nginx:1.27 (or docker inspect --type image nginx) |
| Network | docker network inspect appnet |
| Volume | docker volume inspect pgdata |
Container with disk size (SizeRw, SizeRootFs) | docker inspect -s web |
Where things are
The top-level keys of a container, and what to look for in them:
| Key | Holds |
|---|---|
State | Status, Running, Pid, ExitCode, OOMKilled, StartedAt, Health |
Config | What the image and docker run defined: Image, Env, Cmd, Entrypoint, User, Labels, ExposedPorts, Healthcheck |
HostConfig | How it runs on this host: Binds, PortBindings, RestartPolicy, Privileged, CapAdd, NetworkMode, PidMode, LogConfig, Memory, ReadonlyRootfs, SecurityOpt |
NetworkSettings | Networks (per network: IPAddress, Gateway, MacAddress, Aliases, DNSNames) and Ports (actual bindings) |
Mounts | Every volume and bind mount: Type, Source, Destination, RW |
Name, Id, Image, Created, RestartCount, LogPath | As named. Name starts with / |
Config is the wish, HostConfig and NetworkSettings are what Docker actually set up. Explore an unknown key with docker inspect -f '{{json .HostConfig}}' web | jq.
--format with Go templates
-f / --format takes a Go template that runs once per object. . is the object, keys are case-sensitive.
| Syntax | Does | Example |
|---|---|---|
{{.A.B}} | Value of a nested key | {{.State.Status}} |
{{json .X}} | Print as JSON (for maps and lists) | {{json .Mounts}} |
{{range .List}}...{{end}} | Loop; inside, . is the element | {{range .Mounts}}{{.Source}} {{end}} |
{{range $k, $v := .Map}} | Loop over a map with key and value | {{range $n, $c := .NetworkSettings.Networks}}{{$n}}{{end}} |
{{index .Map "key"}} | Key with dots, dashes or a variable name | {{index .Config.Labels "com.docker.compose.service"}} |
{{index .List 0}} | List element by position | {{index .RepoDigests 0}} |
{{if .X}}...{{else}}...{{end}} | Condition (empty, false, 0, nil are false) | {{if .State.Health}}{{.State.Health.Status}}{{end}} |
{{eq .A "b"}}, {{ne}}, {{and}}, {{or}}, {{not}} | Comparisons | {{if eq .State.Status "exited"}}...{{end}} |
{{join .List ", "}} | Join a string list | {{join .Config.Cmd " "}} |
{{split .S "/"}} | Split a string | {{split .Name "/"}} |
{{lower}}, {{upper}}, {{title}} | Change case | {{upper .Name}} |
{{len .X}} | Length | {{len .Mounts}} |
{{printf "%.12s" .Id}} | printf formatting (here: short ID) | |
{{println}} or {{"\n"}} | Newline, to get one line per element | |
{{.Name}}{{"\t"}} | Tab |
# Health status without failing on containers that have no healthcheck
docker inspect -f '{{if .State.Health}}{{.State.Health.Status}}{{else}}none{{end}}' web
One-liners
| What | Command |
|---|---|
| Status, exit code, OOM killed | docker inspect -f '{{.State.Status}} {{.State.ExitCode}} {{.State.OOMKilled}}' web |
| IP on every network | docker inspect -f '{{range $n, $c := .NetworkSettings.Networks}}{{$n}} {{$c.IPAddress}}{{println}}{{end}}' web |
| IP on one network | docker inspect -f '{{(index .NetworkSettings.Networks "appnet").IPAddress}}' web |
| Published ports | docker inspect -f '{{range $p, $b := .NetworkSettings.Ports}}{{$p}} -> {{range $b}}{{.HostIp}}:{{.HostPort}} {{end}}{{println}}{{end}}' web |
| Mounts | docker inspect -f '{{range .Mounts}}{{.Type}} {{.Source}} -> {{.Destination}} rw={{.RW}}{{println}}{{end}}' web |
| Environment variables | docker inspect -f '{{range .Config.Env}}{{println .}}{{end}}' web |
| Entrypoint and command | docker inspect -f '{{json .Config.Entrypoint}} {{json .Config.Cmd}}' web |
| Image the container runs (ID) | docker inspect -f '{{.Image}}' web |
| Image name it was started with | docker inspect -f '{{.Config.Image}}' web |
| Restart policy and restart count | docker inspect -f '{{.HostConfig.RestartPolicy.Name}} {{.RestartCount}}' web |
| Log driver and log file | docker inspect -f '{{.HostConfig.LogConfig.Type}} {{.LogPath}}' web |
| PID on the host | docker inspect -f '{{.State.Pid}}' web |
| Compose project and service | docker inspect -f '{{index .Config.Labels "com.docker.compose.project"}}/{{index .Config.Labels "com.docker.compose.service"}}' web |
| All labels | docker inspect -f '{{json .Config.Labels}}' web | jq |
| Last healthcheck output | docker inspect -f '{{json .State.Health.Log}}' web | jq -r '.[-1].Output' |
| Image digest, OS and arch | docker image inspect -f '{{index .RepoDigests 0}} {{.Os}}/{{.Architecture}}' nginx:1.27 |
| Image default user (empty = root) | docker image inspect -f '{{.Config.User}}' nginx:1.27 |
| Containers on a network | docker network inspect -f '{{range .Containers}}{{.Name}} {{.IPv4Address}}{{println}}{{end}}' appnet |
| Where a volume lives on disk | docker volume inspect -f '{{.Mountpoint}}' pgdata |
Name, IP and status for every running container:
docker inspect -f '{{.Name}} {{range .NetworkSettings.Networks}}{{.IPAddress}} {{end}}{{.State.Status}}' $(docker ps -q)
With jq
jq is easier for filtering across many containers and for keys you don’t know yet. The output is an array, so start with .[] or .[0]. More on jq: jq cheat sheet.
| What | Command |
|---|---|
| Top-level keys | docker inspect web | jq '.[0] | keys' |
| One subtree | docker inspect web | jq '.[0].HostConfig' |
| Several fields as a small object | docker inspect web | jq '.[0] | {Name, Image: .Config.Image, Status: .State.Status}' |
| Networks and IPs | docker inspect web | jq -r '.[0].NetworkSettings.Networks | to_entries[] | "\(.key) \(.value.IPAddress)"' |
Env as KEY=value lines | docker inspect web | jq -r '.[0].Config.Env[]' |
| Find a key anywhere | docker inspect web | jq 'paths | map(tostring) | join(".")' | grep -i log |
| Table of all running containers | docker inspect $(docker ps -q) | jq -r '.[] | [.Name, .Config.Image, .State.Status] | @tsv' |
| Diff two containers’ config | diff <(docker inspect a | jq -S '.[0].Config') <(docker inspect b | jq -S '.[0].Config') |
Security audit one-liners
Run against all running containers; add -a to docker ps -q to include stopped ones.
| Finds | Command |
|---|---|
| Overview: privileged, network, PID mode, added caps | docker inspect -f '{{.Name}} priv={{.HostConfig.Privileged}} net={{.HostConfig.NetworkMode}} pid={{.HostConfig.PidMode}} caps={{.HostConfig.CapAdd}}' $(docker ps -q) |
| Privileged containers | docker inspect $(docker ps -q) | jq -r '.[] | select(.HostConfig.Privileged) | .Name' |
| Docker socket mounted | docker inspect $(docker ps -q) | jq -r '.[] | select(any(.Mounts[]?; .Source == "/var/run/docker.sock")) | .Name' |
| Host network or host PID namespace | docker inspect $(docker ps -q) | jq -r '.[] | select(.HostConfig.NetworkMode == "host" or .HostConfig.PidMode == "host") | .Name' |
| Sensitive host paths mounted | docker inspect $(docker ps -q) | jq -r '.[] | .Name as $n | .Mounts[]? | select(.Source | test("^/($|etc|root|proc|sys|var/run)")) | "\($n) \(.Source)"' |
| Secrets in environment variables | docker inspect $(docker ps -q) | jq -r '.[] | .Name as $n | .Config.Env[]? | select(test("PASS|SECRET|TOKEN|KEY"; "i")) | "\($n) \(.)"' |
| Running as root (empty user) | docker inspect -f '{{.Name}} user={{.Config.User}}' $(docker ps -q) |
| Ports published on all interfaces | docker ps --format '{{.Names}} {{.Ports}}' | grep -E '0\.0\.0\.0|\[::\]' |
| Writable root filesystem | docker inspect -f '{{.Name}} readonly={{.HostConfig.ReadonlyRootfs}}' $(docker ps -q) |
Environment variables of any container are readable by everyone who can run docker inspect, and they are stored in /var/lib/docker/containers/<id>/config.v2.json.
Filters and --format on list commands
docker ps, images, network ls and volume ls take --filter (repeat for AND) and --format. The fields there are different from inspect: see them with --format '{{json .}}'.
| What | Command |
|---|---|
| Stopped containers | docker ps -a --filter status=exited |
| Crashed (non-zero exit) | docker ps -a --filter exited=1 |
| Unhealthy | docker ps --filter health=unhealthy |
| From one image | docker ps -a --filter ancestor=nginx |
| On one network | docker ps --filter network=backnet |
| Publishing a port | docker ps --filter publish=443 |
| Using a volume | docker ps -a --filter volume=pgdata |
| By label (e.g. one Compose project) | docker ps --filter label=com.docker.compose.project=shop |
| By name (substring) | docker ps --filter name=web |
| Custom table | docker ps --format 'table {{.Names}}\t{{.Image}}\t{{.Status}}\t{{.Ports}}' |
| Size of each container’s writable layer | docker ps -s --format 'table {{.Names}}\t{{.Size}}' |
| Dangling images | docker images --filter dangling=true |
| Images by name pattern | docker images --filter reference='registry.example.com/*' |
| Images sorted by size | docker images --format '{{.Size}}\t{{.Repository}}:{{.Tag}}' | sort -h |
| Unused volumes | docker volume ls --filter dangling=true |
| Custom networks only | docker network ls --filter type=custom |
| Raw fields of each row | docker ps --format '{{json .}}' | jq |
Gotchas
- A missing key is an error, not empty output.
{{.State.Health.Status}}fails on containers without a healthcheck. Guard with{{if .State.Health}}. - Keys with dots or dashes need
index.{{.Config.Labels.com.docker.compose.service}}does not work. - Quote the template in single quotes so the shell doesn’t touch
$nor{{. Inside, strings use double quotes. docker inspecton a name that is both an image and a container returns the container. Use--type imageordocker image inspect.