Docker inspect data subsets
Inspect the state of a docker container
You can inspect the state of a container using the following command
cid=REPLACE_THIS_WITH_THE_CONTAINER_ID_OR_NAME
docker inspect $cid
this returns a json document with comprehensive information on the container. This information can be manipulated using a tool like jq on the command line.
Using docker inspect templates to get a subset of the data
If you use the -f argument when calling docker inspect you can use a golang template to control the content that gets emmited, the following are some samples
Get the container process pid
cid=REPLACE_THIS_WITH_THE_CONTAINER_ID_OR_NAME
docker inspect -f '{{ .State.Pid }}' $cid
> 32109
docker is passing the result of the object returned from a full docker inspect command to the ‘{{ .State.Pid }}’ go text template, it is then accessing state within the object using that first “.” so we can do print anything we like
cid=REPLACE_THIS_WITH_THE_CONTAINER_ID_OR_NAME
docker inspect -f 'The process id is {{ .State.Pid }}' $cid
> The process id is 32109
Script to echo some high level info that is not included in “docker ps” command output
#!/usr/bin/env bash
# Containers to run for - default is all, if none passed
containers=$@
if [[ $# -eq 0 ]]; then containers=$(docker ps -q); fi
# See http://golang.org/pkg/text/template/
template='Id: {{.Id}}
Name: {{.Name}}
Image: {{.Image}}
Pid: {{.State.Pid}}
IP: {{.NetworkSettings.IPAddress}}
Host: {{.Config.Hostname}}
EntryPoint: {{.Config.Entrypoint}}
Command: {{.Config.Cmd}}
Ports: {{range $key, $value := .Config.ExposedPorts}}{{$key}} {{end}}
Links: {{range .HostConfig.Links}}{{.}} {{end}}
Volumes: {{if .Volumes}}{{range $key, $value := .Volumes}}
{{$key}} -> {{$value}}{{end}}{{end}}
'
docker inspect -f "$template" $containers