| name | docker-impl-go-templates |
| description | Use when formatting Docker CLI output with --format flags or extracting specific fields from docker inspect. Prevents broken template syntax from missing braces, incorrect field paths, and unquoted json calls. Covers Go template syntax, {{ .Field }}, json, table, range, if/else, and ready-to-use format strings for inspect, ps, images, and stats. Keywords: docker inspect, --format, Go template, json, table, range, docker ps format, format Docker output, get container IP, extract info, custom output.
|
| license | MIT |
| compatibility | Designed for Claude Code. Requires Docker Engine 24+. |
| metadata | {"author":"OpenAEC-Foundation","version":"1.0"} |
docker-impl-go-templates
Quick Reference
Go Template Syntax Basics
| Syntax | Purpose | Example |
|---|
{{ .Field }} | Access top-level field | {{ .State.Status }} |
{{ .Field.SubField }} | Access nested field | {{ .NetworkSettings.IPAddress }} |
{{ json . }} | Full JSON output | docker inspect --format='{{json .}}' |
{{ json .Field }} | JSON for specific field | docker inspect --format='{{json .Config}}' |
table | Table with headers | docker ps --format "table {{.Names}}\t{{.Status}}" |
\t | Tab separator in tables | Used between columns |
{{ println }} | Newline in output | Used inside range loops |
Template Functions
| Function | Purpose | Example |
|---|
json | JSON-encode a value | {{ json .Config.Env }} |
join | Join string slice with separator | {{ join .Config.Cmd " " }} |
upper | Uppercase string | {{ upper .State.Status }} |
lower | Lowercase string | {{ lower .State.Status }} |
title | Title-case string | {{ title .State.Status }} |
split | Split string by separator | {{ split .Image ":" }} |
println | Print with newline | {{ println .Name }} |
index | Access array/map element | {{ index .Config.Env 0 }} |
len | Get length of array/map | {{ len .Config.Env }} |
Critical Warnings
NEVER use double quotes around the entire --format value when it contains Go template double quotes inside -- ALWAYS use single quotes for the outer wrapper on Linux/macOS. On Windows PowerShell, use escaped double quotes or backticks.
NEVER omit {{ end }} when using {{ if }} or {{ range }} -- every conditional and loop block MUST be closed with {{ end }}.
NEVER assume a field exists without checking -- use {{ if .Field }} to guard against nil values that cause template execution errors.
ALWAYS use {{ json .Field }} instead of trying to manually format complex nested objects -- JSON output is reliable and pipeable to jq.
ALWAYS use table prefix with \t separators for readable multi-column output -- omitting table removes column headers.
Conditionals
if / else / end
docker inspect --format='{{if .State.Running}}UP{{else}}DOWN{{end}}' myapp
docker inspect --format='{{if .State.Health}}{{.State.Health.Status}}{{else}}no healthcheck{{end}}' myapp
docker inspect --format='{{if eq .State.Status "running"}}HEALTHY{{else if eq .State.Status "exited"}}STOPPED{{else}}UNKNOWN{{end}}' myapp
Comparison Functions
| Function | Purpose | Example |
|---|
eq | Equal | {{ if eq .State.Status "running" }} |
ne | Not equal | {{ if ne .State.ExitCode 0 }} |
lt | Less than | {{ if lt .State.ExitCode 1 }} |
le | Less or equal | {{ if le .State.Pid 0 }} |
gt | Greater than | {{ if gt .State.ExitCode 0 }} |
ge | Greater or equal | {{ if ge .SizeRw 1000000 }} |
not | Boolean negation | {{ if not .State.Running }} |
and | Logical AND | {{ if and .State.Running .State.Health }} |
or | Logical OR | {{ if or .State.Running .State.Paused }} |
Range Loops
Iterating Over Arrays
docker inspect --format='{{range .Config.Env}}{{println .}}{{end}}' myapp
docker inspect --format='{{range .Mounts}}{{.Source}} -> {{.Destination}}{{println}}{{end}}' myapp
Iterating Over Maps
docker inspect --format='{{range $net, $conf := .NetworkSettings.Networks}}{{$net}}: {{$conf.IPAddress}}{{println}}{{end}}' myapp
docker inspect --format='{{range $port, $bindings := .NetworkSettings.Ports}}{{$port}} -> {{(index $bindings 0).HostPort}}{{println}}{{end}}' myapp
docker inspect --format='{{range $k, $v := .Config.Labels}}{{$k}}={{$v}}{{println}}{{end}}' myapp
Index Function
docker inspect --format='{{index .Config.Cmd 0}}' myapp
docker inspect --format='{{(index (index .NetworkSettings.Ports "80/tcp") 0).HostPort}}' myapp
docker inspect --format='{{index .Config.Labels "com.example.version"}}' myapp
Format Patterns by Command
docker inspect -- Container State (6 patterns)
docker inspect --format='{{.State.Status}}' CONTAINER
docker inspect --format='{{.State.Pid}}' CONTAINER
docker inspect --format='{{.State.StartedAt}}' CONTAINER
docker inspect --format='{{.State.ExitCode}}' CONTAINER
docker inspect --format='{{.State.Running}}' CONTAINER
docker inspect --format='{{if .State.Health}}{{.State.Health.Status}}{{else}}none{{end}}' CONTAINER
docker inspect -- Network Info (6 patterns)
docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' CONTAINER
docker inspect --format='{{range .NetworkSettings.Networks}}{{.MacAddress}}{{end}}' CONTAINER
docker inspect --format='{{range $k,$v := .NetworkSettings.Networks}}{{$k}}={{$v.IPAddress}} {{end}}' CONTAINER
docker inspect --format='{{range .NetworkSettings.Networks}}{{.Gateway}}{{end}}' CONTAINER
docker inspect --format='{{range $p,$conf := .NetworkSettings.Ports}}{{$p}}->{{(index $conf 0).HostPort}} {{end}}' CONTAINER
docker inspect --format='{{(index (index .NetworkSettings.Ports "80/tcp") 0).HostPort}}' CONTAINER
docker inspect -- Configuration (6 patterns)
docker inspect --format='{{.Config.Image}}' CONTAINER
docker inspect --format='{{json .Config.Entrypoint}}' CONTAINER
docker inspect --format='{{json .Config.Cmd}}' CONTAINER
docker inspect --format='{{range .Config.Env}}{{println .}}{{end}}' CONTAINER
docker inspect --format='{{json .Config.Labels}}' CONTAINER
docker inspect --format='{{index .Config.Labels "com.example.version"}}' CONTAINER
docker inspect -- Mounts & Size (4 patterns)
docker inspect --format='{{json .Mounts}}' CONTAINER
docker inspect --format='{{range .Mounts}}{{.Source}} -> {{.Destination}}{{println}}{{end}}' CONTAINER
docker inspect --format='{{.LogPath}}' CONTAINER
docker inspect --size --format='{{.SizeRootFs}}' CONTAINER
docker ps (4 patterns)
docker ps --format "table {{.Names}}\t{{.Image}}\t{{.Status}}\t{{.Ports}}"
docker ps --format "{{.Names}}"
docker ps --format "table {{.Names}}\t{{.Label \"app\"}}\t{{.Status}}"
docker ps --format json
docker images (3 patterns)
docker images --format "table {{.Repository}}\t{{.Tag}}\t{{.Size}}"
docker images --format "{{.Repository}}:{{.Tag}}"
docker images --digests --format "table {{.Repository}}\t{{.Tag}}\t{{.Digest}}"
docker stats / network / volume / system (5 patterns)
docker stats --no-stream --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.NetIO}}"
docker network ls --format "table {{.Name}}\t{{.Driver}}\t{{.Scope}}"
docker volume ls --format "table {{.Name}}\t{{.Driver}}\t{{.Mountpoint}}"
docker system df --format "table {{.Type}}\t{{.TotalCount}}\t{{.Size}}\t{{.Reclaimable}}"
docker info --format '{{.ServerVersion}}'
Scripting Patterns
Extract Values for Shell Scripts
CID=$(docker ps -qf "name=myapp")
IP=$(docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' myapp)
PORT=$(docker inspect --format='{{(index (index .NetworkSettings.Ports "80/tcp") 0).HostPort}}' myapp)
docker ps --format "{{.Names}}" | while read name; do
echo "Container: $name"
done
docker ps -a --filter status=exited --format "{{.Names}}: exit {{.Status}}"
Bulk Operations with Format Output
docker network inspect --format='{{range .Containers}}{{.Name}} {{end}}' mynet | xargs docker stop
docker images myrepo --format "{{.ID}}" | xargs docker rmi
docker ps -q | xargs -I {} docker inspect --format='{{.Name}}: {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' {}
Decision Tree: Choosing Output Format
Need Docker command output?
├── Need full object details?
│ └── Use: docker inspect --format='{{json .}}' | jq
├── Need specific single field?
│ └── Use: --format='{{.Field.SubField}}'
├── Need readable multi-column table?
│ └── Use: --format "table {{.Col1}}\t{{.Col2}}"
├── Need machine-parseable output?
│ └── Use: --format json OR --format='{{json .Field}}'
├── Need to iterate nested data?
│ └── Use: --format='{{range .Items}}{{.Field}}{{println}}{{end}}'
└── Need conditional output?
└── Use: --format='{{if .Cond}}yes{{else}}no{{end}}'
Reference Links
Official Sources