Floyd
Floyd

Reputation: 41

Error while cleaning images from docker

When I run

docker rmi $(docker images --filter "dangling=true" -q --no-trunc)

from the accepted answer to this question I sometimes get

docker: "rmi" requires a minimum of 1 argument.

am I doing something wrong? How can I prevent this from happening?

Upvotes: 4

Views: 2889

Answers (1)

Anthon
Anthon

Reputation: 76652

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'

Upvotes: 6

Related Questions