Create a new build rule for DockerHub repository via API calls

Viewed 383

I have a repository on DockerHub, which I have configured to hook up directly with my GitHub repo, so that a git commit will trigger the build of the Docker images.

I am looking to build multiple Docker images (e.g. v1, v2 etc) for my product.

Now, I can see that DockerHub gives you the option to configure the "build rules" directly from the portal:

enter image description here

so right now, when I make changes to the /releases/v1/Dockerfile, the build will be triggered automatically.

Cool.

Going forward however, I expect to release /releases/v2/Dockerfile to my GitHub repo, and I would like for v2 to be built automatically as well, without me having to create the "build rule" manually. Is there a way to create a "build rule" programmatically?

I'm looking to call the DockerHub API to create the build rule. I've been through the documentation of the API here https://docs.docker.com/registry/spec/api/ but I couldn't find what I was after.

I would like to end up with:

enter image description here

where v2 was created programmatically and not from the console.

1 Answers

Docker Hub is following the git conventions where you wouldn't normally put different versions of the application in the same git commit. Instead, you would use separate branches and tags for different versions of your code. If you follow that git convention, then you can tag your resulting image based on the regex on the branch or tag. E.g.

  • Source Type: Branch
  • Source: /^v([0-9.]+)$/
  • Docker Tag: v{\1}

Then you can build within a branch called v1.1 (or any other version number) and the docker image will be tagged v1.1. To pull out just the first number of the tag, that would look like:

  • Source Type: Tag
  • Source: /^v([0-9]+)[0-9.]*$/
  • Docker Tag: v{\1}

Which would convert a tag with a version number like v10.1.2 into a docker tag of v10 (only numbers before the . are matched in the first part of the regex).

For more details on their build rules, see: https://docs.docker.com/docker-hub/builds/#set-up-build-rules

Regarding the API, while the registry itself has a documented API, and Hub has an API for Hub in beta, neither of these exposes the build settings. You could try to capture the calls by sniffing the browser traffic and replicating it in your app, but Docker could modify those calls at any time since they don't support an API for that.

Related