Every once in a while, my local Docker container image takes up a lot of space on my hard drive, so I clean it up regularly, but this cleanup is not done often enough to remember the commands. In this article, I’m going to organize a few common commands, so I can easily look up commands in the future.

docker resource cleanup

List all dangling images

dangling images are container images that are left behind during each docker build. If you have already built once, using the tag demo:latest, and then build again, using the tag demo:latest, then the previous image will not be deleted, but only Untagged, and these Untagged images are dangling images!

The following command lists all untagged container images.

1
docker images --filter "dangling=true"

The --filter here works great, see the official docker images documentation for more suggestions!

The following command lists all untagged container images, but only the Image ID is displayed.

1
docker images --filter "dangling=true" -q

All untagged container images

Quickly remove all dangling images

The following command amazingly happens to work with both Bash and PowerShell.

1
docker rmi $(docker images -f "dangling=true" -q)

This command is actually a bit complicated, the average person can not remember, in fact, you can also use the following command, to do exactly the same thing, much better to remember! 👍

1
docker image prune

docker image prune

Remove all unused images

If you run docker pull nginx to pull the nginx container image, but have not run any containers (Container) with this image, these cannot be called dangling images, but rather unused images (unused container images), if you want to clear all such images at once, you can run the following command.

If you run docker pull nginx to pull the nginx container image, but have not run any containers (Container) with this image, these cannot be called dangling images, but rather unused images, if you want to clear all such images in one go, you can run the following command.

1
docker image prune -a

docker image prune

Remove all unused Docker objects (Docker System Prune)

The following command not only deletes container images, but also “stopped containers”, “dangling networks”, “dangling images”, “dangling build cache”, all of them!

1
docker system prune -a

Ref