Docker is an open-source containerization platform that allows you to quickly build, test, and deploy applications as portable containers that can run anywhere. In this quick tutorial, we will show you how to remove all dangling, untagged and unused images in Docker. Also, how to remove one or more image by ID or by using a filter.
Removing Docker Images
Remove one or more images
To remove one or more Docker images use the docker images ls
command to find the ID of the images you want to remove.
$ docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE centos latest 15835a67d134 7 days ago 200MB ubuntu latest 3a4cca5ac898 2 months ago 111MB java 8-jre e44d62cf8862 2 months ago 311MB
Once you’ve located the images you want to remove, so use the rm
command followed by the IMAGE ID. For example, to remove the first two images listed in the output above run:
$ docker image rm 15835a67d134 3a4cca5ac898
If you get an error similar to the one shown below, it means that an existing container uses the image. To remove the image, you will have to remove the container first.
Error response from daemon: conflict: unable to remove repository reference "ubuntu" (must force) - container bd20b396a061 is using its referenced image 3a4cca5ac898
Remove dangling images
A dangling image is an image that is not tagged and is not used by any container. To remove dangling images and unused images run the below command:
$ docker image prune
You’ll be prompted to continue, use the -f
or --force
flag to bypass the prompt.
WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y
Remove all unused images
To remove all images which are not referenced by any existing container, not just the dangling ones, use the prune
command with the -a
flag:
% docker image prune -a
WARNING! This will remove all images without at least one container associated to them.
Are you sure you want to continue? [y/N] y
Remove images using filters
With the docker image prune
command, you can also remove images based on a certain condition using the filtering flag --filter
.
The currently supported filters are until
and label
. You can use more than one filter by using multiple --filter
flags.
For example, to remove all images that are created more than 24 hours ago, run the below command:
$ docker image prune -a --filter "until=24h"
Conclusion
You successfully learned how to remove all dangling, untagged and unused images in Docker. Also, how to remove one or more image by ID or by using a filter.
What’s Next? You might want to check the following guides: