Sometimes, there are some untagged images left behind after failed builds. In order to get rid of those, this script can be used.
#!/bin/bash
docker rm $(docker ps -aq)
docker rmi $(docker images | grep "^<none>" | awk '{print $3}')
Yes, for instance if a container is run based on a working image and then stopped by the user, your first command will delete this container, which is not what you want as you only want to remove containers remaining of failed builds.
Hi,
One option that can be used during the image building period is --force-rm
which will remove the intermediate containers despite the image is successfully built or not.
--force-rm Always remove intermediate containers
So we dont need to remove unused intermediate containers
Hi! The last command could be:
docker rmi $(docker images -f "dangling=true" -q)
This will remove all already existing containers as well, not only intermediate ones.