One of the features Azure Web App provides is to leverage containers for the website you are hosting. The offering is officially called, “Web App for Containers.” This provides several benefits:
- Easily deploy and run containers applications that scale with your business
- Use a fully-managed platform to perform infrastructure maintenance
- Take advantage of build-in auto scaling and load balancing
- Streamline CI/CD with Docker Hub, Azure Container Registry (ACR), and GitHub
ACR is an Azure Resource in which you can store containers. One of the features of ACR is Tasks. Tasks allow you to automate container building in the following scenarios:
- Detect source code changes in GitHub or Azure DevOps and automatically build a new image and store in ACR
- Detect base image update and automatically build a new image and store in ACR
- On a schedule, build a new image and store in ACR
- Manually build and push a single container image on-demand into ACR without needing a local Docker Engine installation. Think docker build & docker push in the cloud.
- Multi-step task to extend the single image build-and-push capability of ACR Tasks with multi-step, multi-container-based workflows.
We will be leveraging the first method in which we will build a container, modify the source code from within GitHub, and automatically trigger an image build in ACR, and have Azure Web App Continuous Deployment update the container image used.
This is a three part article series.
In this first part, we look at cloning a sample Docker Container based on Python Django. We test this Container Image by running it to ensure it operates correctly. We then create a new GitHub Repository and push our Docker Container Source Code into your own GitHub Repository.
In the second part, we will take a look at creating an ACR, pushing our image up to ACR, and setting up Azure Web App to use our container image and test the website for production readability.
In the third part, we will take a look at making an update to our container image in a way as to not affect our original image, we test our new container image, we set up our ACR Task with a trigger for Source Code, we set up our Azure Web App for Continuous Deployment, push our updated source code to GitHub, validate our ACR Task detected the change, validate ACR builds the new image, validate Azure Web App pulls the updated image, and finally validate our Azure Web App website is reflecting the new production change.
Clone a Sample Repository and Test Docker Image
Let’s clone a sample repository to your local machine using the following command:
git clone https://github.com/Azure-Samples/docker-django-webapp-linux.git --config core.autocrlf=input
Let’s build the docker image and test it before we push this code up to GitHub. This will require you to have Docker installed. For Windows/Mac Users, install Docker Desktop which you can download here.
You can see currently, there are no local docker containers or images.
Start by executing the following docker command from within the docker-django-webapp-linux directory:
docker build --tag mydockerwebimage .
The docker image is now built.
We can see our mydockerwebimage image has successfully been created. The reason we also see the python image is because in our dockerfile, we are building our image from the base python image. So in order to create our image from the python base image, the python base image is also downloaded.
Now let’s run our mydockerwebimage image to ensure that it runs successfully before we push the source code out to GitHub and set up our Azure Web App for containers.
We do this by running the following command:
docker run -p 8000:8000 mydockerwebimage
If we launch a browser and go to https://127.0.0.1:8000, we can see our website as well as the streaming log in the Command Prompt.
The reason we are going to port 8000, is that our DockerFile exposes port 8000. When we execute docker run with -p 8000:8000, the first port is the host port and the second port is the container port. It is important to ensure the 2nd port, the container port, matches one of the ports defined under EXPOSE within your dockerfile.
Push our Source Code into a GitHub Repository
In GitHub, create a new Repository. I called mine, ACRTasksBlog.
I also created a new directory on my local file system called ACRTasksBlog and copied all the source code to it.
To associate this directory with the new GitHub Repository, I run the following:
echo "# ACRTasksBlog" >> README.md git init git add README.md git commit -m "first commit" git remote add origin https://github.com/ElanShudnow/ACRTasksBlog.git git push -u origin master
Then to add all the source code into GitHub, run the following commands:
git add . git commit -m "Adding docker source code" git push
We can now successfully see all our code in our GitHub Repository.
In Part 2, we take a look at creating an ACR, pushing our image up to ACR, and setting up Azure Web App to use our container image and test the website for production readability.