
上QQ阅读APP看书,第一时间看更新
Automatic builds for Azure Container Registry
Azure Container Registry offers similar functionality to Docker Hub for the automation of Docker image builds on code push. The pipeline is highly customizable and can support building multiple container images at once, but in this example, we will focus on automating a single image build on the GitHub repository code push.
For more advanced multi-step and multi-container scenarios, check out the official documentation: https://docs.microsoft.com/en-us/azure/container-registry/container-registry-tutorial-multistep-task.
Integrating ACR and GitHub can be performed as follows:
- Create a new GitHub repository and push the Docker image source code. In this example, we will use source code from https://github.com/PacktPublishing/Hands-On-Kubernetes-on-Windows/tree/master/Chapter03/04_iis-demo-index, which will be pushed to a new GitHub repository, that is, https://github.com/hands-on-kubernetes-on-windows/iis-demo-index.
- Generate a GitHub Personal Access Token (PAT) in order to access the repository in ACR. Navigate to https://github.com/settings/tokens/new.
- Enter a PAT description and choose the repo:status and public_repo scopes (for private repositories, you need to use a full repo scope):
- Click the Generate token button.
- You will be provided with a PAT value. Copy the token to a secure location as you will need it to set up integration.
- Now, let's create an ACR task called iis-demo-index-task. This will be triggered automatically when code is pushed to https://github.com/hands-on-kubernetes-on-windows/iis-demo-index. The required parameters are similar to the build configuration for Docker Hub:
az acr task create `
--registry handsonkubernetesonwinregistry `
--name iis-demo-index-task `
--platform windows `
--image "iis-demo-index:{{.Run.ID}}" `
--context https://github.com/hands-on-kubernetes-on-windows/iis-demo-index `
--branch master `
--file Dockerfile `
--git-access-token <gitHubPersonalAccessTokenValue>
If you run into an az acr task create: 'utputformat' is not a valid value for '--output'. See 'az acr task create --help'. error being returned by the Azure CLI, ensure that you are escaping/quoting curly brackets for PowerShell properly.
- Test your ACR task definition using the az acr task run command:
az acr task run `
--registry handsonkubernetesonwinregistry `
--name iis-demo-index-task
- In the source code for your Docker image, introduce a change and commit and push it to the GitHub repository. For example, modify the static text so that it reads as follows:
Hello World from IIS container! The image is provided by Azure Container Registry and automatically built by Azure Container Registry task.
- Retrieve the ACR task logs to verify that the task was indeed triggered:
az acr task logs --registry handsonkubernetesonwinregistry
You should see an output similar to the following, which indicates that a new task instance was triggered by the push:
- When the task is finished, pull the image tagged with Run ID (in this case, this is cb5). You can also use the latest tag, but this requires removing a locally cached image using the docker rmi command:
docker pull handsonkubernetesonwinregistry.azurecr.io/iis-demo-index:cb5
- Create a new container using the handsonkubernetesonwinregistry.azurecr.io/iis-demo-index:cb5 image:
docker run -it --rm `
-p 8080:80 `
handsonkubernetesonwinregistry.azurecr.io/iis-demo-index:cb5
- Navigate to http://localhost:8080 in a web browser and verify that the container is running as expected. Also, verify that the static HTML page contains changes that were introduced in the code push:
Other cloud service providers have similar offerings for setting up Docker image registries and build pipelines. If you are interested in Google Cloud Platform services, please check out GCP Cloud Build: https://cloud.google.com/cloud-build/docs/quickstart-docker.
You have successfully set up your Docker image build pipeline using GitHub and Azure Container Registry – congratulations! Now, we will take a quick look at best practices for image tagging and versioning.