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)
- 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:
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.