How to Retrieve Dot-Delimited Labels in Docker with `docker inspect`?

Barbara Streisand
Release: 2024-11-09 15:06:02
Original
706 people have browsed it

How to Retrieve Dot-Delimited Labels in Docker with `docker inspect`?

Navigating Dot-Delimited Docker Image Labels

The Docker inspect command proves valuable in retrieving labels associated with Docker images. Using the --format option, one can conveniently access label values using Go templates. However, challenges arise when dealing with labels containing dots (".") in their names.

The Challenge of Dotted Label Names

As demonstrated in the code below, using the --format option fails to retrieve labels with dots in their names:

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

# Build the image
$ docker build -t foo .

# Attempt to retrieve the label using --format
$ docker inspect -f '{{ .Config.Labels.com.wherever.foo }}' foo
<no value>
Copy after login

Resolving the Issue with the Index Function

To access dotted labels, utilize the index function, which allows for the retrieval of arbitrary strings from a map. This eliminates the need to parse the JSON output from docker inspect.

# Using the index function
$ docker inspect -f '{{ index .Config.Labels "com.wherever.foo" }}' foo
bang
Copy after login

The above is the detailed content of How to Retrieve Dot-Delimited Labels in Docker with `docker inspect`?. 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