dockermi

TL;DR

Tired of deleting several tags for the same Docker image? dockermi might help.

When generating images with dibs, I usually set several alias tags to keep track of different aspects. As an example, I usually generate both a versioned tag and latest, in addition to a date-related tag to keep track of this info too. We might go on futher, of course.

When doing some cleaning, though, I have to go through all these aliases and remove them one by one. Or have I?

I was about to write the typical ad-hoc script when I realized that… I already had done this in the past!

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#!/bin/sh

if [ "$#" -eq 0 ] ; then
   docker images \
      | perl -nale 'print $F[2] if $F[0] eq "<none>"' \
      | while read imageid ; do
         docker rmi "$imageid"
      done
else
   for target in "$@" ; do
      docker images \
         | grep "^$target " \
         | while read repo tag rest ; do
            docker rmi "$repo:$tag"
         done
   done
fi

Local version here.

Remove hanging images

Sometimes images are kicked out because new ones take their tags. In this case, they usually end up with name <none>, which makes them a bit difficult to get rid of (hint: use the image id). When you call the script without any parameter, it will get rid of those stale, hanging images:

$ dockermi

Remove all tags associated to an image

The other use case where dockermi can prove useful is to get rid of all variants of a specific image name. As an example, suppose that you have several tags associated to image foo/barbazius:

foo/barbazius:latest
foo/barbazius:1.0
foo/barbazius:1.0.3
foo/barbazius:20200306-102030-1234

To get rid of all of them in one single sweep you would call:

$ dockermi foo/barbazius

So long!

I think it’s fair to close this post here… have fun!


Comments? Octodon, , GitHub, Reddit, or drop me a line!