This Python app is a simple restaurant review application built with the Django framework. The 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 forSECRET_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 need Docker Desktop installed. For this scenario, set REMOTE_POSTGRESQL=1 and other environment variables in a .env file to point to a PostgreSQL server instance in Azure. You also need to create an application service principal, add it as a PostgreSQL ROLE to the server, and grant it necessary permissions on the database. See the .env.example file for details.
docker build --file Dockerfile --tag pythoncontainer:latest . docker run -it --env-file .env --publish 8000:8000/tcp pythoncontainer:latest
If you want to use PostgreSQL instance locally, you 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 image in Web Apps for Containers (App Service). See Containerized Python web app on Azure.
If you need an Azure account, you can create on for free.
A Flask sample application with similar functionality is at https://github.com/Azure-Samples/msdocs-python-flask-azure-container-apps.
The requirements.txt has the following packages:
Package | Description |
---|---|
Django | Web application framework. |
pyscopg2-binary | PostgreSQL database adapter for Python. |
gunicorn | WSGI HTTP Server for UNIX. Required for running containers 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. This package is used in the manage.py file to load environment variables. |
requests | Python HTTP for Humans. |
whitenoise | Static file serving for WSGI applications, used in the deployed app. This package is used in the azureproject/production.py file, which configures production settings. |
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:
- Fork and then clone locally.
- Create an Azure Container Registry.
- Build a container image from the repo.
- Create an Azure Database for PostgreSQL Flexible Server instance.
- Make your Azure user account a Microsoft Entra administrator on the server.
- Create a database on the server.
- Create a user-assigned managed identity.
- Add the managed identity as a ROLE on the PostgreSQL server.
- Assign the managed identity PostgreSQL permissions on the database.
- Deploy the web app container to Azure Container Apps.
- Migrate and create database schema by connecting to the container app and issuing the
python manage.py migrate
command. - Configure continuous deployment.
See the create_resources.sh Bash script for automating the creation of these steps. For best results, run the script in Azure Cloud Shell.