Docker images contain instructions to manage and run Docker containers. Users can create as many docker images as they want. But these Docker images can take up most of the storage in the Docker engine. Therefore, developers are occasionally required to remove the unused Docker image to keep the Docker engine clean.
This write-up will demonstrate how to remove Docker images.
How to Remove Docker Images?
To remove the Docker images, users can utilize any of the given methods:
- Method 1: How to Remove Docker Image Using “docker rmi” Command?
- Method 2: How to Remove Docker Image Using the “docker image prune” Command?
Method 1: How to Remove Docker Image Using “docker rmi” Command?
The “docker rmi” command is one of the most powerful Docker commands. It is used to remove or untag the Docker images. If the image container has more than one tag, then this command will remove tags only. However, if the image has only one tag, then the “docker rmi” command will remove the tag and the Docker image.
List Docker Image
Docker images are removed by image id or image name. To check the image id or image name, list down all Docker images using the “docker images” command:
Remove Docker Image by Id
In order to remove the Docker image by id, utilize the “docker rmi <image-id>” command:
Remove Docker Image by Name
Users can also remove the Docker image by image name using the given command:
Remove Docker Image Associated With Any Container
If the Docker image is associated with any Docker containers, then users can face difficulty while removing the Docker image using the simple “docker rmi” command. They may get an error that the container is using an image reference as shown below:
To remove a Docker image associated with any Docker container, pass the “-f” option along with the “docker rmi” command. The “-f” option will forcefully remove the Docker image:
It can be observed that the image is removed:
Method 2: How to Remove Docker Image Using “docker image prune” Command?
The Docker “image prune” is utilized for removing and cleaning up Docker images. This command removes only dangling images that are not associated with any container or tag.
To remove dangling images, utilize the given command:
In order to remove all unused, stopped as well as dangling images, utilize the “docker image prune” command along with the “-a” and “-f” options:
For verification, again list down all Docker images:
It can be noticed that we have successfully removed all dangling and stopped images.
Conclusion
To remove Docker images, users can either use the “docker image prune” or “docker rmi” command. The Docker “rmi” command is utilized to remove images as well as tags. To remove the Docker image using the “rmi” command, use the “docker rmi <image-name>” command. In order to remove all dangling or unused images, utilize the “docker image prune” command. This write-up has demonstrated how to remove Docker images.