Accessing Docker Image Labels with Dots in Names
Docker's inspect command provides a convenient way to retrieve a container image's labels. However, extracting labels with dots in their names using the --format option and Go templates can prove challenging.
Issue:
The following Docker image defines two labels:
FROM busybox LABEL foo="bar" LABEL com.wherever.foo="bang"
Using the inspect command with the --format option:
$ docker inspect -f '{{ .Config.Labels.foo }}' foo
returns the value for the "foo" label correctly. However, trying to access the label with a dot in its name:
$ docker inspect -f '{{ .Config.Labels.com.wherever.foo }}' foo
results in "
Solution:
To retrieve labels with dots in their names, use the index function within the Go template:
$ docker inspect -f '{{ index .Config.Labels "com.wherever.foo" }}' foo
This will output the desired label value, "bang".
The index function looks up arbitrary strings in the map and returns the corresponding value if found. By specifying the label name as the second parameter, the function retrieves the associated value from the Labels map of the Config object.
以上是如何使用「inspect」指令存取名稱中帶點的 Docker 映像標籤?的詳細內容。更多資訊請關注PHP中文網其他相關文章!