DEV Community

Seenivasa Ramadurai
Seenivasa Ramadurai

Posted on

Easy way to deploy FastAPI In Azure

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

  1. An Azure account
  2. Azure CLI installed -Optional Since i am going to create resources via Azure Portal .
  3. Docker Desktop installed
  4. FastAPI - Python app to deploy
  5. 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"]
Enter fullscreen mode Exit fullscreen mode

First logon to Portal.azure.com ** create resource **container registry. Follow the steps

Image description

Image description

Image description

Copy the server name , user ID, Password

Image description

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

Image description

After deployment go to the resource and copy the IP address and browse it

Image description

Image description

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)

Collapse
 
daniel_wei_c4f19869b5775a profile image
Daniel Wei

What can the FastAPI do ?

Collapse
 
sreeni5018 profile image
Seenivasa Ramadurai

Its a Framework to build REST API in python., Same as Flask. However this is faster Async In Nature .