3

I would like to ask if it's possible to delete docker images for its tag or the created label, I have something like this:

Repository                     TAG             IMAGE_ID       CREATED          SIZE

registry.someRegistry.         latest          fa8767676       5 hours ago     119MB
registry.someRegistry.         <none>          878787874       29 hours ago    119MB
registry.someRegistry.         <none>          jkj7jjjk4       2 days ago      119MB
registry.someRegistry.         <none>          d99090iii       3 days ago      119MB

I need to remove all the images but not the most recently one in this case is the one tagged with latest or the one with the most recent Created date. I can use the next command to delete all the images until 5 hours of creation, but it's not appropriate, because I could delete more images not just the last one, would be better to delete images by the tag=latest.

docker image prune -a --force --filter "until=5h"

I need also an additional filter for the registry I think, in order to remove just the images for the registry I need.

Any good way to achieve that?

rasilvap
  • 1,771
  • 3
  • 31
  • 70
  • https://stackoverflow.com/questions/45142528/what-is-a-dangling-image-and-what-is-an-unused-image, Please check here – Jinna Balu Oct 12 '21 at 23:18

5 Answers5

2

The simplest would be the rmi command, i.e.:

docker rmi 878787874

This is a "remove image" command and uses the IMAGE_ID you list.

stdunbar
  • 16,263
  • 11
  • 31
  • 53
  • 1
    No, because I don't know what is the id of the images that I need to remove and could be any number 2 to 1000 for example. So I. need to implement a process to remove all of this dangling images. – rasilvap Oct 12 '21 at 21:49
1

None of the replies were helpful, and there does not seem to be another thread either. It is way too late but I had the same problem and found this solution:

docker rmi $(docker images | grep "<none>" | grep "registry.someRegistry." | awk {'print $3'})

This is not 100% proof because grep looks for a string in the name and the tag, so theoretically if your registry name appears in some tag and vice versa you could accidentally delete it.

1

It is possible to find all dangling images from one specific repository with

docker images repository --filter dangling=true

If you want to filter by more than one repository based on the registry, it is also possible to do

docker images --filter=reference='registry/someRegistry/*:*' --filter dangling=true

Once you have the list of the ones you want to remove you can do it with:

docker rmi $(docker images --filter=reference='registry/someRegistry/*:*' --filter dangling=true -q)

Edit to add that the wildcard * does not apply to /, so if the registry has a more complex folder structure, the expression should be amended to something like

'registry/someRegistry/*/*:*'
PaulaF
  • 106
  • 4
0

you can remove dangling images with filter

 docker images --filter dangling=true
None
  • 330
  • 2
  • 16
  • Thanks but I need to remove the ones from a specific repo name not all of them, I need to keep just the latest one and remove the dangling or the ones tagged with – rasilvap Oct 12 '21 at 22:06
0

I have to modify the command slightly to have it run:

sudo docker rmi $(sudo docker images | grep "<none>" | awk {'print $3'})
MikeR
  • 11
  • 2