Using the "docker images" Command (with examples)
The “docker images” command is used to manage Docker images. It provides functionality to list, filter, and sort images. In this article, we will explore different use cases of the command with code examples.
1: List all Docker images
docker images
Motivation: This command is used to display a list of all Docker images available on the system.
Explanation: The “docker images” command without any arguments lists all the Docker images. It shows the repository, tag, image ID, created time, and size of each image.
Example Output:
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 7e0aa2d69a15 10 days ago 72.9MB
nginx latest b4af90e1aa04 2 weeks ago 126MB
2: List all Docker images including intermediates
docker images --all
Motivation: Use this command to display both the parent and child images (intermediates) of the Docker images.
Explanation: The “–all” flag is used to include intermediate image layers in the output. By default, Docker only displays the top-level images (images with no children).
Example Output:
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 7e0aa2d69a15 10 days ago 72.9MB
nginx latest b4af90e1aa04 2 weeks ago 126MB
<none> <none> 9b8bca97734f 2 weeks ago 126MB
3: List the output in quiet mode (only numeric IDs)
docker images --quiet
Motivation: This command is useful when you only need the image IDs (numeric) of the Docker images for scripting or further processing.
Explanation: The “–quiet” flag suppresses all the additional information and only displays the image IDs. This can be helpful when you want to perform operations on images using the IDs.
Example Output:
7e0aa2d69a15
b4af90e1aa04
9b8bca97734f
4: List all Docker images not used by any container
docker images --filter dangling=true
Motivation: This command allows you to find images that are no longer used by any container. Removing such dangling images can free up disk space.
Explanation: The “–filter” flag is used to apply filters to the list of images. In this case, we are filtering images with the “dangling” property set to true, indicating they are not associated with any container.
Example Output:
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> 9b8bca97734f 2 weeks ago 126MB
5: List images that contain a substring in their name
docker images "*name*"
Motivation: This command helps to filter images based on a specific substring in their names.
Explanation: By specifying a string enclosed in asterisks, we can filter images based on the substring in their repository or tag name. This can be useful when searching for images with specific keywords.
Example Output:
REPOSITORY TAG IMAGE ID CREATED SIZE
wordpress:latest latest c3c687477f01 2 weeks ago 445MB
6: Sort images by size
docker images --format ".ID\t.Size\t.Repository:.Tag" | sort -k 2 -h
Motivation: Sorting images by size can help identify large images that may be taking up significant disk space. This enables efficient management of images.
Explanation: The “–format” flag is used to customize the output format. Here, we specify the format to display image ID, size, and repository with tag. The output is then piped to the “sort” command to sort the images based on the second column (size) in human-readable format.
Example Output:
ID Size Repository:Tag
9b8bca97734f 126MB <none>:<none>
nginx 126MB nginx:latest
ubuntu 72.9MB ubuntu:latest
Conclusion
In this article, we explored different use cases of the “docker images” command, including listing all Docker images, including intermediates, filtering images, and sorting them by size. These examples can help you effectively manage and work with Docker images in your environment.