After I build my image, there are a bunch of images. When I try to delete them I get “image has dependent child images” errors. Is there anyway to clean this up?
docker images --filter “dangling=true” -q --no-trunc
this doesn’t print anything so I get an error “docker: “rmi” requires a minimum of 1 argument.” when I call docker rmi $(docker images --filter “dangling=true” -q --no-trunc)
[quote]When you build your images add “–rm” this should help with removing any intermediate and none images.
‘’‘docker build --rm ‘’’
[/quote]
I do build with this but this only removes containers not images. The results of docker images -a shows images with “none”
The results of docker images -a shows images with “none”
docker images -a will always show lots of “none” images, since it shows every layer of every image. If you run, for instance, docker history apache:latest, you should see all of the intermediate layer IDs. Those are included in the docker images -a output and can’t be removed.
You can use the following command to clean these components
docker system prune
will be showed the message below:
WARNING! This will remove:
- all stopped containers
- all volumes not used by at least one container
- all networks not used by at least one container
- all dangling images
tl;dr - They cannot be deleted: save your new image, delete all the images and load it back
For anyone landing here, please read carefully Docker’s documentation and previous answers: as stated before ‘<none>:<none>’ images are intermediate images created during a build command and are parents of the newly created and, again, cannot be deleted.
Commands like docker image prune -f will not delete that images, I’ve personally tested every command mentioned on this post and the only way I’ve found to achieve that is using docker save/load:
docker rmi $(docker images --filter “dangling=true” -q --no-trunc) command will try to remove all dangled images. Once it is cleaned (no more image left), it will throw error as docker rmi require at least one image name/id.
2>/dev/null will direct any error message to null, so it will do nothing if all image is cleaned up.
OPTION 1: (no error)
You can also try cleaning only when dangled image is available using if condition to avoid any error. Something like below:
if [ "$(docker images -f "dangling=true" -q | awk '{print $3}' | sort -u)x" != "x" ]
then
docker rmi $(docker images --filter "dangling=true" -q --no-trunc)
fi
It is important to understand why you have intermediate untagged images showing as <none> <none> in order to avoid them since, as you have seen, you can’t remove them if they are in use.
The reason untagged images happen is because you built an image, then you changed the Dockerfile and built that image again and it reused some of the layers from the previous build. Now you have an untagged image which cannot be deleted because some of it’s layers are being used by a new version of that image.
The solution is to:
Delete the new version of the image
Delete the untagged image and
Rebuild the new version of the image so that it owns all of the layers.
You will be left with a single tagged image that contains all of the layers of the previous untagged images and the new image.