Powershell logo

The other day, I had the need to get a list of all the repositories from an organization in DockerHub to gather some statistics.

Being a huge advocate I wanted to do this in PowerShell, as this makes it easy to process the data further, such as exports to CSV.

To be able to do this, we first need to authenticate to their API using the /users/login endpoint. Currently, the API version Docker Hub uses is version 2. When we successfully authenticate, we obtain a JWT authentication token we need to pass on in any subsequent calls to their API, in this case, the repositories/<orgname> endpoint.

This is the code I ended up with.

$loginUrl = "https://hub.docker.com/v2/users/login/"

$username = "username"
$password = 'password'
$organization = "yourorganization"

$params = @{
  "username"        = $username
  "password"        = $password 

# This is a little trick to suppress the progress bar
$ProgressPreference = 'SilentlyContinue'

# First we need to get a token from Docker Hub
Write-Output "Authenticating to Docker Hub"
try {
  $loginResult = Invoke-WebRequest -Uri $loginUrl -Method Post -Body $params -UseBasicParsing  
catch {
  throw $Exception.Message
$token = ($loginResult.Content | ConvertFrom-Json).token
$header = @{Authorization = "JWT $token" }

# Then we attempt to get a list of repositories
Write-Output "Getting repositories"
$reposUrl = "https://hub.docker.com/v2/repositories/$organization/?page_size=200"
try {
  $reposList = Invoke-WebRequest -Uri $reposUrl -Headers $header -UseBasicParsing  
catch {
  throw $Exception.Message

# Finally we output the list of repositories and total count
# We convert the results from JSON
$listsObject = $reposList.Content | ConvertFrom-Json
Write-Host "$($listsObject | Select-Object -ExpandProperty Count) repositories found)"
$listsObject | Select-Object -ExpandProperty results | Out-GridView

By Jostein Elvaker Haande

"A free society is a society where it is safe to be unpopular" - Adlai Stevenson

Leave a Reply

Your email address will not be published. Required fields are marked *