Docker Community Forums

Share and learn in the Docker community.

How to remove <none> images after building

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.

2 Likes

If you are running Docker For Windows, the following will work:

echo off&for /f “delims=” %A in ('docker images -f “dangling=true” -q ') do docker rmi %A & echo on

To remove all images which aren’t associated with a running container:

docker image prune -a

The -a parameter is the crucial bit here.

If you want to force the action to occur without a confirmation prompt, you may add the -f parameter, like so:

docker image prune -af

Note: -af is the same as specifying -a and -f separately

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

docker container prune
docker image prune

This worked for me;

Shows all images, copy the ‘Image ID’ of the one you want to delete

sudo docker images

Command to delete, paste image id, without <> and hit enter

sudo docker rmi -f <$imageID>

##Visit your friends at;
www.shenko.org

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

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