Error when deploy a FastAPI Docker Image into Azure Container Instance

74 views Asked by At

I tried to deploy a instance of a simple FastAPI service into azure.

I have tried several ways, using uvicorn as server, using gunicorn with uvicorn workers, using an image from Docker Hub, uploading the image to Container Registry in Azure...

When I create the Docker image and run it at local it works perfectly, but then in Azure is not working.

This is my main.py

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
async def root():
    return {"msg": "Hola FastAPI!!"}


@app.get("/url")
async def url():
    return {"url": "http://whatever.es"}

This is my Dockerfile

# syntax=docker/dockerfile:1

FROM python:3.11

WORKDIR /code

COPY requirements.txt .

RUN pip install --no-cache-dir --upgrade -r requirements.txt

COPY . .

EXPOSE 8000

CMD ["gunicorn" ,"--config", "gunicorn.py", "main:app"]

This is the gunicorn.py configuration file:

# Gunicorn configuration file
import multiprocessing

bind = "0.0.0.0:8000"

worker_class = "uvicorn.workers.UvicornWorker"
workers = multiprocessing.cpu_count() * 2 + 1

This is my requirements.txt

fastapi
uvicorn
gunicorn

As I told you, all the process seem to be ok until the last step, after the deployment, I got this message and the server is not listening in the port 8000. In local it works perfectly.

enter image description here

As you can see it says that is in a waiting state.

I appreciate your help.

I have tried to execute my container image in an Azure Container Instance.

I am expecting to have my service running in :8000

The result is that is nothing running there

1

There are 1 answers

2
Aslesha Kantamsetti On

I used your code to create this project and successfully deployed the image to the Azure Container Registry.

Ensure that your ACI has sufficient CPU, memory, and other resources allocated to it.

Check the logs for your Azure Container Instance to see if there are any error messages or warnings.

Verify that the container images specified in your ACI configuration are accessible and properly configured.

gunicorn. py:

import multiprocessing
bind = "0.0.0.0:8000"
worker_class = "uvicorn.workers.UvicornWorker"
workers = (multiprocessing.cpu_count() * 2) + 1

I used the commands below to build and run FastAPI in a Docker.

docker build -t <ImageName> . 
docker run -p 8000:8000 <ImageName> 

I ran the commands below to push the Docker image to the Azure Container Registry.

az acr login --name <ContainerUserName> --userName <userName> --password <Password>
docker tag <ImageName> <AzureContainerRegistryName>.azurecr.io/<ImageName>:<tagName>
docker push <AzureContainerRegistryName>.azurecr.io/<ImageName>:<tagName>

I created an Azure Container Instance in the Azure Portal, as shown below.

enter image description here

enter image description here

Output:

enter image description here

enter image description here