Introduction to Docker List Images
We have to use the docker image list command to list Docker images that are locally available on the host on which we are running the command. ‘ls’ and ‘images’ are aliases for this command, which means we can also run the command ‘docker image ls’ or ‘docker images’ to list the Docker images. It has options to manipulate the output of the list of images as per our requirements. For example, we can use the ‘–all’ or ‘-a’ to list all Docker images, including intermediate images hidden by default.
Table of Contents
Syntax
Below are the different syntaxes to list images in docker:
docker image list
docker image ls
docker images
Options:
-a or –all: It shows all images as intermediate images are hidden by default.
–digests: It Shows the digests of the image.
-f or –filter: It filters the images as per conditions specified
–format: This option allows for the prettier display of images using the Go template.
–no-trunc: If the output is truncated due to a small display, then we cannot use this option to truncate the output.
–quiet: It shows only image IDs.
image list --help
How to List Images in Docker?
It is very easy to list the Docker images; we only have to run any of the above-mentioned commands to list them. However, Docker CLI must be installed on the host where we are running the command. It takes the command from the terminal, converts it to JSON format, and makes an API call to the Docker daemon, and the Docker daemon responds with the list of the images available on the host. Understanding how different image types work, whether it’s for software or optimizing social media image sizes, can help you better navigate various tools. For a more comprehensive understanding of these processes and to learn more about the broader context, consider taking a DevOps Course, which covers essential tools and practices, including Docker, CI/CD, and infrastructure automation.
Examples of Docker List Images
Let’s understand the command and its options using examples
Example #1
List images available on the host, excluding intermediate images.
We can use any of the below commands to list Docker images other than intermediate images:
docker image list
docker image ls
docker images
Example #2
List all images, including intermediate images.
Let’s build a Docker image using the below Dockerfile:
cat Dockerfile
FROM ubuntu
RUN apt-get update && apt-get install
nginx -y
If we check the output carefully, we can see few images are used, as highlighted in the below snapshot:
docker build -t custom-nginx:v1.
Explanation: – In the above snapshot, the first highlighted numeric ID is the image ID of Ubuntu, the second highlighted is the image ID of the intermediate image, and the third one is the image ID of the final image. Intermediate images do not have a repository and tag; listing the images shows <none> under the Repository and Tag column.
We can verify this by running the ‘docker image list –all’ command or using “-a” as below:
docker image list --all
docker image list -a
Example #3 – Show the hash of Docker images
We can use the ‘–digests’ command to output the digests of the images as below:
docker image list --digests
Explanation: In the above snapshot, we can see one more column added of DIGEST in the command’s output, and the recently created Docker image has no DIGEST as it is not signed.
We can even check the digest of any single Docker image as below:
docker image list ubuntu --digests
Example #4 – Filter the Docker images
We can filter out images using the “–filter” or “-f” option. For instance, we can bypass the ‘dangling=true’ condition to filter out the dangling image.
docker image list --filter danling=true
We can also use the ‘before’ or ‘since’ condition to list the images pulled before the specified image or after the specified image as below:
docker image ls
docker image ls --filter before=ubuntu
docker image ls --filter since=ubuntu
Example #5
Format the output of the ‘docker image list’ command.
We can use the ‘–format’ option to manipulate the output using the Go template; for example, if we only want to list the name of the repository with the tag, we can use the ‘–format’ option as below:
docker images --format "{{.Repository}}:{{.Tag}}"
- We can see that the column heading is missing in the above snapshot; we can use the ‘table’ keyword in the Go template to show the column header as below:
docker images --format "table {{.ID}}\t{{.Repository}}:{{.Tag}}\t{{.Size}}"
Example #6
We want to show all output of the Docker images without truncating them. It can be done by using the “–no-trunc” option as shown below:
docker images --digests
docker images --digests –no-trunc
Explanation: – In the above snapshot, we can see that there is only one DIGEST shown if the ‘–no-trunc” is not used; however, when we use the “–no-trunc” option, we get one more digest that is truncated in the previous example.
Example #7
Only list the image ID. It is possible in two ways, the first one is by using the ‘–quiet’ or the “-q” option, and the second one is by using the “–format” option by using the Go template, as explained earlier, however, the ‘–quiet’ option is easy to use and fast.
docker images --quiet
Conclusion
The shortest and easiest command to list the Docker images is the ‘docker images. The parent command of this command is the ‘docker image’. We mostly use the “–format’ option to automate as it provides us output how we need it.
FAQs
Q1. How can I filter the Docker images by repository name?
Ans: To filter the Docker images by repository name, you can use the following command:
docker images <repository_name>
Q2. How can I filter the Docker images by tag?
Ans: To filter the Docker images by tag, you can use the following command:
docker images <repository_name>:<tag>
Q3. Why is listing Docker images important?
Ans: Listing Docker images is crucial for managing containers. It helps users track available images, identify those to run as containers and monitor disk usage to optimize storage.
Q4. How can I remove Docker images using this command?
Ans: To remove Docker images, use the “docker rmi” command followed by the image ID or name. For example, “docker rmi my-image” would remove an image named “my-image.”
Recommended Articles
We hope that this EDUCBA information on “Docker List Images” was beneficial to you. You can view EDUCBA’s recommended articles for more information.