How to remove <none> images after building

This works on my end. Removing all or unused images.

Docker rmi $(docker images -a -q)

use
docker rmi (docker images -f “dangling=true” -q )

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

Removing the double quotes from the dangling options solves the problem

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

sometimes we may need “f” for forcing to remove containers

I’ve came across the same problem and have written a solution on Stack Overflow, which I will reproduce here.


You can label the intermediate images and docker image prune those images based on this label.

Dockerfile (using multi-stage builds)

FROM node as builder
LABEL stage=builder
...

FROM node:dubnium-alpine
...

After you’ve built you image, run:

$ docker image prune --filter label=stage=builder

For Automation Servers (e.g. Jenkins)

If you are running the builds in an automation server (e.g. Jenkins), and want to remove only the intermediate images from that build, you can

  1. Set a unique build ID as an environment variable inside your Jenkins build
  2. Add an ARG instruction for this build ID inside your Dockerfile
  3. Pass the build ID to docker build through the --build-arg flag
FROM node as builder
ARG BUILD_ID
LABEL stage=builder
LABEL build=$BUILD_ID
...

FROM node:dubnium-alpine
...
$ docker build --build-arg BUILD_ID .
$ docker image prune --filter label=stage=builder --filter label=build=$BUILD_ID

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
...

This worked for me :slight_smile:
docker rmi <image_ID> -f
Then you’ll see:
Deleted: sha256:e23dfs4hlkjlkjsdflksjdfosdflksdjf2rf239fi932i09329f0i2390if2903f0239f

sudo docker image rm cd14cecfdb3a -f

--force-rm seems to prevent leaving <none>:<none> images when I use docker-compose to build images like so:

docker-compose build --force-rm

I don’t know why it works. The doc (https://docs.docker.com/compose/reference/build/) says --force-rm always remove intermediate containers but not images.

This works for me.
docker image prune -f

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:

docker rmi $(docker images -f "dangling=true" -q | tr "\n" " ")

Thanks! Copy-paste does not work because of the quotes. But the code works anyhow.

you are right, but need in the powershell

Hi all,
I tried the docker rmi $(docker images -f “dangling=true” -q) command and it worked for me.

Great.also has this problem.
thanks for it

Try this:

docker rmi `docker images | grep "<none>" | awk {'print $3'}`

… or instead of using grep and awk in pipes with docker’s build-in functionality:

docker image rm $(docker image ls --filter dangling=true -q)

It’s quite odd that docker rm does not accept the --filter argument.

All effective ways are fine as long as they get the job done :slight_smile:

you can use this command
docker image rm -f $(docker images | grep "<none>" | awk '{print$3}')

There is really no need for using grep and awk or any fancy long commands.

Just use:

docker image prune

and it will prune all dangling images.

3 Likes

This really help all issue above mention…