Deploying Flask Apps to Azure Web App via Docker Hub
This tutorial will explore a step-by-step approach to deploying a Python-based Flask web application to Azure Web App using Docker Hub. Our project, a book recommendation system “BookBuddy”, represents a collaborative effort between myself, Haliunna Munkhuu, and Lilly Grella. We will guide you through each phase of the project: from the initial development of the recommendation logic in Python to creating the Flask framework, packaging our application into a Docker image, and finally deploying it to Azure.
Development with Flask on a Local IDE
Before we start, ensure that Python and pip are installed on our system. Then, install Flask using pip:
pip install Flask
Create a file named `flask_app.py` for our application “BookBuddy”. This code initializes the Flask app and sets up a simple route:
from flask import Flask
app = Flask(__name__)
@app.route(‘/’)
def index():
return “Welcome to BookBuddy!”
To ensure our Flask app is functioning correctly, we write a simple unit test using Python’s built-in `unittest` framework:
import unittest
class TestBookRecommendation(unittest.TestCase):
def setUp(self):
# Set up any variables you need for your tests
self.test_genre = “Cookbooks”
Containerization with Docker
Next, we need to create a Dockerfile to specify the environment of our Flask application:
# Dockerfile content
With Docker Desktop installed, build the Docker image with the following command:
docker build -t bookbuddy:latest .
After signing up and logging into Docker Hub, create a repository named `bookbuddy`, and ensure we have our tag name in lowercase:
docker login
docker tag bookbuddy:latest <your-docker-hub-username>/bookbuddy:latest
docker push <your-docker-hub-username>/bookbuddy:latest
Infrastructure as Code with Terraform
Before we deploy our Flask application to Azure, we need to set up the necessary infrastructure. We use Terraform, an open-source infrastructure as a code software tool, to write, plan, and create infrastructure efficiently.
Install Terraform on our local machine, we can refer to the Terraform official website.
Create a new directory for our Terraform configuration files. Within this directory, run:
terraform init
This command initializes a new Terraform project and sets up the necessary plugins.
Define our Azure resources in a file named `main.tf`. This file should include our Azure provider, resource groups, and App Service resources.
# main.tf content with Azure provider and resources
Execute the following command to see the execution plan, which shows what Terraform will do when we apply our configuration
terraform plan
If the plan looks correct, deploy our infrastructure with:
terraform apply
Confirm the action when prompted, and Terraform will begin creating the resources. Once Terraform has finished applying the changes, check the Azure portal to see our new resources.
Azure Web App Deployment
First, we need to install the Azure CLI. This can be done from the official website or via package managers:
# Azure CLI installation commands
Log into our Azure portal. From the Azure services dashboard, click on “App Services” to start creating a new Web App.
In the App Services section, click on “Create” and select “Web App”.
Create Web App
Fill out the “Instance Details” section with our web app’s name, publish settings, and operating system. For example, choose “Docker Container” and the region closest to ourselves for optimal performance.
Review our settings and click “Create” to provision the Web App with our configurations.
Once the Web App is provisioned, we will be directed to the deployment overview page. This page will indicate that our deployment is in progress.
After a short while, we will receive a notification that your deployment is complete. Click “Go to resource” to manage your deployed Web App.
In the Web App management section, we can view our Web App’s details, such as the default domain, status, and location. Here, we can also manage domain settings, scaling, and deployment slots
Navigate to the default domain provided by Azure to view our running Flask application. We should see the landing page of our “BookBuddy” application.
Conclusion
We’ve now successfully deployed a Flask app to Azure Web App using a Docker container. Our book recommendation system is live and accessible. Toggle “On” for continuous deployment in Azure to allow automatic redeployment whenever the Docker image is updated on Docker Hub. For any updates, push the new Docker image to Docker Hub and Azure will handle the rest.
Microsoft Tech Community – Latest Blogs –Read More