How can I use “docker prune” to delete the unnecessary images in docker?

179    Asked by DanielBAKER in Devops , Asked on Nov 30, 2023

I am a DevOps engineer and I am currently managing a production environment with limited capacity. My docker registry has some irrelevant and unused images that are consuming a lot of space, therefore, I need to remove all these unused and irrelevant images to gain extra space. How can I utilize the “docker prune” command to remove all these docker images? 

Answered by Danilo Guidi

The docker prune all images or docker image prune command is a useful command in the scenario where you need to delete or remove unnecessary docker images in order to gain extra disk space. Here are the considerations and approaches you should follow during the whole process:-

Listing the Unused Images:- The first step should be listing the images that you want to delete by using the docker image prune. You can list them by using the command

  “docker image prune -a – dry-run”

Thus above command will provide you with the list of images that would be deleted without actually being deleted.

Filtering the images:-

If there are various and wide ranges of images in the list then you can filter the images by using a filter flag. Here is what you can do:-

  “docker image prune -a – filter “until=< duration>” – filter “label! = myapp”

Selective image deletion:- After selecting desired images from the list by performing the previous command, this is the time to delete those images. Instead of pruning all images at a time, you should consider selectively deleting the specific images by using their ID. Here is how you can do so:-

  “ docker emi ”

Join our DevOps course online to get more practical and efficient knowledge about the process of deleting unnecessary docker images.



Your Answer

Interviews

Parent Categories