OPTION 1: using error handling (send error to /dev/null)
docker rmi $(docker images --filter "dangling=true" -q --no-trunc) 2>/dev/null
docker rmi $(docker images --filter “dangling=true” -q --no-trunc) command will try to remove all dangled images. Once it is cleaned (no more image left), it will throw error as docker rmi require at least one image name/id.
2>/dev/null will direct any error message to null, so it will do nothing if all image is cleaned up.
OPTION 1: (no error)
You can also try cleaning only when dangled image is available using if condition to avoid any error. Something like below:
if [ "$(docker images -f "dangling=true" -q | awk '{print $3}' | sort -u)x" != "x" ]
then
docker rmi $(docker images --filter "dangling=true" -q --no-trunc)
fi