5

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?

These do NOT work:

docker rmi $(docker images -q)
docker rmi $(docker images | grep “^” | awk “{print $3}”)
docker rmi $(docker images -f “dangling=true” -q)
mahmoodi
  • 73
  • 1
  • 9
  • Possible duplicate of [Docker remove TAG images](https://stackoverflow.com/questions/33913020/docker-remove-none-tag-images) – Adiii Oct 01 '19 at 12:21

4 Answers4

11
docker rmi `docker images | grep "<none>" | awk {'print $3'}`
Vishnu Nair
  • 1,399
  • 1
  • 14
  • 21
  • 1
    Thank you for your answer - although code/config snippets might provide some limited short-term help, a proper explanation [would greatly improve](https://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) its long-term value by showing why this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you've made. – kenny_k Oct 01 '19 at 14:05
0

Try this:

docker rmi $(docker images | grep none | awk '{print $3}')

You can add -f to force image removal but I do not recommend doing so. If you cannot remove the images with the command above that means it is in partially in use or stopped containers are using the images you are trying to remove.

You can always check the stopped or Existed containers with

docker ps -a

Here, the Status column will indicate the container status.

nPcomp
  • 8,637
  • 2
  • 54
  • 49
0

Above answers will help for docker cli.

In k8s 1.24+ versions docker is replaced by nerdctl

to delete tag images with nerdctl, one can use --names flag of nerdctl command.

nerdctl images --names | grep sha256 | awk '{print $1}' | xargs nerdctl rmi
KHEMRAJD
  • 41
  • 4
  • You can't usually directly log into Kubernetes nodes to run commands like this, and you wouldn't directly build images on Kubernetes nodes as described in the question. – David Maze Jul 04 '23 at 12:14
  • @DavidMaze you are correct. Answer still holds true without kubernetes for one who uses nerdctl – KHEMRAJD Jul 10 '23 at 07:33
-1

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

Adding -a is required.