Giter Site home page Giter Site logo

flask-webapp-quickstart's Introduction

This tutorial shows you how to create and deploy a Flask web app in an Alpine Linux + NGINX Docker container to Azure Web Apps for Containers using the Azure CLI.

See VSCODE.md for using Visual Studio Code and the Azure Portal instead.

Prerequisites

To complete this tutorial:

Create app and run it locally

First create a root folder, name it e.g. flask-quickstart and create a single app folder inside the root folder. Now let's write our app, copy and paste the following code into app/main.py:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
  return 'Hello, World!'

if __name__ == '__main__':
  app.run()

Now let's create a dockerfile for the app, we'll use an Alpine Linux container with an NGINX web server. Put the following in a file named Dockerfile inside of the flask-quickstart folder:

FROM tiangolo/uwsgi-nginx-flask:python3.6-alpine3.7

ENV LISTEN_PORT=8000
EXPOSE 8000

COPY /app /app

Now build and run the docker container:

docker build --rm -t flask-quickstart .
docker run --rm -it -p 8000:8000 flask-quickstart

Open a web browser, and navigate to the sample app at http://localhost:8000.

You can see the Hello World message from the sample app displayed in the page.

Flask app running locally

In your terminal window, press Ctrl+C to exit the web server and stop the container.

If you make code changes you can re-run the docker build and run commands above to update the container.

Deploy the container to Azure

For this portion of the tutorial you will need the Azure CLI. Either install it locally, or you can run commands in the browser by navigating to the Azure Cloud Shell. As you are running the commands below, you can view the resources being created in the Azure portal by navigating to portal.azure.com.

Create a resource group:

az group create --name FlaskApp --location "West US"

Create a container registry and retrieve the password, note that <registry_name> needs to be a unique name:

az acr create --name <registry_name> --resource-group FlaskApp --location "West US" --sku Basic --admin-enabled true
az acr credential show -n <registry_name>

You see two passwords. Make note of the user name and the first password.

{
  "passwords": [
    {
      "name": "password",
      "value": "<registry_password>"
    },
    {
      "name": "password2",
      "value": "<registry_password2>"
    }
  ],
  "username": "<registry_name>"
}

Log in to your registry. When prompted, supply the username and password shown above.

docker login <registry_name>.azurecr.io -u <registry_name>

Tag your container and push it to the registry:

docker tag flask-quickstart <registry_name>.azurecr.io/flask-quickstart
docker push <registry_name>.azurecr.io/flask-quickstart

Create the app service plan:

az appservice plan create --name FlaskAppPlan --resource-group FlaskApp --sku B1 --is-linux

Create the web app:

az webapp create --name <app_name> --resource-group FlaskApp --plan FlaskAppPlan --deployment-container-image-name "<registry_name>.azurecr.io/flask-quickstart"

Configure it to pull from the registry:

az webapp config container set --name <app_name> --resource-group FlaskApp --docker-custom-image-name <registry_name>.azurecr.io/flask-quickstart --docker-registry-server-url https://<registry_name>.azurecr.io --docker-registry-server-user <registry_name> --docker-registry-server-password <registry_password>

Run the following command to set the port number on the site and restart it:

az webapp config appsettings set --name <app name> --resource-group FlaskApp --settings  WEBSITES_PORT=8000
az webapp restart --name <app name> --resource-group FlaskApp

Browse to the web app at http://<app_name>.azurewebsites.net

Install additional libraries

To install additional libraries, first create a virtual environment locally, install packages and then generate a requirements.txt file.

On Windows:

py -3 -m venv env
env\scripts\activate
pip install flask <list of other libraries>
pip freeze > requirements.txt
deactivate

On Linux/Unix/macOS:

python3 -m venv env
env/bin/activate
pip install flask <list of other libraries>
pip freeze > requirements.txt
deactivate

Add the following code to the dockerfile so that the additional requirements are installed:

# Install additional requirements from a requirements.txt file
COPY requirements.txt /
RUN pip install --no-cache-dir -U pip
RUN pip install --no-cache-dir -r /requirements.txt

Clean up

To clean up your Azure resources, delete the resource group az group delete --name FlaskApp

flask-webapp-quickstart's People

Contributors

qubitron avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.