After a time, Docker containers and images build up and can eat disk space. Failed docker build
invocations can leave a lot of artifacts behind.
The following commands can help. Many of them say ‘not all containers could be removed’; this has to do with the header from docker ps
. Usually this can be ignored.
Caveat: Docker images cannot be removed if they are in use by a container.
Remove Ended Containers Which Match a Pattern
1 2 |
docker ps -a|grep PATTERN|awk '{print $1}'|xargs docker rm |
Remove All Containers Which Match a Pattern
1 2 |
docker ps -a|grep PATTERN|awk '{print $1}'|xargs docker rm -f |
Remove Images Matching a Pattern
1 2 |
docker images|grep consul|awk '{print $1}'|xargs docker rmi |
Remove Incomplete Images
1 2 |
docker images |grep '<none'|awk '{print $3}'|xargs docker rmi |
Remove all containers except those currently running
1 2 |
docker ps -a|grep -v 'Up '|awk '{print $1}'|xargs docker rm |