1

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

7
  • The port 8000 TCP is opened in the network section. Commented Mar 19, 2024 at 23:29
  • Atienzar did your issue is resolved? Commented Mar 19, 2024 at 23:39
  • No, is not solved Commented Mar 19, 2024 at 23:41
  • If you have uploaded a working container, then what is the difference between the command you use to run it locally versus on Azure Container Instances? Commented Mar 20, 2024 at 3:57
  • Locally I execute with docker run name-of-image as usual. In ACI you don't need to run the Docker, it is executed by default and when you click the "play button" Commented Mar 20, 2024 at 8:41

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your time. The process you indicate is the same that I have took normally, except I used docker login XXXX.azurecr.io -u <user> -p <password> instead of the method proposed by you to login: az acr login.... This last time I have followed your steps carefully and I've got the same result :( . Mi size is the size by default: 1 vcpu, 1.5 GiB memory, 0 gpus. I don't think I need more for this simple API.
The message I got in the "overview" section is a warning that says: "One or more of the containers in 'fastapi-test' are in a 'Waiting' States and may not be running. Click here to view container statuses"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.