How can I get a Docker image's label if the label name has a "." in it?

huangapple go评论85阅读模式
英文:

How can I get a Docker image's label if the label name has a "." in it?

问题

docker inspect命令对于获取Docker镜像上的标签非常有用:

# -*- Dockerfile -*-
FROM busybox
LABEL foo="bar"
LABEL com.wherever.foo="bang"

对于简单的标签名称,inspect命令有一个--format选项(使用Go模板),可以很好地工作。

$ docker build -t foo .
$ docker inspect -f '{{ .Config.Labels.foo }}' foo
bar

但是,如何访问名称中带有点的标签呢?

$ docker inspect -f '{{ .Config.Labels.com.wherever.foo }}' foo
<no value>

我正在使用bash脚本编写这个问题,如果可能的话,我想避免重新解析docker inspect的JSON输出。

英文:

The docker inspect command can be very useful for getting the labels on a Docker image:

# -*- Dockerfile -*-
FROM busybox
LABEL foo=&quot;bar&quot;
LABEL com.wherever.foo=&quot;bang&quot;

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 &#39;{{ .Config.Labels.foo }}&#39; foo
bar

But how do I access labels that have a dot in their name?

$ docker inspect -f &#39;{{ .Config.Labels.com.wherever.foo }}&#39; foo
&lt;no value&gt;

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

得分: 47

index函数是我正在寻找的。它可以在映射中查找任意字符串。

$ docker inspect -f '{{ index .Config.Labels "com.wherever.foo" }}' foo
bang
英文:

The index function is what I was looking for. It can lookup arbitrary strings in the map.

$ docker inspect -f &#39;{{ index .Config.Labels &quot;com.wherever.foo&quot; }}&#39; foo
bang

huangapple
  • 本文由 发表于 2015年11月24日 11:20:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/33884870.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定