I get the error: python: can't open file 'app.py': [Errno 2] No such file or directory when I try to create a deployment. I have included the folder structure, deployment and PVC manifests.
When I create a container from the docker image which I built using the docker file below, it runs fine - STATUS: Running.
I suspect it might have something to do with the persistent volumes or the way I have written my paths. I have tried the long-form (/var/www/code/order_service/app..) for my paths as well but face the same issue.
I'll appreciate any help. Thanks in advance guys.
Docker File
FROM python:3-alpine3.10
COPY ./app/requirements.txt /app/requirements.txt
WORKDIR /app
RUN apk add --update \
bash \
curl \
py-mysqldb \
gcc \
libc-dev \
mariadb-dev \
nodejs \
npm \
&& pip install --upgrade pip \
&& pip install -r requirements.txt \
&& rm -rf /var/cache/apk/*
COPY ./app/package.json /app/package.json
RUN npm install
COPY ./app /app
CMD ["python", "app.py"]
Folder structure
code
order_service
app
app.py
Here is my manifest:
DEPLOYMENT
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
io.kompose.service: order
name: order
spec:
replicas: 1
selector:
matchLabels:
io.kompose.service: order
strategy:
type: Recreate
template:
metadata:
creationTimestamp: null
labels:
io.kompose.service: order
spec:
containers:
- image: order:1.0
imagePullPolicy: IfNotPresent
name: order
ports:
- containerPort: 5000
resources: {}
volumeMounts:
- mountPath: ./app
name: order-claim0
restartPolicy: Always
volumes:
- name: order-claim0
persistentVolumeClaim:
claimName: order-claim0
status: {}
PVC
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
creationTimestamp: null
labels:
io.kompose.service: order-claim0
name: order-claim0
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 100Mi
status: {}
I didn't get the point.
In Dockerfile
, you put app.py
in docker image's folder /app
WORKDIR /app
COPY ./app /app
CMD ["python", "app.py"]
Then in Kubernetes, you try to replace the folder /app
with a persistent volume.
But how the first one comes from?
volumeMounts:
- mountPath: ./app
name: order-claim0
So that's the reason, when you run locally with that docker image, it is fine, but when you run a similar command as below, it will be failed.
docker run -ti --rm -v $(New_Local_folder_somewhere):/app order:1.0
Because the folder /app
has been replaced with a totally new mounted folder.
Second, could you use absolute path more than relative path in this case?
- mountPath: ./app
change to
- mountPath: /app