How can I delete Docker images by tag, preferably with wildcarding?

Docker

Docker Problem Overview


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 Solutions


Solution 1 - Docker

Using only docker filtering:

 docker rmi $(docker images --filter=reference="*:stuff_*" -q)
  • reference="*:stuff_*" filter allows you to filter images using a wildcard;
  • -q option is for displaying only image IDs.

Update: Wildcards are matched like paths. That means if your image id is my-company/my-project/my-service:v123 then the * won't match it, but the */*/* will. See the github issue for description.

Solution 2 - Docker

Fun with bash:

docker rmi $(docker images | grep stuff_ | tr -s ' ' | cut -d ' ' -f 3)

Solution 3 - Docker

You can also acomplish it using grep + args + xargs:

docker images | grep "stuff_" | awk '{print $1 ":" $2}' | xargs docker rmi
  • docker images lists all the images
  • grep selects the lines based on the search for "_stuff"
  • awk will print the first and second arguments of those lines (the image name and tag name) with a colon in between
  • xargs will run the command 'docker rmi' using every line returned by awk as it's argument

Note: be careful with the grep search, as it could also match on something besides the tag, so best do a dry run first:

docker images | grep "stuff_" | awk '{print $1 ":" $2}' | xargs -n1 echo
  • xargs -n1 the -n1 flags means xargs will not group the lines returned by awk together, but echo them out one at a time (for better readability)

Solution 4 - Docker

only using docker

docker rmi $(docker images -q junk/root:*)

Solution 5 - Docker

Docker provides some filtering which you can use with labels, but I don't see wildcard support.

docker images -f "label=mylabel=myvalue"

Furthermore to add labels to an image you must add the information to the Dockerfile with a LABEL command. I couldn't find a way to add labels to an image unless you changed the Dockerfile (i.e. couldn't find a commandline option), though you can add them to containers at runtime with --label and --label-file (run docs).

Solution 6 - Docker

docker rmi $(docker images | grep stuff | tr -s ' ' | cut -d ' ' -f 3)

Solution 7 - Docker

I would use the image id to remove the images, the tag being '1.2.3':

docker rmi $(docker images | awk '$2~/1.2.3/{print $3}')

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGregView Question on Stackoverflow
Solution 1 - DockerVlad-HCView Answer on Stackoverflow
Solution 2 - DockerMichaelView Answer on Stackoverflow
Solution 3 - DockerklogdView Answer on Stackoverflow
Solution 4 - DockerRuwanka MadhushanView Answer on Stackoverflow
Solution 5 - DockerAndyView Answer on Stackoverflow
Solution 6 - Dockerchan zanwayView Answer on Stackoverflow
Solution 7 - DockerTim ChaubetView Answer on Stackoverflow