Remove or prune dangling docker images filter by their docker image name or repository

Viewed 150

I have the next scenario:

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
otherRegistries..              other Tags.     otherIds.       otherDates..    ...

For this scenario I need to implement a process which deletes all the docker images tagged with for the registry.someRegistry, but not the one tagged with latest. I have to call this process from my program so I don't have idea about Ids.

After run the process the result should be:

Repository                     TAG             IMAGE_ID       CREATED          SIZE

registry.someRegistry.         latest          fa8767676       5 hours ago     119MB
otherRegistries..              other Tags.     otherIds.       otherDates..    ...

I tried the next commands:

docker rmi --force $(docker images -q 'registry.someRegistry' | uniq)

But this command deletes all the docker images for the registry.someRegistry

Is there the possibility to add another filter to exclude the image tagged with latest?

I know that docker image prune remove all the dangling images, but I need to remove just the ones for the registry.someRegistry.

Any ideas?

1 Answers

What I was looking for was a docker prune, regarding the documentation https://docs.docker.com/engine/reference/commandline/image_prune/

The prune command supports label and until for filtering purposes, so what I did was to filter by the image label that I need to delete.

I used the next command: docker image prune --force --filter='label=someLabel.image'

In order to know what are the labels that I need I used the command:

sudo docker inspect registry.someRegistry.

Finally I got the result I wanted.

Related