The GitHub repo used in this article can be found here.
One of the lesser known features of Docker is its ability to do cloud builds based on WebHooks from GitHub and Bitbucket. Bitbucket and GitHub integration works in Docker natively so that when code is pushed to a repository, Docker will download and deploy the code and build the image on Docker hub automatically with Docker Automated Builds.
Setting it up is easy. Logon to Docker Hub, and you can select from the Create menu, select Create Automated Build.
This will take you to a page with two big buttons – one for Github and one for Bitbucket. Both work the same way – You first link your Github account with the Docker Hub account. This process is pretty straight forward. Once you link the accounts, you can now select the GitHub or Bitbucket repository you want to use. Once you select the repository, you can now create the build integration. Name the Docker Hub repository whatever you want then click Create.
Now, you can git push your app to GitHub or Bitbucket with git, and it will then trigger a build on Docker Hub. The push will need to include a Dockerfile in the root of the git repo. This is way Docker Hub picks up whenever the automated builds are created. The referenced demo has a Dockerfile that pushes a simple .NET core app to the container, which then compiles the code and sets up the container to run the code. In Docker Hub, you can click on Build Details to see the status of current and past builds.
Also with Docker Hub, you can invoke WebHooks. WebHooks are useful for notifying services using the image that a new image has been deployed. To use a WebHook, there needs to be an endpoint to call. Azure provides many ways to do this with Logic Apps and Azure Functions. What happens after the the build completes and the WebHook is called will vary.
On Azure, it is possible to use a container hosted as a Linux Web App, which is currently in preview. The container is pulled every time the application starts up, so in order to pull the new container, I created an Azure Function that calls a PowerShell script to start and stop the the Web App. The Azure Function can be exposed using a URL. The URL can be supplied to a the Web Hook on Docker Hub.
Now, whenever I push to GitHub, Docker Hub will scoop up the changes, rebuild the image, then call the Azure Function that will then pull the newly built image. What happens downstream of the WebHook is going to depend on what you are using to hosts containers, but in most every case there is a way to have Docker Hub call an HTTP endpoint to invoke an integration script of some kind.
This simple solution allows for a completely PaaS based solution for doing Continuous Integration and or Continuous Delivery in the cloud. There’s no need to set up an automation server or a build server — simply using available services, complete CI/CD pipelines can be built to enable cloud-based DevOps. Best of all, most of these services are free or very inexpensive. It’s hard beat that!
Need Help With A DevOps Project?
DevOps Consulting DevOps Training