skip to Main Content

I am trying to deploy python fastApi in EKS cluster (I am able to test this code in my local system), whenever I am deploying the docker image, its failing with error as

"

INFO:     Will watch for changes in these directories: ['/app']
INFO:     Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [1] using statreload
ERROR:    Error loading ASGI app. Attribute "app" not found in module "main".

"

I have created the docker image and pushed it to local repository, during deployment I am able to pull the image but not able to create container and when I checked Pod logs I got above error message .
My main.py file content-

from typing import Optional
from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def read_root():
    return {"Hello": "World"}

@app.get("/items/{item_id}")
def read_item(item_id: int, q: Optional[str] = None):
    return {"item_id": item_id, "q": q}

My docker file-

FROM python:3.9.5
COPY . /app
COPY .pip /root
WORKDIR /app

RUN pip3 install -r docker_req.txt

#COPY ./main.py /app/
#COPY ./__init__.py /app/

CMD ["uvicorn", "main:app", "--reload", "--host", "0.0.0.0"]

Deployment file looks like this-

apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "1"
    meta.helm.sh/release-name: cti-datalake-poc
    meta.helm.sh/release-namespace: **********<replaced the name>
  generation: 1
  labels:
    app: cti-datalake-poc
    app.kubernetes.io/instance: cti-datalake-poc
    app.kubernetes.io/managed-by: Helm
    app.kubernetes.io/name: cti-datalake-poc
    app.kubernetes.io/version: 1.0.0
    helm.sh/chart: cti-datalake-poc-1.0.0
    version: 1.0.0
  name: cti-datalake-poc
  namespace: **********<replaced the name>
  resourceVersion: "******"

spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: cti-datalake-poc
      app.kubernetes.io/instance: cti-datalake-poc
      app.kubernetes.io/name: cti-datalake-poc
      version: 1.0.0
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: cti-datalake-poc
        app.kubernetes.io/instance: cti-datalake-poc
        app.kubernetes.io/name: cti-datalake-poc
        deployid: *****
        version: 1.0.0
    spec:
      containers:
      - image: develop-ctl-dl-poc/cti-datalake-poc:1.0.5.0
        imagePullPolicy: Always
        livenessProbe:
          failureThreshold: 3
          httpGet:
            path: /
            port: http
            scheme: HTTP
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 1
        name: cti-datalake-poc
        ports:
        - containerPort: 5000
          name: http
          protocol: TCP
        readinessProbe:
          failureThreshold: 3
          httpGet:
            path: /
            port: http
            scheme: HTTP
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 1
        resources:
          limits:
            cpu: 200m
            memory: 256Mi
          requests:
            cpu: 100m
            memory: 128Mi
        securityContext: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      imagePullSecrets:
      - name: ***<name removed>
      restartPolicy: Always
      schedulerName: default-scheduler

      serviceAccount: default
      serviceAccountName: default
      terminationGracePeriodSeconds: 30

requirement.txt is

fastapi==0.73.0
pydantic==1.9.0
uvicorn==0.17.0

Any help is appreciated.

3

Answers


  1. It’ll depend on the file name – if for example it was called server.py then the string you should use is "server:app"

    Login or Signup to reply.
  2. Add the directory name in front of filename i.e; if your directory name is app

    change main:app to app.main:app so in CMD of Dockerfile this will be

    CMD ["uvicorn", "app.main:app", "--reload", "--host", "0.0.0.0"]
    

    In addition you can check this SO post.

    Login or Signup to reply.
  3. I had this same problem, but I was trying to run a FastAPI app with docker-compose. Turns out I had mounted my local directories wrong.

    This was my docker-compose yml:

    
    version: "3.3"
    
    services:
      fastapi_app:
        build:
          context: .
          dockerfile: FastAPI-Dockerfile
        volumes:
          - ./:/code/app:z
        ports:
          - "80:80"
        tty: true
    

    See the error in volumes? I’m mounting the whole project directory in /code/app in the container. Since my project directory already has an /app folder in it, the only way uvicorn can find it is with

    CMD ["uvicorn", "app.app.main:app", "--reload", "--host", "0.0.0.0"]
    

    I’ve made a mess of my directories 🙁

    The correct volumes mount is:

        volumes:
          - ./app:/code/app:z
    

    Hope this helps someone else!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search