FIX - Permission Error When Removing Multiple Docker Images
[Docker, Fix]
In yesterday’s post, “Removing Multiple Docker Images (Improved)”, we looked at how to remove multiple Docker images in a single command.
The command is as follows:
docker images -f dangling=true -q | xargs docker rmi -f
In this post, we will look at how to tackle the following error:

I got this error on a Linux box, which was strange because the same command works perfectly on macOS.
The difference is that on macOS, my Orbstack setup does not require root access, whereas on Linux, it typically does.
The fix sounds simple. Slap a sudo in front of it.
sudo docker images -f dangling=true -q | xargs docker rmi -f
This returns the same error!

The problem here is that there are actually two commands that you are executing:
docker imagesdocker rmi
BOTH of them require sudo.
So the command is actually as follows:
sudo docker images -f "dangling=true" -q | xargs -r sudo docker rmi -f
You should see something like this (assuming you have danging images):

TLDR
When running Docker with root access, removing multiple images requires additional effort.
Happy hacking!