Docker images are considered as the snapshot or main template of Docker containers. These include all the essential instructions such as commands to install dependencies, path to source code, and container base image to build the container. These are basically guides that instruct the containers on how to encapsulate the application.
However, as images are major components of the Docker development environment, these may take much of system resources such as CPU and memory which can harm the system performance. To optimize the system performance and to clean up the disk space, users are required to remove the dangling, unused images and sometimes all Docker images.
In this article, we will illustrate how to remove Docker images through the following outline:
- How to Remove Dangling Docker Images?
- How to Remove Unused and Used Docker Images?
- How to Remove All Docker Images?
- Method 1: Remove All Docker Images Using “docker rmi” Command
- Method 2: Remove All Docker Images Using “docker image prune” Command
- Conclusion
How to Remove Dangling Docker Images?
Dangling images are unnamed and untagged images that are generated while building the Docker image. These images are not used by any container and serve no purpose in the Docker development environment. But these images consume more of the disk space which can effectively affect the system performance. These images should be removed to clean up disk space.
To delete all dangling images from Docker, follow these steps.
Step 1: List Down Docker Images
First, view all images through the “docker images” command. Here, the “-a” option is utilized to show all docker images that are placed on disk:
To filter out only untagged or dangling Docker images, utilize the “docker image list” command. The “–filter” option filtered the images based on the provided command. For instance, we have filtered the dangling images only:
Step 2: Remove All Dangling Images
To remove all the dangling images from Docker, utilize the “docker image prune” command:
Step 3: Verification
For verification, again list down and filter the dangling images only:
The below output shows that we have removed all the dangling images successfully:
How to Remove Unused and Used Docker Images From Docker?
The unused images are those that are not associated with or used by any container. Unlike dangling images, these images can be used by Docker containers. On the other hand, the used images are those that are connected or being used by at least one Docker container.
In order to remove unused or used Docker images, go through the below instructions.
Step 1: List Down All Docker Images
Images from Docker can be removed using an image ID or name. To remove the Docker image, first, list down all the Docker images and note the image ID or image name:
For illustration, we will use image ID to remove the unused image and image name to remove the used image:
Step 2: Remove Unused Docker Image
Now, use the “docker rmi <image-id>” command to remove the unused image that is not linked with any container:
The below output shows that we have effectively deleted the unused Docker image:
Note: If the image is being used by any container, it can not be deleted directly and encounters the error as shown below:
To resolve the stated error or to remove the image that is linked with at least one container, users are required to add the “-f” option along with the “docker rmi” command as done in the below step.
Step 3: Remove the Used Docker Image
Now, remove the image that is linked or being used by any container through the following command. For instance, we have used the image name to remove the image. The “-f” option removes the image forcefully:
The given output indicates that we have successfully removed the used Docker image:
How to Remove all Docker Images?
Sometimes, Docker users are required to declutter the Docker environment to optimize the system performance. For this reason, they may required to remove all the Docker images as these images take more disk space. To remove all images from Docker, the user can utilize the following commands:
- “docker rmi”
- “docker image prune”
Method 1: Remove All Docker Images Using “docker rmi” Command
To remove all Docker images through the “docker rmi” command, there are a further couple of terminal-based command formats used on Windows. Such as the Command Prompt can run the “docker rmi” command in a loop to remove all images. In contrast, other terminals like Powershell, linux based terminals can remove all the Docker images directly.
Remove All Images From Command Prompt
To remove all images using the command prompt, utilize the below command:
In the above command, the “docker rmi” command will execute in the loop to remove all the Docker images:
Remove All Images From PowerShell or Linux Terminal
To utilize the “docker rmi” command to remove all the Docker images on Powershell or linux based terminal such as Ubuntu, utilize the following command:
In the above command, the “-f” option is used to remove images forcefully, the “-a” option removes all the Docker images, and “-q” is utilized to remove the images by ID:
Method 2: Remove All Docker Images Using the “docker image prune” Command
To remove or prune all the images from Docker, utilize the “docker image prune” command. Also, use the “-a” option to remove all dangling, used, and unused Docker images:
For verification, again list down the images. The below output shows that we have successfully removed all Docker images:
That is all about removing all images from Docker.
Conclusion
To remove all the Docker images from the system, the user can either use the “docker rmi -f $(sudo docker images -aq)” command or the “docker image prune -a” command. In this blog, we have demonstrated how to remove dangling, unused, used, and all Docker images from the system.