Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How does one remove a Docker image?
When working with Docker for extended periods, your local machine accumulates unused images from previous downloads, older versions, or testing purposes. These images consume significant disk space and can impact system performance. Additionally, dangling images (untagged images that are no longer referenced) contribute to storage bloat.
Docker provides several commands to remove images efficiently: docker image rm, docker rmi, and docker image prune. Each command offers various options for strategic image removal, helping you maintain a clean Docker environment.
Basic Image Removal Commands
The primary command for removing Docker images is:
$ docker image rm [OPTIONS] IMAGE [IMAGE...]
Common Options
--force− Forcefully remove images, even if containers are using them--no-prune− Skip deletion of untagged parent images
Important: You cannot remove images that have associated containers unless you use the --force option. Docker will return an error if you attempt this.
Example − Removing a Specific Image
$ docker image rm fedora:24
Checking for Associated Containers
Before removing an image, verify if any containers are using it:
$ docker ps -a
or
$ docker container ls -a
If containers exist, stop and remove them first:
$ docker stop <container-name> $ docker rm <container-name>
Or remove forcefully in one step:
$ docker rm -f <container-name>
Shorter Command Syntax
Docker provides a shorter alternative command:
$ docker rmi [OPTIONS] IMAGE [IMAGE...]
Removing Multiple Images
$ docker rmi -f myimage1 myimage2 myimage3
Pruning Unused Images
The docker image prune command removes dangling and unused images efficiently:
$ docker image prune [OPTIONS]
Prune Options
| Option | Description |
|---|---|
--all |
Remove all unused images, not just dangling ones |
--filter |
Apply filters to target specific images |
--force |
Skip confirmation prompt |
Remove All Unused Images
$ docker image prune --all
Removing All Images Simultaneously
To delete all Docker images at once, combine docker rmi with a subcommand:
$ docker rmi -f $(docker images -aq)
This command uses docker images -aq to list all image IDs quietly, then passes them to docker rmi -f for forced removal.
Conclusion
Regular cleanup of unused Docker images is essential for maintaining optimal system performance and storage efficiency. Use docker image rm for specific images, docker rmi for quick removal, and docker image prune for bulk cleanup of dangling images.
