Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

Docker commands Guide – docker images with examples

Here’s a complete tutorial on docker images, covering what it does, examples, and use cases.


What is docker images?

docker images is a Docker command that lists all available images on the local machine. It provides details like the repository name, tag, image ID, size, and the creation date of each image.

Key Features:

  • Lists all locally available Docker images.
  • Supports filtering, sorting, and formatting output.
  • Helps manage and inspect images for size, age, and tags.

Basic Syntax

docker images [OPTIONS] [REPOSITORY[:TAG]]

Common Options:

  • -a, --all: Show all images, including intermediate layers.
  • --digests: Show image digests (SHA256 hash of the image).
  • --format: Customize the output using Go templates (e.g., {{.Repository}}).
  • --filter, -f: Filter images based on criteria like dangling, before, since, and label.
  • --no-trunc: Show full image IDs and digests without truncation.

Examples of docker images

1. List All Docker Images

docker images

Example Output:

REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
nginx        latest    e21c333399e4   1 day ago     22.6MB
python       3.9       5b6b7b8932de   2 days ago    885MB

2. Show All Images, Including Intermediate Layers

docker images -a

This lists all images, including intermediate layers used during image builds.


3. Show Image Digests

docker images --digests

Example Output:

REPOSITORY   TAG       DIGEST                                                                    IMAGE ID       SIZE
nginx        latest    sha256:c95f7e6fdf92bc932f3cfad7bb9275d53f3af7c7e69c2c27dc28b7e60a7c5b61   e21c333399e4   22.6MB

4. List Images for a Specific Repository

docker images nginx

This lists all available tags for the nginx image.


5. List a Specific Image by Tag

docker images nginx:1.21

Shows only the image with the tag 1.21 for nginx.


6. Filter Images by Dangling Status

docker images --filter "dangling=true"

This lists dangling images (untagged images that are no longer associated with a container).


7. Filter Images by Creation Date (before and since)

docker images --filter "before=nginx:latest"

Lists images created before the nginx:latest image.

docker images --filter "since=python:3.9"

Lists images created after the python:3.9 image.


8. Use Custom Formatting for Output

docker images --format "Repository: {{.Repository}}, Tag: {{.Tag}}, Size: {{.Size}}"

Example Output:

Repository: nginx, Tag: latest, Size: 22.6MB
Repository: python, Tag: 3.9, Size: 885MB

9. List Only Image IDs

docker images -q

Example Output:

e21c333399e4
5b6b7b8932de

10. List Images and Remove Dangling Ones

docker images --filter "dangling=true" -q | xargs docker rmi

This removes all dangling images.


11. Use docker images in a Shell Script

#!/bin/bash
for image in $(docker images -q); do
  echo "Found image: $image"
done

Use Cases for docker images

1. Image Management and Maintenance

  • List all images to monitor image size and manage disk space.
  • Example: Identify large images and remove them if not needed.

2. Debugging and Troubleshooting

  • Use docker images to verify image availability before running containers.
  • Example: Ensure the correct version of an image is available for a specific service.

3. Continuous Integration and Deployment (CI/CD)

  • Integrate docker images in CI/CD pipelines to check for the latest images.
  • Example: Use it to determine if a new image needs to be built.

4. Security and Auditing

  • List images and verify that they are up-to-date and secure.
  • Example: Regularly check for old or vulnerable images.

5. Automated Image Cleanup

  • Use docker images with docker rmi to automate image cleanup scripts.

6. Track Image Changes and Updates

  • Monitor different image tags for a repository to track updates.

List of Common docker images Commands

CommandDescription
docker imagesList all Docker images
docker images -aShow all images, including intermediate layers
docker images --digestsShow image digests
docker images nginxList all images for the nginx repository
docker images --filter "dangling=true"List all dangling images
docker images --filter "before=nginx:latest"List images created before nginx:latest
docker images -qShow only image IDs
docker images --format "{{.Repository}}"Customize the output format

Best Practices for Using docker images:

  1. Regularly monitor and clean up images to save disk space.
  2. Use filters (--filter) to focus on specific images (dangling, older than a given image, etc.).
  3. Tag images properly for easy identification (e.g., my_app:dev, my_app:prod).
  4. Combine with docker rmi for automated cleanup scripts.
  5. Track image sizes and optimize Dockerfiles to reduce image bloat.

Common Errors and Solutions

  1. “No such image”
    → Ensure the image exists and is properly tagged. Use docker images to verify.
  2. “Image size too large”
    → Use multi-stage builds or switch to a smaller base image (alpine) to reduce size.
  3. “Dangling images taking too much space”
    → Use docker images --filter "dangling=true" to identify and remove them.

Combining docker images with Other Commands

Remove Old Images

docker images --filter "before=my_app:1.0" -q | xargs docker rmi

Check Disk Usage

docker system df

Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.

0
Would love your thoughts, please comment.x
()
x