Docker uses images in order to start new container. New containers will use existing images and adds new layers for the customized parts. In this tutorial we will look how to manage docker images.
List Docker Images
Before removing Docker Images we generally need to list these images. We will use list
command in order to list currently existing Docker Images.
$ sudo docker images

Remove Specific Docker Image
The first example to remove Docker image is removing single image. We need to specify the image with its name or image id. We will use rmi
command which is shortcut of remove image. In this example we will remove the image named ubuntu
.
$ sudo docker rmi ubuntu

As we can see from output all layers related with ubuntu image is deleted. In the following example we will provide the image id.
$ sudo docker rmi 3bee3060bfc8

List Dangling Docker Images
As we know docker images consist of different layers. These layers are used by multiple or single images. Sometimes layers can be unrelated with any image. We can list these unused and unrelated docker image layers with -f
and dangling=true
options.
$ docker images -f dangling=true
Remove Dangling Docker Images
We can remove dangling images by one by one typing the docker image id but this is not a feasible solution. We can list the dangling docker images id’s with -q
parameter .
$ docker rmi $(docker images -f dangling=true -q)
List Docker Images With Specified Name and Regex
In enterprise environments there will be a lot of images we need to cope. We can use pattern search or regex with grep
tool to filter and list specific named images. In this example we will list images those contains buntu
in their names.
$ sudo docker images | grep "buntu"

Remove Docker Images With Specified Name and Regex
We can use rmi
to remove docker images those have specified names or patterns. In this example we will remove images those names have buntu
.
$ docker images | grep "buntu" | awk '{print $1}' | xargs docker rmi
List All Docker Images
We can use -a
option to list all docker images.
$ docker images -a

Remove All Docker Images
We can also remove all docker images just using similar technique like previous examples. We will provide the image id’s to the rmi
command.
$ docker rmi $(docker images -a -q)