Showing posts with label azure. Show all posts
Showing posts with label azure. Show all posts

How to Deploy FastAPI Docker Image in Azure

 How to Deploy FastAPI Docker Image in Azure


Introduction:

Following tutorial will guide you in deployment of FastAPI Docker Image in Microsoft Azure Portal( Cloud). 
Following article will guild you all details from writing sample FastAPI code, Converting FastAPI API to Docker Image and Deployment of Docker image to azure means from Scratch to details, everything.
Following tutorial will also help in deployment of Docker image in others cloud platform such as: AWS, Google Cloud, etc.

Prerequisites:

Following Prerequisites are required for docker image deployment in Azure: 
  • Internet Connectivity.
  • Azure Account (www.portal.azure.com):
    • Azure App Service (Linux, F1: Free or B1: Basic)
    • Container registry.
    • Web app for container.

FastAPI Code:

  • To install FastAPI in your system please follow my earlier article:- FastAPI Installation.
  • Create Following folder structure in your local folder:
                
project folder structure




  • In main.py file write following code:

main.py file


  • In Dockerfile file write following code:
        FROM python:3.7                                                                                       
RUN pip install fastapi uvicorn                                                                  
EXPOSE 15400                                                                                          
COPY ./app /app                                                                                         
CMD [ "uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "15400" ]

Dockerfile



  • In docker-compose.yml file write following code:
    version: '3'                                                
services:                                               
    core_api:                                                  
        build: .                                                 
        container_name: "core-api-container"
        ports:                                                   
            - "8000:15400"                               
        volumes:                                             
            - ./app/:/app                                    





docker-compose.yml


  • Now to create your FastAPI docker image:
    • open terminal in vs code and follow following steps:
    • > docker-compose up -d
    • >  docker images (to check list of docker images)
  • Before pushing image to azure container, we have to login to azure container registry:
    • open terminal and type:
    • > docker login <azure container registry Login server name>
      • e.g > docker login registerdock.azurecr.io
    • username (username will get inside Setting tab-> access key option from container registry in azure)
    • password (same steps as username)
Once you enter username and password, will get login success..prompt
  • Now we have build and tag docker image for azure registry deployment:
    • move to project folder where Dockerfile is there and fire command:
    • > docker build -t <azure container registry Login server name>/<imageName>:latest .
      • e.g > docker build -t registerdock.azurecr.io/test_fastapi_core_api:latest .
  • To run tagged docker image in local:
    • > docker run -d -p 8000:15400 registerdock.azurecr.io/test_fastapi_core_api
    • Now open browser and type http://127.0.0.1:8000/9 here we can check our running docker image)
  • Pushing tagged docker image to azure container:
    • open terminal and type:
    • > docker push <tagged docker image name>:latest
      • e.g: > docker push registerdock.azurecr.io/test_fastapi_core_api:latest
    • Once you pushed docker image to azure container registry, we check here:









  • After successfully pushed image in container, we need web-app for end point:
    • Create "web app for container" from Resources in Azure and Select Image source as "Azure Container Registry" from drop-down.
    • For more details following pictorial representation:








Once you create web app for container you check your API running in browser:-

https://testfastapidocker.azurewebsites.net/
 

Conclusion:

 
Following article will guide you to azure deployment of FastAPI docker image. Let me know in comment if you guys facing any problem in deployment.






Read More

FastAPI Introduction

 

FastAPI


Introduction:

FastAPI is a new, high-performance, web framework for building APIs with Python. If we compare FastAPI with other web framework like Flask or Django, FastAPI is very fast and ready to deployment.

Key Features:

  • Fast:- Very high performance, on par with NodeJS and Go (thanks to Starlette and Pydantic). which is one of the fastest python frameworks available.
  • Fast to code:- Increase the speed to develop features by about 200% to 300% in comparison with other web framework.
  • Less Bugs:- Reduce about 40% of human (developer) induced errors.
  • Intuitive:- Great editor support. Completion everywhere. Less time debugging.
  • Easy:- Designed to be easy to use and learn. Less time reading docs.
  • Short:- Minimize code duplication. Multiple features from each parameter declaration. Fewer bugs.
  • Robust:- Get production-ready code. With automatic interactive documentation.
  • Standards-based:- Based on (and fully compatible with) the open standards for APIs: OpenAPI (previously known as Swagger) and JSON Schema.Based on (and fully compatible with) the open standards for APIs: OpenAPI (previously known as Swagger) and JSON Schema.  


Installation:

To install FastAPI, follow below steps:-
            
    Open terminal and fire below command:
    
            $ pip install fastapi

            we need an ASGI server for production such as uvicorn .

            $ pip install uvicorn

Example:

Create a file  namely, main.py and add below codes:

    from fastapi import FastAPI
from fastapi import FastAPIapp = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.get("/items/{item_id}")
def read_item(item_id: int, q: str = None):
return {"item_id": item_id, "q": q}

or we add async def also..

Run your code:

To run the server with below command:

    uvicorn main:app --reload

Its will start like this;

INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [28720]
INFO: Started server process [28722]
INFO: Waiting for application startup.
INFO: Application startup complete.


We will get the JSON response as:

            {"item_id": 5, "q": "anyquery"}

You already created an API that:

  • Receives HTTP requests in the paths / and /items/{item_id}
  • Both paths take GET operations (also known as HTTP methods).
  • The path /items/{item_id} has a path parameter item_id that should be an int
  • The path /items/{item_id} has an optional str query parameter q


Interactive API docs:

Now go to http://127.0.0.1:8000/docs.
You will see the automatic interactive API documentation (provided by Swagger UI):


Alternative API docs:

And now, go to http://127.0.0.1:8000/redoc.






Learn More:


Documentation:- https://fastapi.tiangolo.com/

Source Code:- https://github.com/tiangolo/fastapi








            





Read More

Visitor