Python3在无发行版镜像上出错[没有这样的文件]

qzwqbdag  于 2023-05-19  发布在  Python
关注(0)|答案(1)|浏览(116)

我在flask中使用了这个dockerfile:

FROM python:3.8-slim-buster

WORKDIR /python-docker

COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt

COPY . .

CMD [ "python3", "-m" , "flask", "run", "--host=0.0.0.0"]

它工作正常,在我的目录中,我有requirements.txt和一个非常简单的app.py文件:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_geek():
    return '<h1>Hello from Flask & Docker</h2>'

if __name__ == "__main__":
    app.run(debug=True)

我想把它作为一个无发行版镜像使用,所以我在这个dockerfile上构建:

FROM python:3.8-slim-buster as build-env

ADD / /app
WORKDIR /app

RUN pip3 install --upgrade pip
RUN pip install -r ./requirements.txt

FROM gcr.io/distroless/python3-debian10

COPY --from=build-env /app /app
COPY --from=build-env /usr/local/lib/python3.8/site-packages /usr/local/lib/python3.8/site-packages
WORKDIR /app

ENV PYTHONPATH=/usr/local/lib/python3.8/site-packages

CMD [ "python3", "-m" , "flask", "run", "--host=0.0.0.0"]

编译时没有错误,但当我运行时:sudo docker run python-dockerdistro
我得到这个错误:/usr/bin/python3.7:无法打开文件'python3':[错误2]没有该文件或目录
你知道如何解决这个错误吗?先谢谢你了

uyhoqukh

uyhoqukh1#

gcr.io/distroless/python3-debian10图像的ENTRYPOINT是Python 3:

$ docker image inspect -f '{{.Config.Entrypoint}}' gcr.io/distroless/python3-debian10
[/usr/bin/python3.7]
$ docker run --rm -it gcr.io/distroless/python3-debian10
Python 3.7.3 (default, Jan 22 2021, 20:04:44) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

但是,您的CMD[ "python3", "-m" , "flask", "run", "--host=0.0.0.0"]
你实际上是在尝试运行Python两次:

$ python3 python3
python3: can't open file '/tmp/python3': [Errno 2] No such file or directory

只需将CMD更改为仅["-m" , "flask", "run", "--host=0.0.0.0"]

相关问题