remove-docker-containers.md
https://gist.github.com/ngpestelos/4fc2e31e19f86b9cf10b
Delete all containers
$ docker ps -q -a | xargs docker rm
-q prints only the container IDs -a prints all containers
Notice that it uses xargs to issue a remove container command for each container ID
Delete all untagged images
$ docker rmi $(docker images | grep '^<none>'' | awk '{print $3})
awk must use a single quote (this filters all image IDs)
docker rmi $(docker images | grep "^<none>" | awk '{print $3}')
I have read it somewhere
It is working , special for deleting <none> images
$ docker-machine stop default
$ docker-machine start default
$ docker images -q | xargs docker rmi
or
$ docker-machine restart default
$ docker images -q | xargs docker rmi
Another way of removing all images is:
docker images -q | xargs docker rmi
If images have depended children, forced removal is via the -f flag:
docker images -q | xargs docker rmi -f
I use this script
#!/bin/bash
# Delete all stopped containers
docker rm $( docker ps -q -f status=exited)
# Delete all dangling (unused) images
docker rmi $( docker images -q -f dangling=true)
Edit
xargs with --no-run-if-empty is even better as it does cleanly handle the case when there is nothing to be removed.
#!/bin/bash
# Delete all stopped containers
docker ps -q -f status=exited | xargs --no-run-if-empty docker rm
# Delete all dangling (unused) images
docker images -q -f dangling=true | xargs --no-run-if-empty docker rmi
Tested on 1.12.3 (Windows and Linux (centos 7))
Linux
Containers
docker rm $(docker ps -a -q)
Images
docker rmi $(docker images -q)
Windows
Containers
FOR /f "tokens=*" %i IN ('docker ps -a -q') DO docker rm %i
Images
FOR /f "tokens=*" %i IN ('docker images -q -f "dangling=true"') DO docker rmi %i
docker container prune # Remove all stopped containers
docker volume prune # Remove all unused volumes
docker image prune # Remove unused images
docker system prune # All of the above, in this order: containers, volumes, images
docker system df # Show docker disk usage, including space reclaimable by pruning
网友评论