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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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.