Docker image command -Part-02
How To List Docker Images with different options

- List Docker images are to use following command.
$ docker images$ docker image ls
Note: Make sure that you have written “image” and not “images”.
2. Filtering Docker Image List
“docker images” command with the “–filter” followed by the filter in the form of a key-value tuple.
$ docker images --filter "<key>=<value>"
With the “–filter” option, you can specify the following keys :
- “reference” : that can be used in order to isolate images having a certain name or tag;
- “before” : to filter images created “before” a specific point in time;
- “since” : to filter images since a specific point in time (usually another image creation);
- “label” : if you used the LABEL instruction to create metadata for your image you can filter them later with this key
- “dangling” : in order to isolate images that are not used anymore.
If we want to isolate all the images starting with “Deb”, we would run the following command.
$ docker images --filter "reference=alpine"
$ docker images --filter "reference=alpine:3.12.8"

Dangling vs Unused image
- An unused image means that it has not been assigned or used in a container.
- Dangling images are not images that are not used by any container anymore.
- Dangling” images are created whenever you create a new version of a Docker image with the same image tag.
- In other words, a dangling image just means that you’ve created with no tag and show untagged and displays “
<none>.
3. List Unused Docker Images
Filter dangling docker images, you can append the “dangling=true” option.
$ docker images --filter "dangling=true"

4. Filtering Images on Date
List Docker images created before another image, you would run the following command.
$ docker images --filter "before=<image_name>"
Similarly, you can choose to list Docker images created after another image, with the “since” keyword.
$ docker images --filter "since=<image_name>"

5. Listing Only Image IDs
In order to list only image IDs on Docker, execute the below command.
$ docker images --quiet$ docker images -q

6. Removing images according to a pattern
You can find all the images that match a pattern using a combination of docker images
and grep
.
$ docker images -a | grep "alpine*"
You can delete them by using awk
to pass the IDs to docker rmi
.
$ docker images -a | grep "alpine*" | awk '{print $3}' | xargs docker rmi

7. Remove all images which are not in use or dangling.
$ docker rmi $(docker images -a -q)

8. Remove dangling and Unused images with the prune command
$ docker image prune

9. Remove all images that are not referenced by any existing container, not just the dangling ones.
$ docker image prune -a

10. Removing images using filters
For example, to remove all images that are created more than seven days (168 hours) ago, you would run:
$ docker image prune -a --filter "until=168h"