Consider the following json data, sourced from from Docker image nginx:1.25-bookworm
:
# docker pull nginx:1.25-bookworm
# docker image inspect 81be38025439 | jq '.[].Config.Env'
[
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"NGINX_VERSION=1.25.3",
"NJS_VERSION=0.8.2",
"PKG_RELEASE=1~bookworm"
]
#
I would like to be able to access ‘1.25.3’ from this list to tag the image in a build step of a CI-CD pipeline.
I am close to my goal with the following:
# docker image inspect 0743972990c7 | jq -r '.[].Config.Env[1]'
NGINX_VERSION=1.23.4
But would like to know how to access the item ‘NGINX_VERSION’ by name instead of relying on the assumption that it will be the second environment variable.
2
Answers
So filter NGINX_VERSION from the array and remove it.
For the task given, I’d also go with
startswith
as described in KamilCuk’s answer. But if you need to access multiple values, you might want to consider creating a lookup object usingINDEX
:and then access the values by field name (truncate by the
index
position of the first=
character):Demo