How can I remove dangling Docker images? I tried
sudo docker rmi $(docker images -f "dangling=true" -q)
but it shows
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.35/images/json?filters=%7B%22dangling%22%3A%7B%22true%22%3Atrue%7D%7D: dial unix /var/run/docker.sock: connect: permission denied
Both docker
commands require sudo
otherwise the docker images
list will run first as your user.
sudo docker rmi $(sudo docker images -f "dangling=true" -q)
Sometimes sudo
doesn't work properly when run like this for the docker images
query and you need to run the entire command under a single sudo
:
sudo sh -c 'docker rmi $(docker images -f "dangling=true" -q)'
Recent docker has added images prune
so this task only requires a single invocation of docker
sudo docker image prune
Docker has a build-in command to cleanup dangling images.
sudo docker image prune
To cleanup unused images and dangling ones, use:
sudo docker image prune -a
To make it work without sudo
docker rmi -f $(docker images -f "dangling=true" -q)
Simply do
docker image prune
It will ask you to confirm
WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N]
Type y
, you're done... Or use -f
to not prompt for confirmation.
docker image prune -f
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With