How To Remove Docker Images

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.

1

$sudo docker images

List 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 .

1

$sudo docker rmi ubuntu

Remove Specific Docker Image

As we can see from output all layers related with ubuntu image is deleted. In the following example we will provide the image id.

1

$sudo docker rmi3bee3060bfc8

Remove Specific Docker Image

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.

1

$docker images-fdangling=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 .

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.

1

$sudo docker images|grep"buntu"

List Docker Images With Specified Name and Regex

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 .

1

$docker images|grep"buntu"|awk'{print $1}'|xargs docker rmi

List All Docker Images

We can use -a option to list all docker images.

1

$docker images-a

List All Docker Images

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.