I wrote this four years ago, so instead use this command:
$ docker rmi $(docker images -q -f dangling=true)
'docker system prune' doesn't remove images on windows
You can try out below script, i am using that below
#!/bin/bash
timestamp=$(date +%Y%m%d_%H%M%S)
log_path="`pwd`"
filename=docker_cleanup_$timestamp.log
log=$log_path/$filename
docker_space_before(){
CURRENTSPACE=`docker system df`
echo "Current Docker Space:" >> $log
echo $CURRENTSPACE >>$log
}
docker_find (){
echo "#####################################################################" >> $log
echo "Finding images" >> $log
echo "#####################################################################" >> $log
REMOVEIMAGES=`docker images | grep " [days|months|weeks]* ago" | awk '{print $3}'`
echo "Listing images that needs to be cleaned up" >> $log
echo $REMOVEIMAGES >>$log
}
docker_cleanup(){
echo "#####################################################################" >> $log
echo "Cleaning images" >> $log
echo "#####################################################################" >> $log
docker rmi ${REMOVEIMAGES}
}
docker_space_after(){
CURRENTSPACE=`docker system df`
echo "Current Docker Space, after clean up:" >> $log
echo $CURRENTSPACE >>$log
}
docker_space_before
docker_find
docker_cleanup
docker_space_after
'docker system prune' doesn't remove images on windows
same here, have you found any solution?
For Windows, what's more:
Stop and remove by image name =>
FOR /f "tokens=*" %i IN ('docker ps -a --filter "ancestor=ImageNameHere" -q') DO docker stop %i && docker rm %i
...
(don't forget to change %i
to %%i
in batch file)
the best thing to control and clean up unused containers which still running are to label them on docker run and use few command lines through crontab:
1'st one to kill docker containers creates x time ago and with labe x:
docker ps -a --filter "label=<label_name>" | grep 'x period crated ago' | awk '{ print $1 }' | xargs -I {} docker rm {} -f
2'nd one clean up all unused images which also have tag "none":
docker rmi $(docker images --filter "dangling=true" -q --no-trunc) 2>/dev/null
Then your environment should be always clean :) Cheers....
docker ps -a -q | % { docker rm $_ }
docker images -q | % { docker rmi $_ }
thank you so much
This worked for me on Ubuntu 16.04
Before deleting all the containers, force stop them:
docker ps -q -a | xargs docker stop
Then delete the containers using:
docker ps -q -a | xargs docker rm
Now delete all the dangling images using:
docker rmi $(docker images | grep “^<none>” | awk ‘{print $3}’)