【问题标题】:can't open file app. Kubernetes and Docker无法打开文件应用程序。 Kubernetes 和 Docker
【发布时间】:2020-01-22 01:20:12
【问题描述】:

我得到错误:python: can't open file 'app.py': [Errno 2] No such file or directory部署。我已经包含了文件夹结构、部署和 PVC 清单。

当我从使用下面的 docker 文件构建的 docker 映像创建容器时,它运行良好 - 状态:正在运行。

我怀疑这可能与持久卷或我编写路径的方式有关。我也为我的路径尝试了长格式(/var/www/code/order_service/app..),但面临同样的问题。

我将不胜感激。在此先感谢各位。

Docker 文件

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"]

文件夹结构

code
  order_service
    app
     app.py

这是我的清单:

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: {}

【问题讨论】:

  • 尝试在基于镜像的容器中运行sh,查看文件是否存在,workdir是否正确。

标签: python docker kubernetes


【解决方案1】:

我没有明白这一点。

Dockerfile 中,您将app.py 放入docker 映像的文件夹/app

WORKDIR /app

COPY ./app /app

CMD ["python", "app.py"]

然后在 Kubernetes 中,您尝试将文件夹 /app 替换为持久卷。

但是第一个是怎么来的呢?

        volumeMounts:
        - mountPath: ./app
          name: order-claim0

所以这就是原因,当你用那个 docker 镜像在本地运行时,它很好,但是当你运行如下类似的命令时,它会失败。

docker run -ti --rm -v $(New_Local_folder_somewhere):/app order:1.0

因为文件夹/app 已被一个全新的挂载文件夹替换。

其次,在这种情况下,你可以使用绝对路径而不是相对路径吗?

        - mountPath: ./app

change to 

        - mountPath: /app

【讨论】:

  • 感谢您的意见。现在说得通了。抱歉,我正在学习课程,我想那里有错误。我们想通过这个建议达到什么目的?------>>>> 其次,在这种情况下,您能否使用绝对路径而不是相对路径? - mountPath: ./app 更改为 - mountPath: /app
  • 你应该给答案投票,然后让 cmets 感谢。 :-)
  • 已投票。谢谢,宝马。你能帮忙解释一下这个问题吗?----->>>>我们想通过这个建议达到什么目的?----->>>> 其次,在这种情况下,你能否使用绝对路径而不是相对路径? - mountPath: ./app 更改为 - mountPath: /app
  • 阅读本文了解绝对路径和相关路径:geeksforgeeks.org/absolute-relative-pathnames-unix
猜你喜欢
  • 2017-11-16
  • 2020-02-09
  • 2021-01-04
  • 1970-01-01
  • 1970-01-01
  • 2017-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多