In the previous article, we successfully dockerized our demo Spring Boot application. Now, let’s take a look at how we can leverage the GitHub and Docker Hub infrastructure to setup auto builds to trigger the creation of Docker images for our application whenever we push some changes to the code repository.
Docker Hub can be integrated with most of the code versioning and hosting providers. For this tutorial, however, we’ll consider GitHub. So, it is fair to assume that you already have a GitHub account and that you have an example repository created. If not, you can create a free GitHub account here and push the sample project you might have on to your new repository or you can use the one I’m using throughout this tutorial here.
If you don’t have a Docker Hub account already, go ahead and create one for free here. Though there multiple pricing options available, for this tutorial, the free version is all that you need. Once you verify your email ID and log back in, you’ll be greeted with a landing page similar to the one below.
If this is the first time you’re signing in to your Docker Hub account, you need to create your first repository for the docker image. Just like a GitHub code repository, a Docker Hub repository provides you the capability to version your Docker images. So, go ahead and click on the Create a Repository button.
On the Create Repository page, fill in the repository name and description and select if it is a public or private repository. Note that with a free Hub account, you can have only one private account. Under optional Build Settings, you get options to connect your GitHub or BitBucket account. Since we’re here enable auto triggering of the Docker image builds, let’s go ahead and link GitHub (or, BitBucket) account.
After successfully linking your GitHub account, you can select the repository that you want to link to this build. In this case, let’s select the project that we used in our previous article – SpringBoot Hateoas Demo application.
Next, we need to define a build rule. This will define how the new Docker images are versioned/tagged. You can get more idea on how these settings work for you, you can refer to the sample Build Rules under View example build rules. For now, I’m going with the Branch source type with my source branch as main
(not master
) and all other options as given. Click on Create, to create your Build Configuration.
Now, for the repository, under Builds, you should be able to see the newly created configuration.
Go ahead and trigger your first build by clicking the Trigger button under Automated Builds. Make sure you have provided the valid Source branch name – master
or main
. Assuming everything is configured properly, your build should successfully be able to generate the Docker image with the configured build rules.
You can now pull this image – chandanv892/springboot-hateoas-demo
– and run a container.
To verify if the build is indeed triggered automatically upon each change that you push to your code repository, you can push a change and see a new image generated. As you can see below, the new commit bfeb771
has triggered a new build and is in progress.
Well done! You now have an auto-build configuration that takes care of building new Docker images on every change that you push to your repository.
Stay safe!