Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I test if a specific tag of a docker image already exists in gcr.io?

I've noticed with gcr.io that when I push a docker image with a specific tag:

gcr.io/myproject/myimage:mytag

If that image/tag combo already exists, it seems to untag the original image, upload the new one, and apply the tag to the new one.

This is leading to my repo becoming bloating with lots of untagged versions of the same image.

How do I test if the image/tag combo already exist in gcr.io, so that I only push when it's required?

like image 621
Andy Avatar asked Mar 25 '19 17:03

Andy


People also ask

How do I tag in docker?

Docker tags are just an alias for an image ID. The tag's name must be an ASCII character string and may include lowercase and uppercase letters, digits, underscores, periods, and dashes. In addition, the tag names must not begin with a period or a dash, and they can only contain 128 characters.


1 Answers

Here's how I solved this in a shell script

existing_tags=$(gcloud container images list-tags --filter="tags:mytag" --format=json gcr.io/myproject/myimage)

if [[ "$existing_tags" == "[]" ]]; then
  printf "tag does not exist"
else
  printf "tag exists"
fi

Explanation

I'm using gcloud container images list-tags (documentation here)

  • And filtering for tags matching mytag using the --filter flag

  • And formatting as JSON using --format=json

So essentially, if the tag mytag doesn't exist, the output of this will be an empty array [], otherwise, it does exist. You can test this really simply just by string comparison in your script, and then proceed accordingly.

like image 136
davnicwil Avatar answered Sep 21 '22 12:09

davnicwil