I have some automated processes that spew a bunch of Docker images tagged with meaningful labels. The labels follow a structured pattern.
Is there a way to find and delete images by tag? So assume I have images:
REPOSITORY TAG
junk/root stuff_687805
junk/root stuff_384962
Ideally I'd like to be able to do: docker rmi -tag stuff_*
Any good way to simulate that?
Docker provides some filtering which you can use with labels, but I don't see wildcard support.
Furthermore to add labels to an image you must add the information to the
Dockerfile
with aLABEL
command. I couldn't find a way to add labels to an image unless you changed theDockerfile
(i.e. couldn't find a commandline option), though you can add them to containers at runtime with--label
and--label-file
(run docs).Using only docker filtering:
reference="*:stuff_*"
filter allows you to filter images using a wildcard;-q
option is for displaying only image IDs.docker rmi $(docker images | grep stuff | tr -s ' ' | cut -d ' ' -f 3)
You can also acomplish it using grep + args + xargs:
Note: be careful with the grep search, as it could also match on something besides the tag, so best do a dry run first:
Fun with bash:
I would use the image id to remove the images, the tag being '1.2.3':