The docker inspect command can be very useful for getting the labels on a Docker image:
# -*- Dockerfile -*-
FROM busybox
LABEL foo="bar"
LABEL com.wherever.foo="bang"
For simple label names, the inspect command has a --format
option (which uses Go templates) that works nicely.
$ docker build -t foo .
$ docker inspect -f '{{ .Config.Labels.foo }}' foo
bar
But how do I access labels that have a dot in their name?
$ docker inspect -f '{{ .Config.Labels.com.wherever.foo }}' foo
<no value>
I'm writing this in a bash script, where I'd like to avoid re-parsing the JSON output from docker inspect
, if possible.
To check the labels of a particular Image, you can use the Docker Inspect command. Start the Docker Container. Execute the Inspect Command. Inside the LABELS object, you can find all the labels associated with the image that you have specified inside your Dockerfile.
A docker tag name must be valid ASCII and may contain lowercase and uppercase letters, digits, underscores, periods, and dashes. A tag name may not start with a period or a dash and may contain a maximum of 128 characters. You can group your images together using names and tags.
A label is a key-value pair, stored as a string. You can specify multiple labels for an object, but each key must be unique within an object. If the same key is given multiple values, the most-recently-written value overwrites all previous values.
The index
function is what I was looking for. It can lookup arbitrary strings in the map.
$ docker inspect -f '{{ index .Config.Labels "com.wherever.foo" }}' foo
bang
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With