To remove a subset of images
Add a filter, with -f
.
docker rmi -f $(docker images -af <YOUR_FILTER_PATTERN> -q)
E.g. if docker images
returns:
image3 latest 3a371a8efe91 12 days ago 987MB
image2 latest cca6cd42c697 12 days ago 987MB
image1 latest 0373470f2972 12 days ago 987MB
image0 latest 1a99848b511f 13 days ago 987MB
node 18 5087dac9940a 2 weeks ago 947MB
nginx latest 8a5e3e44915c 2 weeks ago 135MB
alpine latest 04eeaa5f8c35 6 weeks ago 7.46MB
hello-world latest 46331d942d63 11 months ago 9.14kB
-f since=*
docker rmi -f $(docker images -af since=node:18 -q)
will result in:
node 18 5087dac9940a 2 weeks ago 947MB
nginx latest 8a5e3e44915c 2 weeks ago 135MB
alpine latest 04eeaa5f8c35 6 weeks ago 7.46MB
hello-world latest 46331d942d63 11 months ago 9.14kB
There are a few options including since
,before
,label
or reference
(pattern match). The docs.
This might be useful if you have a development loop involving repeated builds, but want to keep the base OS local (e.g. node) to avoid repeated downloads.