skip to Main Content

I want to prune images based on image/repo name , lets say i have unused images named after postgres, postgres:v2, nginx and nginx:v2 now i want to prune all images based on image name that is postgres not nginx

docker image prune -a -f | grep postgres

but pruning all images i.e postgres and nginx , Thanks in advance

2

Answers


  1. There are probably multiple ways to do it, but I have a defined function that accepts a string as an argument, and deletes all images that have this partial string in their name:

    $ drmi() { docker images --format "{{.Repository}}:{{.Tag}}" |grep $1 | xargs docker rmi -f; }
    

    Then you can do:

    $ drmi postgres
    

    In reality, I am using a bash alias manager (that I developed, full disclosure) and have many aliases for docker, so I can do things like:

    $ d rmi postgres
    $ d size    # to show images sorted by size, so you can find delete candidates
    

    Here is how I define the d rmi function, along with other useful functions.

    Login or Signup to reply.
  2. docker image prune doesn’t support passing the image name with the command.

    You can use the docker rmi command instead on the output of docker images command to delete specific docker images:

    docker rmi $(docker images postgress -q)
    docker rmi -f $(docker images postgress -q)  # forceful deletion
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search