Skip to content
Switch branches/tags
Go to file
Cannot retrieve contributors at this time
34 lines (22 sloc) 2.21 KB

Listing the labels of an image (or container or task)

Docker leverages Go templates to display information about an image (or a container or a task). I find Go templates to be somewhat tricky, perhaps because I'm not familiar with the Go language, at least not yet (circa 2016).

As I work up the Docker learning curve I've accumulated a large and growing set of images. I decided to start adding labels to some of my docker images so that I could use a --filter to reduce the list of images to just the few I'm interested in.

To see the set of labels attached to a specific image you must use the docker inspect command. Unqualified, that command will spew out a long nested JSON object. To just display the bits you are interested in, you have to use the --format option, and that obliges you to learn about Go templates.

After an unsettling amount of trial and and error, and reading and re-reading the documenation, I finally settled on these two variations:

docker inspect --format='{{range $k, $v := .ContainerConfig.Labels}} {{- $k}}={{$v}}{{printf "\n" -}} {{end}}' datihein/rsync-alpine
docker inspect --format='{{range $k, $v := .ContainerConfig.Labels}} {{- printf "%s = \"%s\"\n" $k $v -}} {{end}}' datihein/rsync-alpine

I like the second one best. It outputs something like this:

net.develves.distro = "alpine"
net.develves.distro-version = "alpine 3.4"
net.develves.examples = "flask-web-development-book"

© 2016 Dave Hein

Creative Commons License
This work by Dave Hein is licensed under a Creative Commons Attribution 4.0 International License.