[PYTHON] Make Flask a Cloud Native application

This article is Request! Tips for developing on Azure using Python![PR] It is his 9th day of Microsoft Japan Advent Calendar 2020 (I will write it later).


It's about making Flask a Cloud Native application.

What is Flask?

Flask is a web application framework for Python.

How to make Flask a Cloud Native application?

The steps to turn Flask into a Cloud Native application are simple:

  1. Create a website in Flask
  2. Create a Dockerfile
  3. Build the Docker image
  4. Publish the Docker image
  5. Create Azure Web Apps using Docker image

An example of the execution command is described below.

1. Create a website in Flask

$ mkdir flaskwebapps
$ cd flaskwebapps
$ touch app.py
$ touch requirements.txt

Open app.py in your favorite editor such as VS Code and add the following code.

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello, Flask!"

if __name__ == "__main__":
    app.run(host='0.0.0.0')

Open requirements.txt in your favorite editor such as VS Code and add the following code.

Flask

2. Create a Dockerfile

$ touch Dockerfile

Open Dockerfile with your favorite editor such as VS Code and add the following code.

FROM python:3.8

RUN mkdir /app
WORKDIR /app
ADD . /app/
RUN pip install -r requirements.txt

EXPOSE 5000
CMD ["python", "/code/app.py"]

3. Build the Docker image

$ docker build -f Dockerfile -t flaskwebapps:latest .

4. Publish the Docker image

Publish the Docker image to Azure Container Registry (https://azure.microsoft.com/ja-jp/services/container-registry/?WT.mc_id=AZ-MVP-5001601) (ACR), Docker Hub, and so on.

Below, the image to be published to ACR with Azure CLI is registered.

$ az acr create --resource-group [Resource group name] --name [ACR name] --sku [Choice of favorite] --admin-enabled true --location [Same location as the resource group]
$ docker tag [tag] [Image name]
$ az acr login --name [ACR name]
$ docker push [Image name]

5. Create Azure Web Apps using Docker image

First, create an App Service Plan with the Azure CLI.

$ az appservice plan create --resource-group [Resource group name] --name [App Service Plan name] --sku [Specify B1 or S1] --location [Same location as the resource group] --is-linux

Create an App Service with the App Service Plan and ACR Docker Image as parameters in the Azure CLI

$ az webapp create --resource-group [Resource group name] --plan [App Service Plan name] --name [App Service name] --deployment-container-image-name [Image name]

Then associate the App Service with the ACR in the Azure CLI.

$ az webapp config container set --name [App Service name] --resource-group [Resource group name] --docker-custom-image-name [Image name] --docker-registry-server-url [ACR location] --docker-registry-server-user [ACR name] --docker-registry-server-password [ACR Password]

Then set the CD for App Service in the Azure CLI.

$ az webapp deployment container config --resource-group [Resource group name] --name [App Service name] --enable-cd true

Then set the webhook on the ACR in the Azure CLI.

$ az acr webhook create --name [App Service name] --resource-group [Resource group name] --scope [ACR name and tag minus location] --location [Same location as the resource group] --uri [URI] --actions push delete

Recommended Posts

Make Flask a Cloud Native application
Creating a web application using Flask ②
Creating a web application using Flask ①
Creating a web application using Flask ③
Creating a web application using Flask ④
Let's make a WEB application for phone book with flask Part 1
Let's make a WEB application for phone book with flask Part 2
Let's make a WEB application for phone book with flask Part 3
Let's make a WEB application for phone book with flask Part 4
Make a rare gacha simulator with Flask
Flask application settings
[GCP] Procedure for creating a web application with Cloud Functions (Python + Flask)
I want to make a web application using React and Python flask
Make a simple pixel art generator with Flask
Let's make an A to B conversion web application with Flask! From scratch ...
Make a squash game
Make a morphological analysis bot loosely with LINE + Flask
How to deploy a Django application on Alibaba Cloud
Make a function decorator
Make a distance matrix
I'll make a password!
Make a Nyan button
Make a Tetris-style game!
Make a Base64 decoder
Flask introduction-Blueprint application memo
How to make a Cisco Webex Teams BOT with Flask
Let's make a Discord Bot.
Make a Blueqat backend ~ Part 1
Make a Blueqat backend ~ Part 2
[Django] Make a pull-down menu
Make a bookmarklet in Python
Make a fortune with Python
Web application with Python + Flask ② ③
Let's make a rock-paper-scissors game
Make a fire with kdeplot
Web application with Python + Flask ④
Make a math drill print
If you know Python, you can make a web application with Django
Build a Flask / Bottle-like web application on AWS Lambda with Chalice
Build a detonation velocity website with Cloud Run and Python (Flask)
I tried to make a 2channel post notification application with Python
I tried to make a todo application using bottle with python
How to deploy a web application on Alibaba Cloud as a freelancer