ANONYMUS
ANONYMUS

Reputation: 665

How to remove dangling images in 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

Upvotes: 62

Views: 87803

Answers (5)

Matt
Matt

Reputation: 74680

Recent docker has added the image prune command so this task only requires a single invocation of docker

sudo docker image prune

For the sudo problem, 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)'

Upvotes: 81

Alexis.Rolland
Alexis.Rolland

Reputation: 6353

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

Upvotes: 8

SharpCoder
SharpCoder

Reputation: 19163

To make it work without sudo

docker rmi -f $(docker images -f "dangling=true" -q)

Upvotes: 11

hiren
hiren

Reputation: 1812

As @Matt mentioned, you are missing sudo in inner command. Below will work

sudo docker rmi $(sudo docker images -f "dangling=true" -q)

To get rid of using sudo everytime, give docker user permissions. Follow below steps

  1. Create the docker group.

    $ sudo groupadd docker
    
  2. Add your user to the docker group.

    $ sudo usermod -aG docker $USER
    

Upvotes: 2

Chris
Chris

Reputation: 1106

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

Upvotes: 36

Related Questions