Inspecting objects with templates
- Version
- 29
- Review
- Draft
Summary#
docker inspect returns detailed JSON for Docker objects. --format applies a Go template to select fields, and --type avoids ambiguity when different kinds of objects share a name.
Syntax#
docker inspect --type container NAME
docker inspect --format '{{.State.Status}}' NAME
docker inspect --format '{{json .Config}}' NAME
docker inspect --format '{{index .Config.Labels "com.example.team"}}' NAME
Example#
The example uses a local alpine:3 image.
(
set -eu
name="dlr-docker-commands-inspect-$$"
trap 'docker rm -f "$name" >/dev/null 2>&1 || true' EXIT
docker create --pull never --name "$name" --env MODE=demo \
--label dlr-docker-commands-inspect.team=search \
alpine:3 sleep 60 >/dev/null
docker inspect --type container \
--format 'status={{.State.Status}} image={{.Config.Image}}' "$name"
docker inspect --format '{{json .Config.Cmd}}' "$name"
docker inspect --format \
'{{index .Config.Labels "dlr-docker-commands-inspect.team"}}' "$name"
docker inspect --format '{{range .Config.Env}}{{println .}}{{end}}' "$name" |
grep '^MODE='
)
Expected output#
status=created image=alpine:3
["sleep","60"]
search
MODE=demo
Common mistakes#
Quote templates with single quotes in a POSIX shell. For a map key containing dots or hyphens, dot notation does not work; use index with the quoted key. Printing a whole container record can reveal secrets stored in its environment.
Version applicability#
Checked with Docker Engine 29. Field paths follow the inspect JSON, which differs between object types. Look at the unformatted output from your daemon before writing a script against a field.
Sources#
- Attribution
- DevLookup contributors
- License
- CC-BY-4.0