skip to Main Content

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.

2

Answers


  1. Chosen as BEST ANSWER

    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.


  2. You can also try :

    docker rmi $(docker images -f "dangling=true" -q)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search