Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

docker

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?

like image 444
Greg Avatar asked Sep 09 '15 22:09

Greg


2 Answers

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.

like image 161
Vlad-HC Avatar answered Oct 13 '22 04:10

Vlad-HC


Fun with bash:

docker rmi $(docker images | grep stuff_ | tr -s ' ' | cut -d ' ' -f 3) 
like image 26
Michael Avatar answered Oct 13 '22 06:10

Michael