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.
docker system prune will do the work. It will remove any unused images though. but I am sure, all worthy images should have been pushed to the registry so it should be okay to prune it, depending on your environment though.
If you are like me and keep trying out interactive terminal sessions on images all the time, you will see a lot of : images in the list. They are all linked to stopped containers that you used earlier. Cleanup containers and then cleanup images
On windows, create a new batch file and add the following command,
FOR /f “tokens=*” %%i IN (‘docker images -f “dangling=true” -q’) DO docker rmi %%i -f
received the response while trying with this command. Tag and repositories might be linked to some active repositories.
Command: docker rmi $(docker images -a|grep "<none>"|awk '$1=="<none>" {print $3}')
Response:
Error response from daemon: conflict: unable to delete f039e1783673 (cannot be forced) - image has dependent child images
If you want to persists the build ID in the image (perhaps as a form of documentation accessible within the container), you can add another ENV instruction that takes the value of the ARG build argument. This also allows you to use the similar environment replacement to set the label value to the build ID.
FROM node as builder
ARG BUILD_ID
ENV BUILD_ID=$BUILD_ID
LABEL stage=builder
LABEL build=$BUILD_ID
...
FROM node:dubnium-alpine
...
docker images -f "dangling=true" -q lists one image per row, and docker rmi expects them to be separated by space rather than newline, so you have to join them together before calling docker rmi if there are multiple images that need to be removed. This is the command that works for me: