Docker Hub Automated Builds
15 Dec 2020 | Docker DevOpsHi, Guys! I am back here. In this episode, I am going to show you how to implement a continuous integration mechanism with Docker Hub and Github.
Let’s assume a scenario that if a team is developing a project, developers responsible for their own part of this project, and they will push their source code to the repository. As a project manager, you certainly don’t want to rebuild and deploy the project manually over and over again. Here, the CI/CD is coming to play.
This article only focuses on continuous integration(CI) based on Github and Docker Hub.
Sign in to your Github and Docker Hub account.
Sign in your GitHub and Docker Hub, or register one if don’t have them. I create a repository docker-hub-CI-CD-explanation and docker-hub-automated-build-example for demonstration in this article.
Create a Github repository.
I created a Github repository named Docker Hub CI CD Explanation for next demonstration.
And I created two branches, they are main
and dev
branches.
main and dev branches
The main
branch is for the production environment and dev
branch is for the development environment. Developers push their code into the dev
branch, and then the project manager merges the merge request of dev
into the main
branch once developer finished a comprehensive unit.
As for this demonstration, I created a simple python common gateway interface(Python CGI) web application with simple file structures. If you are unfamiliar with the python CGI program, take look at the Python Running A CGI Web Server With http.server Module.
CGI Web Example File Structures:
├─cgi-bin
│ └─hello.py
└─index.py
Add Dockerfile
The Dockerfile
will be used by Docker Hub when Docker Hub Automatic Build Mechanism builds your images. So the Dockerfile is the guide, which instructs Docker Hub that how to build your images. In this article, I create a Dockerfile
on both dev
and main
branches. Currently, The file structures should look like this.
file structures on branch: main
├─Dockerfile
└─README.md
file structures on branch: dev
├─cgi-bin
│ └─hello.py
├─index.py
├─Dockerfile
└─README.md
As I will set two automatic builds on both dev
and main
branches in the later, so I need Dockerfile on each of them. The dev
branch is used for the development environment, once developers push their codes into dev
branch, then the Docker Hub will automatically build up a development image. The main
branch is used for the production environment, once the project manager merges the request from the dev
branch, then Docker Hub will automatically build up a production image.
Create a Docker Hub repository and set automated builds
Sign in to your Docker Hub account. Navigate to Account Settings > Linked Accounts
linked account on docker hub
Currently, the docker hub supports linking accounts to Github and Bitbucket.
When you finished linking accounts, then create a repository(repositories > Create Repository). I created a repository, which named docker-hub-automated-build-example for the demonstration.
docker hub simple example
Now, I need set up the Automated Builds, click the Builds.
docker hub automated builds example
Connect to an external repository source, and choose your organization and selected a repository.
docker hub automated builds configuration
As you can see from the above configuration, I configured two Build Rules, which are for the main
branch and the dev
branch. The main
branch uses the latest
as its tag, and the dev
branch uses the dev-latest
as its tag.
You can also specify the tag as its source type, and then the automated builds will be triggered when new tags are created
Click the Create and Build button in the right bottom of this page. Then after few minutes, you will see your images.
docker hub automated builds results
Test
The first time relatively takes more time, which really depends on the bandwidth and dependency size. In this example, both branches build images successfully. If your building failed, you can take a look at the detailed logs. for example:
take a look at the recent builds
building logs
Now, let us create a merge request on dev
branch and merge this request on the main
branch. When you finished the merge request, the Docker Hub will automatically retry to build a new image on the main
branch.
Here, a simple synopsis of the leading steps in this article:
- Add Dockerfile on Github or Bitbucket
- Setup automatical build on Docker Hub
Comments