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>
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
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!