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

Patricia Arquette
Release: 2024-11-14 17:39:02
Original
248 people have browsed it

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"
Copy after login

Using the inspect command with the --format option:

$ docker inspect -f '{{ .Config.Labels.foo }}' foo
Copy after login

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
Copy after login

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
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template