Targeted Docker Inspection

2020-06-10

How do you get basic information about Docker containers in a script? Neither docker ps nor docker inspect feel really neat — one is a pain to parse, the other is very verbose.

If you are happy adding a dependency to your script, the ever more popular jq can do the job for you:

  docker inspect --format="{{.State.Running}}" my-container \
| jq .[0].State.Running

While jq is a useful helper to pick up, this still feels a little off. Shouldn't Docker have something for us?

It does, indeed! Despite the misleading name, --format does the trick.

docker inspect --format="{{.State.Running}}" my-container

It is easy to see how you can cobble together whatever information you need from here, in whichever arrangement. If pain text gets too messy, you can also go back to JSON easily, for instance like {{json .Config.Env}}.

Tip

In combination with watch, you can use --format with the different docker commands to create your favorite live Docker dashboard with only a few terminals!

When I work with containers, I usually have something like this somewhere to the side, just to keep track of what has been accumulating:

Live container and image lists in terminals

Big screens and tiling window managers are very handy for things like this.

ToolDocker
Comment & Share on Twitter 

Sane YAML With JSON Schema

Using Build Tools From Containers