drmi — a tool for cleaning up Docker images

Image for post
Image for post

This tool is available as part of my dockertools project on GitHub

As I am doing work on containers, I have occasionally run into cases where I need to do tasks that involve a lot of repetition. I had recently been playing around with running minikube directly in containers. It works great, but it also creates a lot of images around:

So I wanted to create a way to remove all these images without typing out the names — the default docker rmi command is pretty limited and requires you to enter each image name and tag individually. I had recently created a script called ctsh that allows you to easily run a command in a container by only specifying part of the image name that the container is running, part of the container name, or part of the container id:

Using parts of this script, I created drmi so that you can easily remove a bunch of images by only specifying part of the image name or the image id. For example, say I have these images (ignoring my k8s images):

Removing these is complicated because in a couple cases I not only have to specify the image, I have to specify the version (for example, debian and stretch) even though there is only one version of each of the images. Doing this with drmi is pretty easy:

Note that it can only remove images if they aren’t in running containers — I will eventually enhance this so that it can stop containers that are running the image you are trying to remove, but for now you’ll get an error and you’ll have to stop the container first. For example, if I have a running nginx container, I won’t be able to remove nginx:

For now, I don’t automatically remove an image if there is a stopped container unless you pass in a -f flag:

Now I can easily remove all my k8s images like this:

I also noticed that I sometimes have images lingering around that don’t have a name/tag:

To make it easy to clean these up, I added a -u flag. It can be used in combination with the -f flag if there are stopped containers using these images. So cleaning up these is just:

TODO

  • add flag for stopping running containers using matching images to be removed
  • better command line switch processing
  • better output formatting
  • add similar command to stop containers based on a partial name

Originally published on July 11, 2018.

Written by

I'm a technology enthusiast, always searching for better ways of doing things. Lately that has been all things React. I also write a lot on Medium. :)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store