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
Command | Description |
---|---|
docker rmi nginx:latest | Remove the nginx:latest image |
docker rmi a1b2c3d4e5f6 | Remove an image by ID |
docker rmi nginx:latest alpine:3.14 python:3.9 | Remove multiple images |
docker rmi -f my_image:latest | Forcefully 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
:
- Check image dependencies before removing to avoid breaking services.
- Use
--filter
options to safely remove only specific types of images (e.g., dangling). - Regularly clean up unused images to avoid filling up disk space.
- Be careful with
docker rmi $(docker images -q)
, as it removes all images. - Automate image cleanup in scripts to maintain a clean environment.
Common Errors and Solutions
- “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
- “No such image”
→ Ensure the image exists by checking withdocker images
. - “Permission denied”
→ Run the command withsudo
or check user permissions. - “Failed to remove parent image”
→ The parent image is likely in use by another image. Usedocker 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
I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I am working at Cotocus. I blog tech insights at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at I reviewed , and SEO strategies at Wizbrand.
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at PINTEREST
Rajesh Kumar at QUORA
Rajesh Kumar at WIZBRAND