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

2019-02-21 07:43发布

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条回答
仙女界的扛把子
2楼-- · 2019-02-21 08:18

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
查看更多
登录 后发表回答