We’ll be covering managing Docker images, mainly briefing about the ways to list Docker images and get the required information and then building upon that learning to remove one or more images in efficient way. So let’s get started.

Listing Docker Images

To list pulled Docker images, use: As with other docker commands, it supports images command with multiple options. If you run docker images without any options, it’ll show you the top-level images like docker image ls, their repository and tags and their size on disk. These images have intermediate layers that increase usability, speed up the build process, and reduces disk usage which isn’t shown by the above command. The SIZE is the combined space taken up by the image and all its parent images. If you save the contents of the image as a Tar file when you docker save an image, it’ll equal this listed size. An image gets listed multiple times if it has multiple repository names or tags though the single image identified by its IMAGE ID uses up the SIZE listed only once.

List most recent images

List images by Repository and Tag

To list all images that belong to a particular repository, specify the repository name like: You can additionally specify the repository name with a tag for a more filtered output:

List full-length image IDs

To list all images without truncating the IMAGE ID, use:

List Image Digests

Docker images that use v2 or later format have a content-addressable identifier known as a digest. To list image digest values, use –digest flag as: With a 2.0 registry, you can use these digests with push, pull, create, run and rmi commands. This also works with FROM command in a Dockerfile.

Filtering the Output

docker command supports filtering with images by using –filter flag. The currently supported filters are:

dangling (boolean – true or false) label (label= or label==) before ([:],  or image@digest) – filter images created before given id or references since ([:],  or image@digest) – filter images created since given id or references reference (pattern of an image reference) – filter images whose reference matches the specified pattern

Further, you can use multiple filters by combining them like: Example:

Formatting the Output

docker images support formatting output that may be needed for nesting with other commands, scripting, or otherwise. These are the supported placeholders for the –format flag: As an example, the below command prints the output without headers and outputs the ID and Repository separated by colon ( : ) for all images: Or to list all images with their repository and tag in a table format, you can use:

Removing Docker Images

To remove one or more Docker images from the system, we use: To remove a single image, simply specify the image name: Or to remove multiple images, specify multiple image names separated by space:

Force Removal

There may be containers running that are using the images which you’re trying to delete. In such cases, Docker will issue you a warning when you try to delete a referenced image. You can stop the associated container first and then retry or else use -f flag which forces the image removal (be careful).

Prune Images

There are certain situations where unused images are consuming disk space or you just need a cleanup of old dangling images. You can clean such unused images by using: Example: If you wish to clean up all images which aren’t used by any containers as well, use -a flag: You can also use –force or -f flag to proceed to clean without confirmation or use –filter flag to provide filter values (e.g. ‘until=’) to the prune command. Example: Or

Using rmi Command

You can also use rmi command with docker to remove images. It removes (and un-tags) one or more images from the Docker node. If an image has multiple tags, using this command with the tag as a parameter only removes the tag. If the tag is the only one for the image, both the image and the tag are removed. This command does not remove images from a registry. Also, you cannot remove an image of a running container unless you use the -f option as with docker image rm command. Example: You can also use –no-prune to specify not to delete untagged parents.

Stop Container and Remove Images

On many occasions, you may need to stop all containers and remove all associated images. You can do that simply with: The above command will stop and remove all running containers forcefully. Then we can proceed to remove the linked images by using:

Summary

Docker is a versatile tool that is an essential part of today’s DevOps engineer’s arsenal and is part of other buzzing technologies like Kubernetes. Docker image management is one of the essential parts of managing and troubleshooting a Docker deployment. You should now have a basic idea of the capabilities that docker CLI command offers with its commands like image, images and rmi. Their flags offer further customization and offer advanced filtering and customization options that can help in automation and advanced usage. Use docker [COMMAND] help for further details about available options and related help topics.