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}}
.
In combination with When I work with containers, I usually have something like this somewhere to the side, just to keep track of what has been accumulating: Big screens and tiling window managers are very handy for things like this. |