Home > Software engineering >  Bash Script to delete specific docker images and containers
Bash Script to delete specific docker images and containers

Time:07-08

I work with docker images a lot and create the same image again and again after making changes to the code; so I get a lot of leftover images.

enter image description here

I want to write a bash script that can delete all the docker images with REPOSITORY == none or TAG == none. Sometimes the none docker image is being run by a container so it cannot be deleted; then i want the script to delete the container running the image firstly then remove the image.

Currently i am using docker rmi {imageId1} {imageId2} ... to delete the images. Your help will be appreciated; Thank you.

CodePudding user response:

Use bash command:

docker rmi $(docker images | grep none | tr -s ' ' | cut -d ' ' -f 3)

This command gets all image IDs that have tag none and then removes them, if the image is being used anywhere use --force flag to forcefully delete.

CodePudding user response:

You can also try :

docker rmi $(docker images -f "dangling=true" -q)
  • Related