-2

I need some kind of Docker behaviour. Imagine docker image name is imagename and image also have tag(1, 2, 3 ...). Now I want to delete all images with the name imagename and with the tag that isn't x. For example if I have images:

imagename:1
imagename:2
imagename:3
imagename:4
imagename:5
imagename:6

with 1, 2, 3 ...6 tags, and I want to have only one Image with name imagename and tag 6, what command should I run?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
O. Shekriladze
  • 1,346
  • 1
  • 19
  • 36
  • 3
    What have you already tried? Is there a specific tool set you're thinking of (the Python Docker API; `grep` and `xargs`; ...)? – David Maze May 19 '19 at 16:38

1 Answers1

1

I don't think it's possible with the docker command alone. I would use a small shell loop. For example remove every python image which is not tagged 3.6:

docker images | awk -v img='python' -v tag='3.6' '$1==img && $2!=tag{print $3}' \
  | while read -r id ; do docker rmi "${id}" ; done
hek2mgl
  • 152,036
  • 28
  • 249
  • 266