docker images -f dangling=true -q | xargs --no-run-if-empty docker rmi
docker images
outputs all images currently available.
By specifying -f "dangling=true"
we restrict the list to "dangling" images (i.e. untagged).
By specifying the -q
option we make the output quiet, so it prints only the image hashes.
We pass the output (list of image hashes) to xargs
, to run docker rmi
for them to remove them. To avoid raising an error when there are no untagged images to remove, we use --no-run-if-empty
. This way the command should always succeed unless there was an actual problem removing a Docker image.
This only works in the GNU implementation of xargs
(thanks to the --no-run-if-empty
), the BSD implementation does not have an equivalent at the time of this writing.
docker rmi $(docker images -f "dangling=true" -q)