Sometimes it is common to list all tags available on docker to be able to select one by size, version or any other criteria.

Docker hub has a nice UI but the filtering by tag is not the most convenient - at least for me.

To solve that small issue, Groovy is from a great help (indeed you can do it with bash if you prefer).

For instance, to list all tags of openjdk images and their size you can just use that small script:

import static groovy.json.JsonOutput.prettyPrint
import static groovy.json.JsonOutput.toJson
import groovy.json.JsonSlurper

String next = 'https://hub.docker.com/v2/repositories/library/openjdk/tags' // 1
JsonSlurper jsonSlurper = new JsonSlurper()
def output = [:]
do {
  def result = jsonSlurper.parseText(new URL(next).text) // 2
  next = result.next // 3
  if (result.results) { // 4
    result.results.each { output.put(it.name, it.full_size) }
  }
} while (next)

def sorted = output.sort { it.value } // 5
println prettyPrint(toJson(sorted)) // 6
  1. We start from a "seed" url to grab tags, here openjdk was hardcoded but you can put any image you want,
  2. We grab the current page output (as JSON),
  3. We prepare next iteration (to visit all pages),
  4. If current page has some tags we append them in our result (we extract the fields we are interested in too). Note it can be a good place to remove some tags you don't want if needed.
  5. We sort the tag by the criteria we are interested in (the full_size here),
  6. Finally we print the output data we captured. If you are lazy, a JSON output is pretty readable and/or injectable in any other software.

This kind of simple scripts really enable to boost your productivity and, if you are a java developper, will enable you to avoid any learning curve barrier as it can be with bash, zsh or other scripting alternatives.

Finally, the last tip is that if you are using SDKMan (if you are not maybe you should ;)), installing groovy is as simple as:

sdk install groovy 3.0.2

Happy scripting :).

From the same author:

In the same category: