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 the image prune command so this task only requires a single invocation of docker
sudo docker image prune