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:
An example of the execution command is described below.
$ 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
$ 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"]
$ docker build -f Dockerfile -t flaskwebapps:latest .
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]
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