Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve the newest tag for an image?

Tags:

docker

Given the following list of images, how would I retrieve the newest of the tags? (0.0.268) I guess it's possible with a combination of bash and Go template, but I can't figure it out...

my-image 0.0.1 fd704b8d675e
my-image 0.0.2 9294a62d2c38
my-image 0.0.9 325326e8f7a2
my-image 0.0.10 b97c64b198d9
my-image 0.0.268 8a89b5fac348

For example:
By running the following command (bx cr is an IBM Bluemix CLI):

tagsList=$(bx cr images --format "{{if (eq .Repository \"myregistry/mynamespace/myimage\")}} {{.Tag}}{{end}}")

The echo of $tagsList is:

0.0.10 0.0.12 0.0.13 0.0.14 0.0.15 0.0.3 0.0.4 0.0.5 0.0.7

Out of this list I need the newest tag, which is 0.0.15.

like image 510
Idan Adar Avatar asked Jun 24 '17 13:06

Idan Adar


People also ask

How do I find a photo tag?

You have several alternatives to validate the existence of the tag in the registry: Pull the whole image with the tag by using docker pull <image>:<tag> Leverage the Container Registry API. Use the GitHub Package REST API.

How do I get a docker image tag?

Simply head over to Docker Hub, choose a tag, and each time you pull the image, you'll get the latest version pushed by the authors. The downside of this is that each time a Docker tag is pulled, the latest version is used, and this is often not what you want if you value build reproducibility, and you really should!


1 Answers

To get the last tag <major_version>.<medium_version>.<minor_version> :

file.txt:

my-image 0.10.1 fd704b8d675e
my-image 2.0.2 9294a62d2c38
my-image 0.0.9 325326e8f7a2
my-image 10.0.3 b97c64b198d9
my-image 10.0.10 b97c64b198d9
my-image 0.0.268 8a89b5fac348
my-image 10.0.6 b97c64b198d9

last_tag.sh:

#!/usr/bin/env bash

cut -d' ' -f2 file.txt \
    | sort -t . -k1,1 -k2,2 -k3,3 -nr \
    | head -1

-t . : precise the delimiter .
-k n,n : sort for the column n th (1-based)
-nr : sort number (no string) in decreasing order
-k1,1 -k2,2 -k3,3 : sort in order column 1, column 2, column 3, that is to say major version, medium version then minor version.

Output:

10.0.10
like image 165
glegoux Avatar answered Oct 25 '22 18:10

glegoux