Home >Backend Development >Golang >How to Access Docker Image Labels with Dots in Their Names Using the `inspect` Command?

How to Access Docker Image Labels with Dots in Their Names Using the `inspect` Command?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-14 17:39:02356browse

How to Access Docker Image Labels with Dots in Their Names Using the `inspect` Command?

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.

The above is the detailed content of How to Access Docker Image Labels with Dots in Their Names Using the `inspect` Command?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn