In this blog, I will walk through the steps to deploy a FastAPI application to Azure. We'll start by creating an Azure Container Registry (ACR), then build a Docker image of our FastAPI app, and finally push the image to the ACR. Let's get started!
Prerequisites
- An Azure account
- Azure CLI installed -Optional Since i am going to create resources via Azure Portal .
- Docker Desktop installed
- FastAPI - Python app to deploy
- DockerFile - This file has docker commands to build the docker image here is the sample dockerfile
# Choose our version of Python
FROM python:3.9
# Set up a working directory
WORKDIR /code
# Copy just the requirements into the working directory so it gets cached by itself
COPY ./requirements.txt /code/requirements.txt
# Install the dependencies from the requirements file
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
# Copy the code into the working directory
COPY ./app /code/app
# Tell uvicorn to start spin up our code, which will be running inside the container now
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "80"]
First logon to Portal.azure.com ** create resource **container registry. Follow the steps
Copy the server name , user ID, Password
Now from your PC command like login with above credentials as show in below command
$:> *docker login sreeni.azurecr.io -u sreeni -p Uxd2+xZNyKBOxUQCO12qleTi4XnuW20It8sYDsu4eL+ACRAsL5 *
Then build your image with the following command , please make sure you take with Azure server name sreeni.azurecr.io (ACR Registry )
*docker build -t sreeni.azurecr.io/sreenifastapi:build-tag-1 .
*
Once image is created successfully push the image to Azure ACR repositories using the following command
*docker push sreeni.azurecr.io/sreenifastapi:build-tag-1
*
Now we need to create a Azure container instance and deploy the image we pushed to ACR as shown below
After deployment go to the resource and copy the IP address and browse it
In this blog, we successfully deployed a FastAPI application to Azure. We created an Azure Container Registry, built a Docker image, pushed it to ACR, and deployed it to Azure Container Instances. You can now access your FastAPI app running on Azure.
Happy deploying!
Thanks
Sreeni Ramadurai
Top comments (2)
What can the FastAPI do ?
Its a Framework to build REST API in python., Same as Flask. However this is faster Async In Nature .