simple-fastapi-container
                                
                                 simple-fastapi-container copied to clipboard
                                
                                    simple-fastapi-container copied to clipboard
                            
                            
                            
                        A simple FastAPI app that is containerized and designed for development on Azure Container Apps.
Simple FastAPI container
This repository includes a simple Python FastAPI app with a single route that returns JSON. You can use this project as a starting point for your own APIs.
The repository is designed for use with Docker containers, both for local development and deployment, and includes infrastructure files for deployment to Azure Container Apps. 🐳
The code istested with pytest, linted with ruff, and formatted with black. Code quality issues are all checked with both pre-commit and Github actions.
Opening the project
This project has Dev Container support, so it will be be setup automatically if you open it in Github Codespaces or in local VS Code with the Dev Containers extension.
If you're not using one of those options for opening the project, then you'll need to:
- 
Create a Python virtual environment and activate it. 
- 
Install the requirements: python3 -m pip install -r requirements-dev.txt
- 
Install the pre-commit hooks: pre-commit install
Local development
- 
Run the local server: fastapi dev src/api/main.py
- 
Click 'http://127.0.0.1:8000' in the terminal, which should open a new tab in the browser. 
- 
Try the API at '/generate_name' and try passing in a parameter at the end of the URL, like '/generate_name?starts_with=N'. 
Local development with Docker
You can also run this app with Docker, thanks to the Dockerfile.
You need to either have Docker Desktop installed or have this open in Github Codespaces for these commands to work. ⚠️ If you're on an Apple M1/M2, you won't be able to run docker commands inside a Dev Container; either use Codespaces or do not open the Dev Container.
- 
Build the image: docker build --tag fastapi-app ./src
- 
Run the image: docker run --publish 3100:3100 fastapi-app
Deployment
This repo is set up for deployment on Azure Container Apps using the configuration files in the infra folder.
This diagram shows the architecture of the deployment:

Steps for deployment:
- 
Sign up for a free Azure account and create an Azure Subscription. 
- 
Install the Azure Developer CLI. (If you open this repository in Codespaces or with the VS Code Dev Containers extension, that part will be done for you.) 
- 
Login to Azure: azd auth login
- 
Provision and deploy all the resources: azd upIt will prompt you to provide an azdenvironment name (like "fastapi-app"), select a subscription from your Azure account, and select a location (like "eastus"). Then it will provision the resources in your account and deploy the latest code. If you get an error with deployment, changing the location can help, as there may be availability constraints for some of the resources.
- 
When azdhas finished deploying, you'll see an endpoint URI in the command output. Visit that URI, and you should see the API output! 🎉
- 
When you've made any changes to the app code, you can just run: azd deploy
Costs
Pricing varies per region and usage, so it isn't possible to predict exact costs for your usage. The majority of the Azure resources used in this infrastructure are on usage-based pricing tiers. However, Azure Container Registry has a fixed cost per registry per day.
You can try the Azure pricing calculator for the resources:
- Azure Container App: Consumption tier with 0.5 CPU, 1GiB memory/storage. Pricing is based on resource allocation, and each month allows for a certain amount of free usage. Pricing
- Azure Container Registry: Basic tier. Pricing
- Log analytics: Pay-as-you-go tier. Costs based on data ingested. Pricing
⚠️ To avoid unnecessary costs, remember to take down your app if it's no longer in use,
either by deleting the resource group in the Portal or running azd down.
Getting help
If you're working with this project and running into issues, please post in Discussions.