Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove dangling images in Docker

Tags:

docker

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

like image 430
ANONYMUS Avatar asked Feb 28 '18 04:02

ANONYMUS


4 Answers

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
like image 63
Matt Avatar answered Oct 02 '22 23:10

Matt


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
like image 35
Chris Avatar answered Oct 02 '22 23:10

Chris


To make it work without sudo

docker rmi -f $(docker images -f "dangling=true" -q)
like image 34
SharpCoder Avatar answered Oct 02 '22 23:10

SharpCoder


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
like image 3
Alexis.Rolland Avatar answered Oct 03 '22 01:10

Alexis.Rolland