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
It’ll depend on the file name – if for example it was called server.py then the string you should use is "server:app"
Add the directory name in front of filename i.e; if your directory name is
app
change
main:app
toapp.main:app
so inCMD
of Dockerfile this will beIn addition you can check this SO post.
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:
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 withI’ve made a mess of my directories 🙁
The correct
volumes
mount is:Hope this helps someone else!