How to remove all docker containers and images

Here is a simple but at the same time a useful .sh script which I use in all my projects for cleaning up my docker containers and unused docker images.

#!/usr/bin/env bash
# Remove all stopped containers
docker rm -v $(docker ps -a -q)
# Remove all untagged images
docker rmi $(docker images | grep "^<none>" | awk "{print $3}")

The very first command removes all containers. Note that docker ps command displays only running containers. If you add “-a”  param, docker ps command returns all stopped and running containers. Using “-q” param we ask docker to display container IDs only.

The second command removes all untagged (i.e. replaced by the image with the same version) images.

container-docker-blue-whale

How to remove docker containers and images?

Leave a comment