How can I get a Docker image's label if the la

2019-02-21 07:40发布

问题:

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.

回答1:

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