DEV Community

Cover image for Find and remove docker image
Mohammad Faisal
Mohammad Faisal

Posted on

Find and remove docker image

Docker documentation mentions removing all stopped containers using command:

$ docker rm $(docker ps -a -q)
Enter fullscreen mode Exit fullscreen mode

but at times we want to first find existing containers and then remove them. This can be achieved when you filter and limit the result of docker ps command.

Limit the result of docker ps

You can either use the --filter, --last or --latest options or pipe the result of docker ps to grep command to filter the results for matching PATTERN.

Here is an example using grep:

$ docker ps -a | grep PATTERN | \
  awk '{print $1}' | xargs -r docker rm
Enter fullscreen mode Exit fullscreen mode

where the PATTERN could be part of container name, image name or any other details present in the result of docker ps command like mapped port number.

Feel free to share your feedback/opinion in comments.


Photo by Boxed Water Is Better on Unsplash

Top comments (0)