The problem with that answer is that it runs docker rmi
even though there might not be any images to delete (that is when the output from docker images --filter....
is empty), and that is when you get the error.
@rubicks solution to that question doesn't do a much better job, but points to a usable alternative:
docker images --no-trunc --all --quiet --filter="dangling=true" | xargs --no-run-if-empty docker rmi
the --no-run-if-empty
argument to xargs
does what it says and prevents that error from happening, even if you run it and you have nothing to clean.
I have the following aliases, because the above is a bit too much to type every time I want to use it (the first is for removing unused containers):
alias drrm='docker ps --no-trunc --all --quiet --filter="status=exited" | xargs --no-run-if-empty docker rm'
alias drrmi='docker images --no-trunc --all --quiet --filter="dangling=true" | xargs --no-run-if-empty docker rmi'