brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: docker inspect --format pulls one value out of the wall of JSON

Docker
~/tips/tip-docker-inspect-format

docker inspect --format '{{.State.Status}}' mycontainer

Prints a single field of a container, such as its mounts, instead of the whole inspect output.

docker inspect prints several hundred lines. Nobody wants to scroll through those to find one IP address.

The --format flag takes a Go template, so you can ask for one field. Nested fields work with dots.

docker inspect --format '{{.State.Status}}' mycontainer
docker inspect --format '{{json .Mounts}}' mycontainer

The json function prints a whole structure as JSON, which is handy for mounts, networks or labels. If you do not know the field names, run docker inspect --format '{{json .}}' mycontainer once and read what is there.

A few that I find useful: the status, the restart policy, the networks and the mounts. Once you have a template that works, put it in a shell alias, and you never have to remember the field names again. It works the same for networks and labels.

The caveat is the shell. Use single quotes around the template, otherwise your shell may eat the braces or quotes. And a field that exists on one container may be missing on another, for example the health status on a container without a healthcheck, so the template can error out.

Further reading: Format command and log output, On this site: docker ps –format table gives you a readable overview, On this site: docker diff shows what a container changed in its filesystem

More Docker tips ONBUILD RUN /usr/local/bin/python-build --dir /app/src SHELL ["/bin/bash", "-c"] docker commit <container> myname/testimage:version3 docker import /path/to/exampleimage.tgz docker compose events All Docker tips (192) All topics

Link