Monday, March 13, 2017

Docker: delete all tags of image

Handy one liner to delete all tags of a particular docker image (on Linux):



docker images | grep rabbitmq | tr -s ' ' | cut -d ' ' -f 2 | xargs -I {} docker rmi docker-staging-local.artifactory.corp.alleninstitute.org/rabbitmq:{}  

Here are the image tags that this one-liner removes:

1
2
3
4
5
6
7
thusharaw@denali ois-rabbitmq (thushara/rabbitv1)*$ docker images | grep rabbitmq
docker-staging-local.artifactory.corp.alleninstitute.org/rabbitmq                   2017031300          6fb1ed865ae6        3 hours ago         179 MB
docker-staging-local.artifactory.corp.alleninstitute.org/rabbitmq                   2017031307          6fb1ed865ae6        3 hours ago         179 MB
docker-staging-local.artifactory.corp.alleninstitute.org/rabbitmq                   2017031007          21b84ae0586e        2 days ago          179 MB
docker-staging-local.artifactory.corp.alleninstitute.org/rabbitmq                   201703100           43d51d243631        2 days ago          179 MB
docker-staging-local.artifactory.corp.alleninstitute.org/rabbitmq                   201703107           08927efd735e        2 days ago          179 MB
docker-staging-local.artifactory.corp.alleninstitute.org/rabbitmq                   201703109           646cb7852d8e        2 days ago          179 MB

Let's break down the on-liner:


1) docker images => 

get the images

2) grep rabbitmq

find the tags you care about

3) tr -s ' '

convert all repeating contiguous spaces to a single space so we can easily index to a specific column, the tag in this case

4) xargs -I {} docker rmi docker-staging-local.artifactory.corp.alleninstitute.org/rabbitmq:{} 

pass the thusly recovered tag to the `docker rmi` command, but use xargs to change the stdout of the previous cmd to a cmd line arg (which is what `docker rmi` works with)