Huseyn Gasimov
Huseyn Gasimov
Creator of this blog.
Sep 24, 2019 3 min read

Track how many times docker image was pulled

thumbnail for this post

Hey, everyone. I recently recently tried to get statistics about how many people pull the docker images of Intelec AI from Docker Hub by time. I was surprised that Docker Hub doesn’t provide this kind of basic statistics. Hence I started searching in the Internet to find, how another people tackle this problem. I realized that most people had to bake their own solution, while others installed multiple softwares to track their docker hub statistics. Honestly, I didn’t want to install an aditional software to solve this problem. I guessed using Google Sheet and Apps Script would be an easier solution. This is how I proceeded.

Getting data from Docker Hub

During my search, this page gave me a hint that I can get information about a docker image from a dedicated URL, which also contains pull count for given time. For example, information about image intelecai/automl-server can be fetched from URL https://hub.docker.com/v2/repositories/intelecai/automl-server/. This is an example response:

{"user": "intelecai", "name": "automl-server", "namespace": "intelecai", "repository_type": "image", "status": 1, "description": "Automated Machine Learning server from Intelec AI", "is_private": false, "is_automated": false, "can_edit": false, "star_count": 0, "pull_count": 254, "last_updated": "2019-08-24T1324.825849Z", "is_migrated": false, "has_starred": false, "full_description": "Automated Machine Learning server from Intelec AI. Learn more at www.intelec.ai.", "affiliation": null, "permissions": {"read": true, "write": false, "admin": false}}

Writing data to Google Sheet

We can use Google Apps Script to fetch data from the above URL and write it to a Google Sheet. Let’s go through that step-by-step:

  • Create a Google Sheet
  • Name columns as “Date”, “Num of pulls [image1]“, “Num of pulls [image1]“, etc. In my case, it looked like this:

Name columns in google sheet

  • Go to Tools > Script editor to open the script editor
  • Copy paste the following code there and change the docker image names to the names of your docker images:
function recordDockerImagePullCount() {
  images = ['intelecai/automl-server', 'intelecai/inference-server'];
  var row = [new Date()];
  
  images.forEach(function (image) {
    var pull_count = get_image_pull_count(image);
    row.push(pull_count);
  });
  
  var sheet = SpreadsheetApp.getActiveSheet();
  sheet.appendRow(row);
}

function get_image_pull_count(image) {
  var response = UrlFetchApp.fetch("https://hub.docker.com/v2/repositories/" + image);
  var imageStats = JSON.parse(response.getContentText());
  return imageStats['pull_count'];
}
  • Run the function recordDockerImagePullCount going to Run > Run function > recordDockerImagePullCount . Then go to your google sheet. You will see something like this:

Script editor run function

Execute script periodically

You can run Google Apps Script periodically using triggers. I will guide you through that again step-by-step:

  • In Script editor, go to Edit > Current project’s triggers

Open project triggers

  • Click on “create a new trigger” or “Add trigger”
  • Fill the form like this

Open project triggers

Here, I created a “Time-driven” trigger, which executes the function recordDockerImagePullCount every midnight. You can also execute your function hourly, minutely, etc, by changing some of the above field.

Add a chart

Adding a chart to a google sheet is easy. Just go to Insert > Chart. Then fill it like this:

Create chart

After you wait some hours or days, the end result will look like this:

Create chart

That’s it. I hope you found it helpful.