Code Monkey home page Code Monkey logo

msdocs-python-flask-azure-container-apps's Introduction

Deploy a Python (Flask) app to Azure to Azure Containers Apps

This Python web app is a simple restaurant review application built with the Flask framework. The web app stores application data in PostgreSQL with environment variables defining the connection info.

This repo was created to be built to a Docker image and run as a container instance in Azure Container Apps. For more information, see the tutorial Deploy a Python web app on Azure Container Apps with GitHub Actions.

This Python web app repo can also be used in other ways:

  • You can run the web app locally in a virtual environment. Make sure to define .env file with environment settings.

    • In the .env file, fill in a secret value for SECRET_KEY. You can use this command to generate an appropriate value for your product deployment:
    python -c 'import secrets; print(secrets.token_hex())'
  • You can create a container locally and run it in Docker locally. You'll need Docker Desktop installed. For this scenario, set REMOTE_POSTGRESQL=1 in .env file to point to a PostgreSQL instance. See the .env.example file for details.

    docker build --file Dockerfile --tag pythoncontainer:latest .
    docker run -it --env-file .env --publish 5000:5000/tcp pythoncontainer:latest

    If you want to use PostgreSQL instance locally, add --add-host to the Docker command. For more information, see the Docker run command. For an example of how to do this with MongoDB, see Build and test a containerized Python web app locally.

  • You can deploy the code (not a container) to App Service. For guidance on how to deploy code, see Quickstart: Deploy a Python (Django or Flask) web app to Azure App Service and Overview: Deploy a Python web app to Azure with managed identity.

  • You can create a Docker image from this repo and host the container instance in Web Apps for Containers (App Service). See Overview: Containerized Python web app on Azure.

If you need an Azure account, you can create on for free.

A Django sample application with similar functionality is at https://github.com/Azure-Samples/msdocs-python-django-azure-container-apps.

Requirements

The requirements.txt has the following packages:

Package Description
Flask Web application framework.
SQLAlchemy Provides a database abstraction layer to communicate with PostgreSQL.
Flask-SQLAlchemy Adds SQLAlchemy support to Flask application by simplifying using SQLAlchemy. Requires SQLAlchemy.
Flask-Migrate SQLAlchemy database migrations for Flask applications using Alembic. Allows functionality parity with Django version of this sample app.
pyscopg2-binary PostgreSQL database adapter for Python.
gunicorn WSGI HTTP Server for UNIX. Required for running containers locally in VS Code.
python-dotenv Read key-value pairs from .env file and set them as environment variables. In this sample app, environment variables describe how to connect to the database and storage resources. Because managed identity is used no sensitive information is included in environment variables.

Flask's dotenv support sets environment variables automatically from an .env file.
flask_wtf Form rendering, validation, and CSRF protection for Flask with WTForms. Uses CSRFProtect extension.

The steps to do this are covered more completely in the tutorial Deploy a Python web app on Azure Container Apps with GitHub Actions. Briefly, here are the steps:

  1. Fork and then clone locally.
  2. Build a container image from the repo.
  3. Create a PostgreSQL Flexible Server instance.
  4. Create a database on the server.
  5. Deploy the web app container to Azure Container Apps.
  6. Configure continuous deployment.

See the create_resources.sh Bash script for automating the creation of these steps.

msdocs-python-flask-azure-container-apps's People

Contributors

diberry avatar kjaymiller avatar microsoft-github-operations[bot] avatar microsoftopensource avatar pamelafox avatar v-geberr avatar vmagelo avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  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.