I\'ve the following images:
alex@alexvps:~$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
&
The most compact version of a command to remove all untagged images is:
docker rmi $(docker images | grep "^<none>" | awk '{print $"3"}')
To delete some Docker image you must execute the following command:
$ docker rmi <docker_image_id>
So, to delete all Docker images you can execute the following command:
$ docker rmi $(docker images -q)
Now, if you want delete all Docker images (including images that are in use), you can add the flag -f
, for example:
$ docker rmi -f $(docker images -q)
Remove all the containers
docker ps -q -a | xargs docker rm
Force remove all the Docker images
docker rmi -f $(docker images -f dangling=true -q)
The image could be currently used by a running container, so you first have to stop and remove the container(s).
docker stop <container-name>
docker rm <container-id>
Then you could try deleting the image:
docker rmi <image-id>
You must be sure that this image doesn't depend on other images (otherwise you must delete them first).
I had a strange case in which I had no more containers still alive (docker ps -a
returned nothing) but I couldn't manage to delete the image and its image-dependency.
To solve these special cases you could force the image removal with this:
docker rmi -f <image-id>
In Bash:
for i in `sudo docker images|grep \<none\>|awk '{print $3}'`;do sudo docker rmi $i;done
This will remove all images with name "<none>". I found those images redundant.
If you want to automatically/periodically clean up exited containers and remove images and volumes that aren't in use by a running container you can download the Docker image meltwater/docker-cleanup
.
That way you don't need to go clean it up by hand.
Just run:
docker run -d -v /var/run/docker.sock:/var/run/docker.sock:rw -v /var/lib/docker:/var/lib/docker:rw --restart=unless-stopped meltwater/docker-cleanup:latest
It will run every 30 minutes (or however long you set it using DELAY_TIME=1800 option) and clean up exited containers and images.
More details: https://github.com/meltwater/docker-cleanup/blob/master/README.md