
Docker Impl Go Templates
- 9 installs
- 9 repo stars
- Updated July 8, 2026
- openaec-foundation/docker-claude-skill-package
Helps with devops & ci/cd tasks.
About
docker-impl-go-templates is a Claude Code skill for devops & ci/cd. It helps solo builders move faster with AI-assisted development.
- docker-impl-go-templates
- DevOps & CI/CD
- AI-coding skill
Docker Impl Go Templates by the numbers
- 9 all-time installs (skills.sh)
- +1 installs in the week ending Aug 4, 2026 (Skillselion tracking)
- Ranked #1,020 of 1,435 DevOps & CI/CD skills by installs in the Skillselion catalog
- Data as of Aug 4, 2026 (Skillselion catalog sync)
npx skills add https://github.com/openaec-foundation/docker-claude-skill-package --skill docker-impl-go-templatesAdd your badge
Show developers this skill is listed on Skillselion. Paste this into your README.
| Installs | 9 |
|---|---|
| repo stars | ★ 9 |
| Last updated | July 8, 2026 |
| Repository | openaec-foundation/docker-claude-skill-package ↗ |
What it does
Helps with devops & ci/cd tasks.
Files
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
# Simple conditional
docker inspect --format='{{if .State.Running}}UP{{else}}DOWN{{end}}' myapp
# Check for empty value
docker inspect --format='{{if .State.Health}}{{.State.Health.Status}}{{else}}no healthcheck{{end}}' myapp
# Nested conditional
docker inspect --format='{{if eq .State.Status "running"}}HEALTHY{{else if eq .State.Status "exited"}}STOPPED{{else}}UNKNOWN{{end}}' myappComparison 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
# Environment variables (one per line)
docker inspect --format='{{range .Config.Env}}{{println .}}{{end}}' myapp
# Mounts with source and destination
docker inspect --format='{{range .Mounts}}{{.Source}} -> {{.Destination}}{{println}}{{end}}' myappIterating Over Maps
# Network names and IPs
docker inspect --format='{{range $net, $conf := .NetworkSettings.Networks}}{{$net}}: {{$conf.IPAddress}}{{println}}{{end}}' myapp
# Port bindings
docker inspect --format='{{range $port, $bindings := .NetworkSettings.Ports}}{{$port}} -> {{(index $bindings 0).HostPort}}{{println}}{{end}}' myapp
# All labels
docker inspect --format='{{range $k, $v := .Config.Labels}}{{$k}}={{$v}}{{println}}{{end}}' myappIndex Function
# First element of an array
docker inspect --format='{{index .Config.Cmd 0}}' myapp
# Specific port binding
docker inspect --format='{{(index (index .NetworkSettings.Ports "80/tcp") 0).HostPort}}' myapp
# Label by key (with dot in name)
docker inspect --format='{{index .Config.Labels "com.example.version"}}' myapp---
Format Patterns by Command
docker inspect -- Container State (6 patterns)
# 1. Container status
docker inspect --format='{{.State.Status}}' CONTAINER
# 2. Container PID
docker inspect --format='{{.State.Pid}}' CONTAINER
# 3. Container start time
docker inspect --format='{{.State.StartedAt}}' CONTAINER
# 4. Container exit code
docker inspect --format='{{.State.ExitCode}}' CONTAINER
# 5. Running boolean
docker inspect --format='{{.State.Running}}' CONTAINER
# 6. Health check status
docker inspect --format='{{if .State.Health}}{{.State.Health.Status}}{{else}}none{{end}}' CONTAINERdocker inspect -- Network Info (6 patterns)
# 7. IP address (first network)
docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' CONTAINER
# 8. MAC address
docker inspect --format='{{range .NetworkSettings.Networks}}{{.MacAddress}}{{end}}' CONTAINER
# 9. All networks with IPs
docker inspect --format='{{range $k,$v := .NetworkSettings.Networks}}{{$k}}={{$v.IPAddress}} {{end}}' CONTAINER
# 10. Gateway
docker inspect --format='{{range .NetworkSettings.Networks}}{{.Gateway}}{{end}}' CONTAINER
# 11. Port bindings (all)
docker inspect --format='{{range $p,$conf := .NetworkSettings.Ports}}{{$p}}->{{(index $conf 0).HostPort}} {{end}}' CONTAINER
# 12. Specific port mapping
docker inspect --format='{{(index (index .NetworkSettings.Ports "80/tcp") 0).HostPort}}' CONTAINERdocker inspect -- Configuration (6 patterns)
# 13. Image name
docker inspect --format='{{.Config.Image}}' CONTAINER
# 14. Entrypoint
docker inspect --format='{{json .Config.Entrypoint}}' CONTAINER
# 15. Command
docker inspect --format='{{json .Config.Cmd}}' CONTAINER
# 16. Environment variables
docker inspect --format='{{range .Config.Env}}{{println .}}{{end}}' CONTAINER
# 17. All labels as JSON
docker inspect --format='{{json .Config.Labels}}' CONTAINER
# 18. Specific label
docker inspect --format='{{index .Config.Labels "com.example.version"}}' CONTAINERdocker inspect -- Mounts & Size (4 patterns)
# 19. All mounts as JSON
docker inspect --format='{{json .Mounts}}' CONTAINER
# 20. Mount sources and destinations
docker inspect --format='{{range .Mounts}}{{.Source}} -> {{.Destination}}{{println}}{{end}}' CONTAINER
# 21. Log file path
docker inspect --format='{{.LogPath}}' CONTAINER
# 22. Root filesystem size (requires -s flag)
docker inspect --size --format='{{.SizeRootFs}}' CONTAINERdocker ps (4 patterns)
# 23. Compact table
docker ps --format "table {{.Names}}\t{{.Image}}\t{{.Status}}\t{{.Ports}}"
# 24. Names only
docker ps --format "{{.Names}}"
# 25. With specific label
docker ps --format "table {{.Names}}\t{{.Label \"app\"}}\t{{.Status}}"
# 26. JSON output (one object per line)
docker ps --format jsondocker images (3 patterns)
# 27. Compact image list
docker images --format "table {{.Repository}}\t{{.Tag}}\t{{.Size}}"
# 28. Repository:tag pairs
docker images --format "{{.Repository}}:{{.Tag}}"
# 29. With digest
docker images --digests --format "table {{.Repository}}\t{{.Tag}}\t{{.Digest}}"docker stats / network / volume / system (5 patterns)
# 30. Stats with custom columns
docker stats --no-stream --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.NetIO}}"
# 31. Network listing
docker network ls --format "table {{.Name}}\t{{.Driver}}\t{{.Scope}}"
# 32. Volume listing
docker volume ls --format "table {{.Name}}\t{{.Driver}}\t{{.Mountpoint}}"
# 33. System disk usage
docker system df --format "table {{.Type}}\t{{.TotalCount}}\t{{.Size}}\t{{.Reclaimable}}"
# 34. Docker info field
docker info --format '{{.ServerVersion}}'---
Scripting Patterns
Extract Values for Shell Scripts
# Get container ID by name
CID=$(docker ps -qf "name=myapp")
# Get IP address into variable
IP=$(docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' myapp)
# Get host port for container port 80
PORT=$(docker inspect --format='{{(index (index .NetworkSettings.Ports "80/tcp") 0).HostPort}}' myapp)
# Get all running container names
docker ps --format "{{.Names}}" | while read name; do
echo "Container: $name"
done
# Get exit codes for all stopped containers
docker ps -a --filter status=exited --format "{{.Names}}: exit {{.Status}}"Bulk Operations with Format Output
# Stop all containers on a specific network
docker network inspect --format='{{range .Containers}}{{.Name}} {{end}}' mynet | xargs docker stop
# Remove all images from a specific repository
docker images myrepo --format "{{.ID}}" | xargs docker rmi
# Get IPs of all running containers
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
- references/patterns.md -- 30+ format patterns organized by Docker command
- references/examples.md -- Complex template examples with conditionals, range loops, and scripting
- references/anti-patterns.md -- Common Go template mistakes and how to fix them
Official Sources
- https://docs.docker.com/reference/cli/docker/inspect/
- https://docs.docker.com/reference/cli/docker/container/ls/
- https://docs.docker.com/reference/cli/docker/image/ls/
- https://pkg.go.dev/text/template
Go Template Anti-Patterns
Common mistakes when using Docker --format flags. Each anti-pattern includes the error,WHY it fails, and the CORRECT alternative. Verified against Docker Engine 24+.
---
AP-01: Missing {{ end }} Closure
# WRONG -- missing {{ end }} causes parse error
docker inspect --format='{{if .State.Running}}UP' myapp
# Error: template: :1: unexpected EOF
# CORRECT -- ALWAYS close if/range blocks
docker inspect --format='{{if .State.Running}}UP{{end}}' myappWHY: Go templates require explicit block closure. Every {{ if }} and {{ range }} MUST have a matching {{ end }}.
---
AP-02: Quoting Conflict on Linux/macOS
# WRONG -- double quotes conflict with Go template syntax
docker inspect --format="{{.State.Status}}" myapp
# May work on some shells but breaks with nested quotes
# CORRECT -- ALWAYS use single quotes on Linux/macOS
docker inspect --format='{{.State.Status}}' myappWHY: The shell interprets double-quoted strings before Docker sees them. Backticks, dollar signs, and braces inside double quotes can trigger shell expansion. Single quotes pass the template verbatim.
---
AP-03: Accessing Nil/Missing Fields Without Guard
# WRONG -- crashes if no healthcheck is configured
docker inspect --format='{{.State.Health.Status}}' myapp
# Error: template: :1:... executing "..." at <.State.Health.Status>: nil pointer
# CORRECT -- guard with if
docker inspect --format='{{if .State.Health}}{{.State.Health.Status}}{{else}}none{{end}}' myappWHY: Not all containers have a healthcheck configured. .State.Health is nil when no HEALTHCHECK instruction exists. Accessing a field on a nil pointer causes a template execution error.
---
AP-04: Using Dot Notation for Labels with Dots
# WRONG -- Go interprets dots as field separators
docker inspect --format='{{.Config.Labels.com.docker.compose.project}}' myapp
# Error: template: :1:... can't evaluate field com in type ...
# CORRECT -- use index function for dotted key names
docker inspect --format='{{index .Config.Labels "com.docker.compose.project"}}' myappWHY: Go template dot notation (.field.subfield) treats each segment as a struct field. Label keys containing dots are map keys, not nested structs. ALWAYS use index to access map keys that contain dots.
---
AP-05: Forgetting table Keyword for Headers
# WRONG -- no headers, hard to read
docker ps --format "{{.Names}}\t{{.Status}}\t{{.Ports}}"
# Output:
# web Up 2 hours 80/tcp
# db Up 2 hours 5432/tcp
# CORRECT -- table prefix adds column headers
docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"
# Output:
# NAMES STATUS PORTS
# web Up 2 hours 80/tcp
# db Up 2 hours 5432/tcpWHY: The table keyword is a Docker-specific extension (not standard Go templates). Without it, you get raw values only. ALWAYS use table when output is for human reading; omit it only for machine parsing.
---
AP-06: Using \t Without table for Alignment
# WRONG -- \t without table produces inconsistent alignment
docker ps --format "{{.Names}}\t{{.Image}}\t{{.Status}}"
# Output may not align because tab stops depend on terminal
# CORRECT for humans -- use table keyword
docker ps --format "table {{.Names}}\t{{.Image}}\t{{.Status}}"
# CORRECT for scripting -- use a fixed separator
docker ps --format "{{.Names}}|{{.Image}}|{{.Status}}"WHY: Tab characters without the table directive rely on terminal tab stop settings. Different terminals and pipes handle tabs differently. For reliable alignment, ALWAYS use table. For machine parsing, use a deterministic separator like | or use --format json.
---
AP-07: Range Without Variable Assignment on Maps
# WRONG -- cannot access both key and value
docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' myapp
# Works but you lose the network name
# CORRECT -- assign key and value variables
docker inspect --format='{{range $name, $conf := .NetworkSettings.Networks}}{{$name}}: {{$conf.IPAddress}}{{println}}{{end}}' myappWHY: When iterating over maps, the simple {{ range }} form only gives you the value. To access both key and value, ALWAYS use the $key, $value := .Map assignment form.
---
AP-08: Assuming Port Bindings Always Exist
# WRONG -- crashes when port has no host binding
docker inspect --format='{{(index (index .NetworkSettings.Ports "80/tcp") 0).HostPort}}' myapp
# Error: index out of range when port is exposed but not published
# CORRECT -- guard against nil binding list
docker inspect --format='{{with index .NetworkSettings.Ports "80/tcp"}}{{(index . 0).HostPort}}{{else}}not published{{end}}' myapp
# ALSO CORRECT -- use if
docker inspect --format='{{if index .NetworkSettings.Ports "80/tcp"}}{{(index (index .NetworkSettings.Ports "80/tcp") 0).HostPort}}{{else}}not published{{end}}' myappWHY: A port can be EXPOSE'd in the Dockerfile without being published (-p). The binding list is nil or empty for unexposed ports. ALWAYS check before indexing into port binding arrays.
---
AP-09: Using json on Entire Inspect Without jq
# WRONG -- unreadable wall of JSON
docker inspect --format='{{json .}}' myapp
# Outputs thousands of characters on one line
# CORRECT -- pipe to jq for readability
docker inspect --format='{{json .}}' myapp | jq .
# BETTER -- target specific sections
docker inspect --format='{{json .State}}' myapp | jq .
docker inspect --format='{{json .NetworkSettings}}' myapp | jq .WHY: {{ json . }} dumps the entire object as a single unformatted line. ALWAYS pipe to jq for human-readable output. Better yet, target a specific sub-object to reduce noise.
---
AP-10: Mixing Go Template and jq in --format
# WRONG -- jq syntax inside Go template
docker inspect --format='{{.Config.Labels | keys}}' myapp
# Error: function "keys" not defined
# CORRECT -- use json then pipe to jq
docker inspect --format='{{json .Config.Labels}}' myapp | jq 'keys'WHY: Go templates and jq are separate languages. Go templates have a limited set of built-in functions (json, join, split, upper, lower, title, println, index, len). For complex JSON transformations, ALWAYS output JSON from Docker and process with jq.
---
AP-11: Forgetting println in Range Loops
# WRONG -- all values concatenated on one line
docker inspect --format='{{range .Config.Env}}{{.}}{{end}}' myapp
# Output: PATH=/usr/binHOME=/rootTERM=xterm
# CORRECT -- use println for line breaks
docker inspect --format='{{range .Config.Env}}{{println .}}{{end}}' myapp
# Output:
# PATH=/usr/bin
# HOME=/root
# TERM=xtermWHY: Go templates do not insert newlines between range iterations. ALWAYS use {{ println . }} or {{ println }} at the end of range blocks to separate items.
---
AP-12: Using --format with docker inspect on Wrong Object Type
# WRONG -- using container fields on an image
docker inspect --format='{{.State.Status}}' nginx:latest
# Error: template: :1:... can't evaluate field State in type...
# CORRECT -- use image-specific fields
docker inspect --format='{{.Config.Cmd}}' nginx:latest
docker inspect --format='{{json .Config.ExposedPorts}}' nginx:latest
# CORRECT -- restrict type explicitly
docker inspect --type image --format='{{.RepoTags}}' nginx:latest
docker inspect --type container --format='{{.State.Status}}' myappWHY: docker inspect auto-detects object type, but container-specific fields (.State, .NetworkSettings, .HostConfig) do not exist on images. ALWAYS use --type when the object type is ambiguous, or know which fields belong to which object type.
---
AP-13: Hardcoding Container Names in Scripts
# WRONG -- breaks when container name changes
IP=$(docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' my-hardcoded-name)
# CORRECT -- use variables or docker ps filters
CONTAINER=$(docker ps -qf "label=app=web" | head -1)
IP=$(docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "$CONTAINER")
# ALSO CORRECT -- use Compose service names
IP=$(docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "$(docker compose ps -q web)")WHY: Container names may change between deployments. ALWAYS use labels, filters, or Compose service lookups to find containers dynamically in scripts.
---
AP-14: Not Using --no-stream with docker stats in Scripts
# WRONG -- hangs forever in script (stats is a live stream)
CPU=$(docker stats --format "{{.CPUPerc}}" myapp)
# CORRECT -- use --no-stream for single snapshot
CPU=$(docker stats --no-stream --format "{{.CPUPerc}}" myapp)WHY: docker stats continuously streams live data by default. In a script, this blocks indefinitely. ALWAYS use --no-stream when capturing stats output in variables or pipelines.
---
AP-15: Ignoring Platform Differences in Quoting
# Linux/macOS -- use single quotes
docker inspect --format='{{.State.Status}}' myapp
# Windows CMD -- use double quotes
docker inspect --format="{{.State.Status}}" myapp
# Windows PowerShell -- single quotes usually work
docker inspect --format='{{.State.Status}}' myapp
# WRONG -- escaping that works on one platform but not another
docker inspect --format=\"{{.State.Status}}\" myappWHY: Shell quoting rules differ across platforms. Single quotes on Linux/macOS prevent all shell interpolation. Windows CMD requires double quotes. PowerShell has its own rules. ALWAYS test format strings on your target platform.
---
Summary Table
| Anti-Pattern | Risk | Fix |
|---|---|---|
AP-01: Missing {{ end }} | Template parse error | ALWAYS close if/range blocks |
| AP-02: Double-quote wrapper | Shell expansion corrupts template | Use single quotes on Linux/macOS |
| AP-03: Nil field access | Template execution panic | Guard with {{ if }} |
| AP-04: Dots in label keys | Field resolution error | Use index function |
AP-05: No table keyword | Missing headers | Add table prefix |
AP-06: \t without table | Misaligned output | Use table or fixed separator |
| AP-07: Range without key var | Lost map keys | Use $k, $v := .Map form |
| AP-08: Unguarded port index | Index out of range panic | Check binding exists first |
| AP-09: Full JSON without jq | Unreadable output | Pipe to jq, target sub-objects |
| AP-10: jq syntax in template | Undefined function error | Separate Go template from jq |
| AP-11: No println in range | Concatenated output | Add {{ println }} |
| AP-12: Wrong object type | Field not found error | Use --type flag |
| AP-13: Hardcoded names | Brittle scripts | Use labels/filters |
| AP-14: No --no-stream | Script hangs | ALWAYS use --no-stream in scripts |
| AP-15: Platform quoting | Cross-platform failures | Test on target platform |
Go Template Complex Examples
Advanced Go template patterns for Docker CLI. Each example is verified against Docker Engine 24+.
ALWAYS test complex templates on a single container before using in scripts.
---
Conditional Output Examples
Container Status Dashboard
# Color-coded status (for terminal output)
docker inspect --format='{{.Name}}: {{if eq .State.Status "running"}}RUNNING{{else if eq .State.Status "exited"}}EXITED (code {{.State.ExitCode}}){{else if eq .State.Status "paused"}}PAUSED{{else}}{{.State.Status}}{{end}}' CONTAINERHealth Check with Fallback
# Show health status or "no healthcheck configured"
docker inspect --format='{{if .State.Health}}Health: {{.State.Health.Status}} ({{len .State.Health.Log}} checks logged){{else}}Health: no healthcheck configured{{end}}' CONTAINERConditional Port Display
# Show port mappings only if they exist
docker inspect --format='{{if .NetworkSettings.Ports}}Ports: {{range $p, $conf := .NetworkSettings.Ports}}{{$p}}->{{if $conf}}{{(index $conf 0).HostPort}}{{else}}unmapped{{end}} {{end}}{{else}}No ports exposed{{end}}' CONTAINERMemory Limit Check
# Show memory limit or "unlimited"
docker inspect --format='Memory: {{if eq .HostConfig.Memory 0}}unlimited{{else}}{{.HostConfig.Memory}} bytes{{end}}' CONTAINERNon-Zero Exit Code Alert
# Alert on non-zero exit codes
docker inspect --format='{{if and (eq .State.Status "exited") (ne .State.ExitCode 0)}}ALERT: {{.Name}} exited with code {{.State.ExitCode}}{{end}}' CONTAINER---
Range Loop Examples
Environment Variable Extraction
# All environment variables, one per line
docker inspect --format='{{range .Config.Env}}{{println .}}{{end}}' CONTAINER
# Filter-like: show env vars as KEY=VALUE (already in that format)
docker inspect --format='Environment:{{range .Config.Env}}
{{.}}{{end}}' CONTAINERMulti-Network Container Report
# Full network report for a container
docker inspect --format='Networks:{{range $name, $conf := .NetworkSettings.Networks}}
{{$name}}:
IP: {{$conf.IPAddress}}
Gateway: {{$conf.Gateway}}
MAC: {{$conf.MacAddress}}{{end}}' CONTAINERMount Summary
# All mounts with type, source, destination, and mode
docker inspect --format='Mounts:{{range .Mounts}}
[{{.Type}}] {{.Source}} -> {{.Destination}} ({{if .RW}}rw{{else}}ro{{end}}){{end}}' CONTAINERLabel Enumeration
# All labels formatted as key=value
docker inspect --format='Labels:{{range $k, $v := .Config.Labels}}
{{$k}} = {{$v}}{{end}}' CONTAINERContainer List on a Network
# All containers on a network with their IPs
docker network inspect --format='Containers on {{.Name}}:{{range .Containers}}
{{.Name}} ({{.IPv4Address}}){{end}}' NETWORKIPAM Configuration
# All subnet/gateway pairs for a network
docker network inspect --format='IPAM:{{range .IPAM.Config}}
Subnet: {{.Subnet}}
Gateway: {{.Gateway}}{{end}}' NETWORK---
Nested Map Access with index
Access Label with Dots in Key Name
# Labels with dots MUST use index function
docker inspect --format='{{index .Config.Labels "com.docker.compose.project"}}' CONTAINER
docker inspect --format='{{index .Config.Labels "org.opencontainers.image.version"}}' CONTAINERAccess Specific Port Binding
# Get host port for a specific container port
docker inspect --format='{{(index (index .NetworkSettings.Ports "8080/tcp") 0).HostPort}}' CONTAINER
# With nil guard (port may not be mapped)
docker inspect --format='{{if index .NetworkSettings.Ports "80/tcp"}}{{(index (index .NetworkSettings.Ports "80/tcp") 0).HostPort}}{{else}}not mapped{{end}}' CONTAINERAccess Specific Network Configuration
# Get IP for a specific network by name
docker inspect --format='{{(index .NetworkSettings.Networks "bridge").IPAddress}}' CONTAINER
# Get IP for a named network
docker inspect --format='{{(index .NetworkSettings.Networks "mynet").IPAddress}}' CONTAINER---
Scripting Integration Examples
Container IP Lookup Script
#!/bin/bash
# Get IP addresses for all running containers
for id in $(docker ps -q); do
name=$(docker inspect --format='{{.Name}}' "$id" | sed 's/^\///')
ip=$(docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "$id")
echo "$name: $ip"
doneHealth Monitoring Script
#!/bin/bash
# Check health of all containers with healthchecks
docker ps --format '{{.Names}}' | while read name; do
health=$(docker inspect --format='{{if .State.Health}}{{.State.Health.Status}}{{else}}none{{end}}' "$name")
if [ "$health" = "unhealthy" ]; then
echo "ALERT: $name is unhealthy"
docker inspect --format='{{json .State.Health}}' "$name" | jq '.Log[-1]'
fi
donePort Mapping Report
#!/bin/bash
# Report all port mappings across running containers
docker ps --format '{{.Names}}' | while read name; do
ports=$(docker inspect --format='{{range $p, $conf := .NetworkSettings.Ports}}{{if $conf}}{{$p}}->{{(index $conf 0).HostPort}} {{end}}{{end}}' "$name")
if [ -n "$ports" ]; then
echo "$name: $ports"
fi
doneResource Limit Audit
#!/bin/bash
# Audit containers without memory limits
docker ps -q | while read id; do
mem=$(docker inspect --format='{{.HostConfig.Memory}}' "$id")
name=$(docker inspect --format='{{.Name}}' "$id" | sed 's/^\///')
if [ "$mem" = "0" ]; then
echo "WARNING: $name has no memory limit"
else
echo "OK: $name limited to $((mem / 1024 / 1024))MB"
fi
doneCleanup Script Using Format Output
#!/bin/bash
# Remove containers that exited with non-zero status
docker ps -a --filter status=exited --format '{{.ID}} {{.Names}} {{.Status}}' | while read id name status; do
exitcode=$(docker inspect --format='{{.State.ExitCode}}' "$id")
if [ "$exitcode" -ne 0 ]; then
echo "Removing $name (exit code: $exitcode)"
docker rm "$id"
fi
done---
Combined Format with jq
Pretty-Print Specific Sections
# Pretty-print network settings
docker inspect --format='{{json .NetworkSettings}}' CONTAINER | jq .
# Pretty-print environment as object
docker inspect --format='{{json .Config.Env}}' CONTAINER | jq '.[] | split("=") | {(.[0]): .[1]}'
# Pretty-print mounts
docker inspect --format='{{json .Mounts}}' CONTAINER | jq '.[] | {type, source: .Source, dest: .Destination, rw: .RW}'
# Pretty-print labels
docker inspect --format='{{json .Config.Labels}}' CONTAINER | jq .Cross-Container Comparison
# Compare images across all running containers
docker ps --format json | jq -r '[.Names, .Image] | @tsv' | sort -k2
# Find containers using the same image
docker ps --format json | jq -s 'group_by(.Image) | .[] | select(length > 1) | {image: .[0].Image, containers: [.[].Names]}'---
Table Format Best Practices
Custom Table with Headers
# table keyword ALWAYS adds headers automatically
docker ps --format "table {{.Names}}\t{{.Image}}\t{{.Status}}\t{{.Ports}}"
# Output:
# NAMES IMAGE STATUS PORTS
# web nginx Up 2 hours 0.0.0.0:80->80/tcp
# db postgres Up 2 hours 5432/tcpWithout Headers (for Scripting)
# Omit "table" prefix to get raw values without headers
docker ps --format "{{.Names}}\t{{.Image}}\t{{.Status}}"
# Output:
# web nginx Up 2 hours
# db postgres Up 2 hoursMulti-Line Per Entry
# Use println for multi-line output per container
docker ps --format "Container: {{.Names}}\n Image: {{.Image}}\n Status: {{.Status}}\n"---
Windows PowerShell Considerations
# On PowerShell, use double quotes with escaped inner quotes
docker inspect --format="{{.State.Status}}" myapp
# For complex templates, use single quotes (works in PowerShell 7+)
docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' myapp
# Alternative: use backtick escaping
docker inspect --format="{{.State.Status}}" myappALWAYS prefer single-quoted --format='...' on Linux/macOS. On Windows CMD, use double quotes. On PowerShell, test both; single quotes work in most cases.
Go Template Format Patterns Reference
Complete catalog of 40+ ready-to-use --format patterns for Docker CLI commands.ALWAYS copy these patterns verbatim -- they are verified against Docker Engine 24+.
---
docker inspect -- Container Patterns
State & Lifecycle
# P-01: Container status (running, exited, paused, etc.)
docker inspect --format='{{.State.Status}}' CONTAINER
# P-02: Container PID on host
docker inspect --format='{{.State.Pid}}' CONTAINER
# P-03: Running boolean
docker inspect --format='{{.State.Running}}' CONTAINER
# P-04: Exit code
docker inspect --format='{{.State.ExitCode}}' CONTAINER
# P-05: Start time (RFC 3339)
docker inspect --format='{{.State.StartedAt}}' CONTAINER
# P-06: Finish time
docker inspect --format='{{.State.FinishedAt}}' CONTAINER
# P-07: Health check status (with nil guard)
docker inspect --format='{{if .State.Health}}{{.State.Health.Status}}{{else}}no-healthcheck{{end}}' CONTAINER
# P-08: OOM killed boolean
docker inspect --format='{{.State.OOMKilled}}' CONTAINER
# P-09: Restart count
docker inspect --format='{{.RestartCount}}' CONTAINER
# P-10: Restart policy
docker inspect --format='{{.HostConfig.RestartPolicy.Name}}' CONTAINERNetwork Information
# P-11: IP address (first network)
docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' CONTAINER
# P-12: All networks with IPs
docker inspect --format='{{range $net, $conf := .NetworkSettings.Networks}}{{$net}}={{$conf.IPAddress}} {{end}}' CONTAINER
# P-13: MAC address
docker inspect --format='{{range .NetworkSettings.Networks}}{{.MacAddress}}{{end}}' CONTAINER
# P-14: Gateway
docker inspect --format='{{range .NetworkSettings.Networks}}{{.Gateway}}{{end}}' CONTAINER
# P-15: All port bindings
docker inspect --format='{{range $p, $conf := .NetworkSettings.Ports}}{{$p}} -> {{(index $conf 0).HostPort}}{{println}}{{end}}' CONTAINER
# P-16: Specific port (80/tcp) host mapping
docker inspect --format='{{(index (index .NetworkSettings.Ports "80/tcp") 0).HostPort}}' CONTAINER
# P-17: Network mode
docker inspect --format='{{.HostConfig.NetworkMode}}' CONTAINER
# P-18: DNS servers
docker inspect --format='{{json .HostConfig.Dns}}' CONTAINERConfiguration
# P-19: Image name
docker inspect --format='{{.Config.Image}}' CONTAINER
# P-20: Entrypoint (JSON array)
docker inspect --format='{{json .Config.Entrypoint}}' CONTAINER
# P-21: Command (JSON array)
docker inspect --format='{{json .Config.Cmd}}' CONTAINER
# P-22: Working directory
docker inspect --format='{{.Config.WorkingDir}}' CONTAINER
# P-23: Hostname
docker inspect --format='{{.Config.Hostname}}' CONTAINER
# P-24: Environment variables (one per line)
docker inspect --format='{{range .Config.Env}}{{println .}}{{end}}' CONTAINER
# P-25: All labels (JSON)
docker inspect --format='{{json .Config.Labels}}' CONTAINER
# P-26: Specific label by key
docker inspect --format='{{index .Config.Labels "com.example.version"}}' CONTAINER
# P-27: Exposed ports
docker inspect --format='{{json .Config.ExposedPorts}}' CONTAINER
# P-28: User
docker inspect --format='{{.Config.User}}' CONTAINERMounts & Storage
# P-29: All mounts (JSON)
docker inspect --format='{{json .Mounts}}' CONTAINER
# P-30: Mount sources and destinations
docker inspect --format='{{range .Mounts}}{{.Source}} -> {{.Destination}}{{println}}{{end}}' CONTAINER
# P-31: Mount types and names
docker inspect --format='{{range .Mounts}}{{.Type}}: {{.Name}} @ {{.Destination}}{{println}}{{end}}' CONTAINER
# P-32: Log file path
docker inspect --format='{{.LogPath}}' CONTAINER
# P-33: Root filesystem size (use with docker inspect -s)
docker inspect --size --format='{{.SizeRootFs}}' CONTAINER
# P-34: Writable layer size (use with docker inspect -s)
docker inspect --size --format='{{.SizeRw}}' CONTAINERResource Limits
# P-35: Memory limit (bytes, 0 = unlimited)
docker inspect --format='{{.HostConfig.Memory}}' CONTAINER
# P-36: CPU shares
docker inspect --format='{{.HostConfig.CpuShares}}' CONTAINER
# P-37: CPU quota and period (NanoCpus)
docker inspect --format='{{.HostConfig.NanoCpus}}' CONTAINER
# P-38: PID limit
docker inspect --format='{{.HostConfig.PidsLimit}}' CONTAINER---
docker ps Patterns
# P-39: Compact status table
docker ps --format "table {{.Names}}\t{{.Image}}\t{{.Status}}\t{{.Ports}}"
# P-40: Names only (for scripting)
docker ps --format "{{.Names}}"
# P-41: IDs only (equivalent to -q)
docker ps --format "{{.ID}}"
# P-42: Name with state and uptime
docker ps --format "table {{.Names}}\t{{.State}}\t{{.RunningFor}}"
# P-43: With specific label column
docker ps --format "table {{.Names}}\t{{.Label \"app\"}}\t{{.Status}}"
# P-44: With size info (requires -s flag)
docker ps -s --format "table {{.Names}}\t{{.Size}}"
# P-45: With network and mount info
docker ps --format "table {{.Names}}\t{{.Networks}}\t{{.Mounts}}"
# P-46: JSON output (one object per line, native)
docker ps --format json
# P-47: All containers with exit info
docker ps -a --format "table {{.Names}}\t{{.Status}}\t{{.Command}}"---
docker images Patterns
# P-48: Compact image table
docker images --format "table {{.Repository}}\t{{.Tag}}\t{{.Size}}"
# P-49: Repository:tag pairs (for scripting)
docker images --format "{{.Repository}}:{{.Tag}}"
# P-50: With creation time
docker images --format "table {{.Repository}}\t{{.Tag}}\t{{.CreatedSince}}\t{{.Size}}"
# P-51: With digest
docker images --digests --format "table {{.Repository}}\t{{.Tag}}\t{{.Digest}}"
# P-52: IDs only (equivalent to -q)
docker images --format "{{.ID}}"
# P-53: JSON output
docker images --format json---
docker stats Patterns
# P-54: CPU and memory overview
docker stats --no-stream --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.MemPerc}}"
# P-55: Full resource view
docker stats --no-stream --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.NetIO}}\t{{.BlockIO}}\t{{.PIDs}}"
# P-56: Names and CPU only
docker stats --no-stream --format "table {{.Name}}\t{{.CPUPerc}}"
# P-57: Container ID with stats
docker stats --no-stream --format "table {{.Container}}\t{{.Name}}\t{{.CPUPerc}}\t{{.MemPerc}}"---
docker network Patterns
# P-58: Network listing with driver
docker network ls --format "table {{.Name}}\t{{.Driver}}\t{{.Scope}}"
# P-59: Network IDs only
docker network ls --format "{{.ID}}"
# P-60: Network names only
docker network ls --format "{{.Name}}"
# P-61: Containers on a specific network
docker network inspect --format='{{range .Containers}}{{.Name}} ({{.IPv4Address}}){{println}}{{end}}' NETWORK
# P-62: Network subnet
docker network inspect --format='{{range .IPAM.Config}}{{.Subnet}}{{end}}' NETWORK
# P-63: Network gateway
docker network inspect --format='{{range .IPAM.Config}}{{.Gateway}}{{end}}' NETWORK---
docker volume Patterns
# P-64: Volume listing with driver
docker volume ls --format "table {{.Name}}\t{{.Driver}}\t{{.Mountpoint}}"
# P-65: Volume names only
docker volume ls --format "{{.Name}}"
# P-66: Volume mount point
docker volume inspect --format='{{.Mountpoint}}' VOLUME
# P-67: Volume labels (JSON)
docker volume inspect --format='{{json .Labels}}' VOLUME
# P-68: Volume creation time
docker volume inspect --format='{{.CreatedAt}}' VOLUME---
docker system Patterns
# P-69: Disk usage table
docker system df --format "table {{.Type}}\t{{.TotalCount}}\t{{.Size}}\t{{.Reclaimable}}"
# P-70: Server version
docker info --format '{{.ServerVersion}}'
# P-71: Storage driver
docker info --format '{{.Driver}}'
# P-72: Operating system
docker info --format '{{.OperatingSystem}}'
# P-73: Total memory
docker info --format '{{.MemTotal}}'
# P-74: Number of containers
docker info --format 'Running: {{.ContainersRunning}}, Stopped: {{.ContainersStopped}}'
# P-75: Plugins (JSON)
docker info --format '{{json .Plugins}}'---
docker events Patterns
# P-76: Custom event format
docker events --format '{{.Time}} {{.Type}} {{.Action}} {{.Actor.Attributes.name}}'
# P-77: JSON events
docker events --format '{{json .}}'---
Pattern Index by Use Case
| Use Case | Pattern IDs |
|---|---|
| Container health monitoring | P-01, P-07, P-08, P-09 |
| Network debugging | P-11 through P-18, P-61 through P-63 |
| Security auditing | P-10, P-19, P-25, P-28, P-35 through P-38 |
| Scripting / automation | P-40, P-41, P-46, P-49, P-52, P-59, P-65 |
| Resource monitoring | P-33 through P-38, P-54 through P-57, P-69 |
| Configuration inspection | P-19 through P-28 |
| Storage / volume management | P-29 through P-34, P-64 through P-68 |
| System overview | P-69 through P-75 |