I want to change my docker-compose file service images via yq tool. The problem is that I have several services which have the same name prefix and I need to select their image fields and update them:
input example:
---
version: '3.4'
services:
app_sandbox_zeta:
image: some-image:not-latest
app_sandbox_sigma:
image: some-image:not-latest
general_app: ...
some_app: ...
expected output:
---
version: '3.4'
services:
app_sandbox_zeta:
image: some-image:latest
app_sandbox_sigma:
image: some-image:latest
general_app: ...
some_app: ...
So, how can I achieve it? I've tried to select keys, but I can not get the image
key underneath:
cat docker-compose.yml | yq '.services |= with_entries(select(.key == "app_sandbox*"))'
*yq (https://github.com/mikefarah/yq/) version 4.27.3
There's no need to use with_entries
(which provides .key
) as you can directly access the key using key
. Add .image
to descend into each (selected) image, and assign your new value(s).
.services[] |= select(key == "app_sandbox*").image = "some-image:latest"
Updating (|=
) with an assignment (=
) filter, however, may be hard to read (or to know of the individual precedences involved). It might be more convenient to wrap (at least the traversal part of) the LHS in parens, and use the assignment on that:
(.services[] | select(key == "app_sandbox*")).image = "some-image:latest"