码头工人检查:如何从范围中仅选择某些键

I created an docker image that has few labels, here is my Dockerfile section on LABELS:

grep LABEL Dockerfile

LABEL "css1"="/var/www/css1"
LABEL "css2"="/var/www/css2"
LABEL "img"="/var/www/img"
LABEL "js"="/var/www/js"

Then:

docker image inspect --format='{{.Config.Labels}}' labels-test

map[css1:/var/www/css1 css2:/var/www/css2 img:/var/www/img js:/var/www/js]

I need to get for example all labels starting with css. This is as far as i was able to figure:

docker image inspect --format='{{range $k,$v:=.Config.Labels}}{{$k}}:{{$v}} {{end}}' labels-test

css1:/var/www/css1 css2:/var/www/css2 img:/var/www/img js:/var/www/js

Desired output would be:

css1:/var/www/css1 css2:/var/www/css2

The Go template functions are available in golang docco

eq can test if arg1 == arg2.
printf "%.3s" $k will give you the first 3 chars of a string.

docker image inspect \
  --format='{{ range $k,$v:=.Config.Labels }}{{ if eq (printf "%.3s" $k) "css" }}{{ $k }}:{{ $v }} {{end}}{{end}}' \
  IMAGE

You might want to look at the querying the Docker API images endpoint /images/IMAGE/json directly or processing the JSON output somewhere if you need to do any more advanced processing:

docker image inspect \
  --format='{{json .Config.Labels}}' \
  IMAGE

You can do something like

docker inspect --format='{{index (index (.Config.Labels)).css1 }}' labels-test

which shows for me

/var/www/css1

and also

docker inspect --format='{{index (index (.Config.Labels)).css2 }}' labels-test

which shows for me

/var/www/css2

See my previous answer on that subject How to get ENV variable when doing Docker Inspect

Edit

The following gives exactly what you ask for

docker inspect --format='{{index (index (.Config.Labels)).css1 }} {{index (index (.Config.Labels)).css2 }} labels-test

as i get

/var/www/css1 /var/www/css2