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.
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.
|
|
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.
|
|
Quickly remove all dangling images
The following command amazingly happens to work with both Bash and PowerShell.
|
|
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! 👍
|
|
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.
|
|
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!
|
|