Docker 101 format command output

Dejanu Alex
FAUN — Developer Community 🐾
3 min readJun 9, 2021

--

One of the most underrated flags of docker CLI is --format 🏴

I often see different constructs using docker CLI and redirections toawk xargs or grep in order to extract information about containers.

Docker provides the formatting option --format which uses Go templates that allow the manipulation of the output of certain commands and log drivers.

Suppose we want only the name and status for all our containers, docker ps -a is too verbose, so we can use the --format flag to extract the desired fields.

docker ps -a --format 'table {{.Names}}\t{{.Status}}'

We can even check the “real” container runtime used by Docker

docker system info --format 'Docker uses {{title .DefaultRuntime}} as container runtime'
client for running applications packaged according to the Open Container Initiative (OCI) format

RunC is a OCI-spec compliant container runtime originally developed as part of Docker and later extracted out as a separate open source tool and library .

If we want to check the environment variables for a particular image let’s say

docker inspect --format '{{ .Config.Env}}' <IMAGE_NAME>#actually we can go even further
docker inspect --format '{{range .Config.Env}}{{with split . "="}}{{printf "%s: %s\n" ( index . 0 ) ( index . 1 )}}{{end}}{{end}}' <IMAGE_NAME>
env vars for grafana image

To conclude, do not hesitate to use --format flag to extract the desired information, and as a hint always check what data can be extracted using json function, e.g. docker container ls --format='{{json .}}'

Bellow example of Go template placeholders :

Join FAUN: Website 💻|Podcast 🎙️|Twitter 🐦|Facebook 👥|Instagram 📷|Facebook Group 🗣️|Linkedin Group 💬| Slack 📱|Cloud Native News 📰|More.

If this post was helpful, please click the clap 👏 button below a few times to show your support for the author 👇

--

--