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 rmi with examples

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


What is docker rmi?

docker rmi is a Docker command used to remove Docker images from the local system. It helps in cleaning up unused images and managing disk space. You can remove individual images, multiple images, or dangling images (untagged).

Key Features:

  • Deletes both tagged and untagged images.
  • Supports removing multiple images at once.
  • Helps in freeing up disk space.

Basic Syntax

docker rmi [OPTIONS] IMAGE [IMAGE...]

Options:

  • -f, --force: Force remove an image, even if it’s in use by a stopped container.
  • --no-prune: Do not remove dangling parent images (default is to remove them).

Examples of docker rmi

1. Remove a Single Image by Tag

docker rmi nginx:latest

This removes the nginx:latest image.


2. Remove an Image by ID

docker rmi a1b2c3d4e5f6

You can use the image’s unique ID to remove it.


3. Remove Multiple Images

docker rmi nginx:latest alpine:3.14 python:3.9

This removes the specified images in one command.


4. Force Remove an Image (--force)

docker rmi -f my_image:latest

This forcefully removes my_image:latest, even if it’s in use by a stopped container.


5. Remove Dangling Images

Dangling images are untagged images that are no longer associated with any container.

docker rmi $(docker images -f "dangling=true" -q)

This removes all dangling images.


6. Remove Images Created Before a Specific Image

docker rmi $(docker images --filter "before=nginx:latest" -q)

This removes images created before the nginx:latest image.


7. Remove Images Above a Certain Size

List images larger than 500MB and remove them:

docker images --filter "dangling=false" --format "{{.Repository}}:{{.Tag}} {{.Size}}" | awk '$2+0 > 500 {print $1}' | xargs docker rmi

8. Remove All Images

docker rmi $(docker images -q)

This removes all images from the local system.

Note: Be careful with this command—it removes everything.


9. Remove an Image and Its Parent Images

By default, docker rmi removes parent images if they are no longer used by other images:

docker rmi my_custom_image:latest

10. Use docker rmi in a Cleanup Script

#!/bin/bash
# Remove all dangling images
docker rmi $(docker images -q -f "dangling=true")
echo "Cleaned up dangling images."

Use Cases for docker rmi

1. Freeing Up Disk Space

  • Regularly remove unused and dangling images to reclaim disk space.
  • Example: Remove large intermediate build images after building the final image.

2. Cleaning Up After Testing

  • Remove temporary or test images after they are no longer needed.
  • Example: Clean up development images after integration tests.

3. Managing Image Versions

  • Remove outdated versions of images to prevent clutter.
  • Example: Keep only the latest version of your application image and remove older ones.

4. Automating Image Cleanup

  • Integrate docker rmi in CI/CD pipelines to clean up old or untagged images.

5. Reducing Build Times

  • Remove intermediate build images to reduce storage usage and improve build performance.

6. Preparing for Disaster Recovery

  • Clean up unused images to simplify backup and recovery processes.

List of Common docker rmi Commands

CommandDescription
docker rmi nginx:latestRemove the nginx:latest image
docker rmi a1b2c3d4e5f6Remove an image by ID
docker rmi nginx:latest alpine:3.14 python:3.9Remove multiple images
docker rmi -f my_image:latestForcefully remove an image
docker rmi $(docker images -f "dangling=true" -q)Remove all dangling images
docker rmi $(docker images --filter "before=nginx:latest" -q)Remove images created before nginx:latest
docker rmi $(docker images -q)Remove all images

Best Practices for Using docker rmi:

  1. Check image dependencies before removing to avoid breaking services.
  2. Use --filter options to safely remove only specific types of images (e.g., dangling).
  3. Regularly clean up unused images to avoid filling up disk space.
  4. Be careful with docker rmi $(docker images -q), as it removes all images.
  5. Automate image cleanup in scripts to maintain a clean environment.

Common Errors and Solutions

  1. “Image is being used by a running container”
    → Stop and remove the container before removing the image: docker stop my_container && docker rm my_container && docker rmi my_image
  2. “No such image”
    → Ensure the image exists by checking with docker images.
  3. “Permission denied”
    → Run the command with sudo or check user permissions.
  4. “Failed to remove parent image”
    → The parent image is likely in use by another image. Use docker images --filter to identify dependencies.

Combining docker rmi with Other Commands

Clean Up Old Images Automatically

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

Clean Up Dangling Images

docker rmi $(docker images -f "dangling=true" -q)

Check Disk Usage Before and After Cleanup

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