How to remove old, unused images of Docker?


There are times in a project where you have been working for quite a long time and you create and tag docker images on daily basis but after some time it is generally observed that we tend to forget how many docker images we have build and tagged.

I do not see a problem in building and tagging a new docker image number of times but if you do not do the housekeeping of docker images properly you might soon consume all of disk space and then you need to look for commands which you can use for removing old, unused docker images.

So in this post, we will see the number of ways by which we can remove or delete unused old docker images.

Here is the table of content, so choose the option carefully based on your need -

  1. Remove all docker images using docker prune
  2. Prune docker images, volumes, containers individually
  3. Alias for removing dangling docker images
  4. bahsrc function to remove docker images
  5. docker-gc a simple script for docker garbage collection


1. Remove all docker images using docker prune

If you are stuck and you want to have a clean slate where you do not want to have any previous docker images, containers, volumes, and networks then use the following command prune command provided by docker -

1docker system prune

The above command will remove all the dangling images which are not tagged or referenced by any active container.

1.1 Remove all the images

As docker system prune will only remove the dangling images but if you want to remove all the images which are not used by existing containers then you can add an additional flag -a

1docker system prune -a 

After issuing the above command it will ask for the confirmation -

1WARNING! This will remove all images without at least one container associated to them.
2Are you sure you want to continue? [y/N] y  


1.2 Remove specific docker images using filters

If you want to have more control over deletion and removal of the docker image then you can use the filters along with the docker prune command.

Here is one example where I can delete the image by name

1docker system prune -a --filter "name=my_image_name"

Here is one more example where you can prune the image by exit status

1docker system prune -a --filter 'exited=0'

So there are many possibilities by which you can refine your search to remove or delete the Docker container based on the various filter parameters.

Here are the following filter conditions which are supported by the docker -

1 -f, --filter value    Filter output based on conditions provided (default [])
2                        - dangling=(true|false)
3                        - label=<key> or label=<key>=<value>
4                        - before=(<image-name>[:tag]|<image-id>|<image@digest>)
5                        - since=(<image-name>[:tag]|<image-id>|<image@digest>)
6                        - reference=(pattern of an image reference) 


2. Prune docker images, volumes, containers individually

In the Step we have seen how to delete or prune all the docker images, containers, volume, network at once using the command docker system prune but if you want granular control over the deletion of images, containers, volumes and network then docker offers command line support.

2.1 Prune/Delete only docker images

Here is an example command in which we are going to prune/Delete only the docker images leaving everything as is. -

Example -

1docker image prune -a --filter "until=24h"  

In the above example, we are going to delete prune all the images except for the last 24h.

So the only keyword which has changed from the Step is we started using the image instead of system

2.2 Prune/Delete only docker containers

Following the similar pattern as similar to the deletion of docker images, the command looks like -

1docker container prune -a --filter "name=my_image_name"

2.3 Prune/Delete only docker volumne

Prune and deletion of the docker volume is pretty much the same as those of previous steps -

1docker volune prune -a --filter "name=my_image_name"

or you can simply remove the filter from the prune command and it will delete all the volumes associated with it.

1docker volume prune 

2.4 Prune/Delete only networks

As we have seen in the previous prune and delete commands we can remove and delete the networks also in a similar fashion either by using the filters along with the docker prune command or without any filter.

Here are the examples of the docker prune networks -

1 docker newtwork prune -a --filter "until=12h"

Or you can use it without any filters.

1docker network prune 


3. Alias for removing dangling docker images

Well if you have gone through the previous steps(Step 1, Step 2) then you know how can manually delete/remove the unused images but is there one command which I can use to do all the removal and deletion at once?

So the answer is YES, you can use the following command to do that -

1docker rmi $(docker images --filter "dangling=true" -q --no-trunc)

The above command will remove all the unused docker images.



4. bahsrc function to remove docker images

Creating a bashrc function is a little advance and it requires you to be familiar with bash scripting so that you can create a bash function for deletion/removal of the docker images.

Here is the bash script which you can put or add inside your existing bash script -

1dcleanup(){
2    docker rm -v $(docker ps --filter status=exited -q 2>/dev/null) 2>/dev/null
3    docker rmi $(docker images --filter dangling=true -q 2>/dev/null) 2>/dev/null
4} 


5. docker-gc a simple script for docker garbage collection

There is one more open-source script docker-gc for docker garbage collection. It is not an official script from the docker community but it is being developed by Spotify and made available on GitHub as an open-source.

You can simply build the package as per the instruction provided by the Github readme page of docker-rs -

1sudo apt-get install git devscripts debhelper build-essential dh-make
2git clone https://github.com/spotify/docker-gc.git
3cd docker-gc
4debuild -us -uc -b 

Install the debian package -

1sudo dpkg -i ../docker-gc_0.1.0_all.deb

Install docker-gc into the /usr/sbin

1#!/bin/bash
2/usr/sbin/docker-gc 

Here are some examples of docker-gc command -

  1. Forcing deletion of images that have multiple tags
1FORCE_IMAGE_REMOVAL=1 docker-gc
  1. Forcing deletion of containers
1FORCE_CONTAINER_REMOVAL=1 docker-gc 
  1. Excluding Recently Exited Containers and Images From Garbage Collection
1GRACE_PERIOD_SECONDS=86400 docker-gc 


Hope this article helps you to target and remove the old unused images of docker.

Here are some references which I took for preparing this article -

  1. Stackoverflow - How to remove old and unused Docker images
  2. Docker - Prune unused Docker objects
  3. Docker-gc on git hub - docker-gc


Anchor-17-feb-2021