简体   繁体   English

gunicorn:找不到命令 Flask

[英]gunicorn: command not found Flask

I have created a flask app and it is running fine locally using this command gunicorn --workers 2 -b:5000 wsgi:app我创建了一个 flask 应用程序,它使用此命令在本地运行良好gunicorn --workers 2 -b:5000 wsgi:app
Now I'm trying to run the same application in docker and I'm getting ./gunicorn_starter.sh: line 3: gunicorn: command not found .现在,我尝试在 docker 中运行相同的应用程序,我得到./gunicorn_starter.sh: line 3: gunicorn: command not found

my dockerfile:我的 dockerfile:

FROM python:3.5

WORKDIR /app

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

COPY . .
RUN chmod +x gunicorn_starter.sh
CMD ["./gunicorn_starter.sh" ]

gunicorn_starter.sh: gunicorn_starter.sh:

#!/bin/sh
source venv/bin/activate
gunicorn --workers 2 -b :5000 wsgi:app

wsgi.py: wsgi.py:

from app import app

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

File structure:文件结构:

flask_app
    >venv
    dockerfile
    gunicorn_starter.sh
    app.py
    wsgi.py
    requirements.txt

I do have gunicorn in require.nets.txt file.我在 require.nets.txt 文件中确实有 gunicorn。
I have tried following links:我试过以下链接:
gunicorn not found when running a docker container with venv 使用 venv 运行 docker 容器时找不到 gunicorn
Installed gunicorn but it is not in venv/bin folder 安装了 gunicorn 但它不在 venv/bin 文件夹中
Gunicorn throwing error - cannot find executable on $PATH (Docker/Nginx) Gunicorn 抛出错误 - 在 $PATH (Docker/Nginx) 上找不到可执行文件

I couldn't figure out why it is not working, please help Thanks.我无法弄清楚为什么它不起作用,请帮助谢谢。

Probably the most significant issue in what you show is that you're trying to COPY your host's virtual environment in the venv directory into the Docker image, but virtual environments aren't portable across installations.您显示的内容中最重要的问题可能是您试图将COPY目录中主机的虚拟环境venv到 Docker 图像中,但虚拟环境不可跨安装移植。 So if the non-standard source command succeeds (if /bin/sh is actually GNU bash) you could be trying to run ./venv/bin/gunicorn , but that's connected to a Python installation that doesn't exist inside the container.因此,如果非标准source命令成功(如果/bin/sh实际上是 GNU bash),您可能会尝试运行./venv/bin/gunicorn ,但它连接到容器内不存在的 Python 安装。

The first thing you should do is write a .dockerignore file that includes the line您应该做的第一件事是编写一个包含以下行的.dockerignore文件

venv

That will keep your host-based virtual environment out of the image build.这将使您的基于主机的虚拟环境远离映像构建。 This avoids problems where the Python doesn't match (maybe it's not even the same OS) and also will make the docker build sequence run significantly faster.这避免了 Python 不匹配的问题(甚至可能不是相同的操作系统),并且还会使docker build序列运行得更快。

The Docker image is isolated from other Python installations by virtue of being in Docker, so you don't need a virtual environment here. Docker镜像由于在Docker中,所以与其他Python安装是隔离的,所以这里不需要虚拟环境。 One standard approach is to install packages into the "system" Python, but inside the isolated image.一种标准方法是将软件包安装到“系统”Python 中,但在隔离映像中。 This means you don't need to "activate" anything and you can dispense with the shell-script wrapper as well.这意味着您不需要“激活”任何东西,您也可以省去 shell 脚本包装器。

A simplified Dockerfile could look like:简化的 Dockerfile 可能如下所示:

FROM python:3.5

WORKDIR /app

COPY requirements.txt ./              # can skip second filename
RUN pip3 install -r requirements.txt  # without --user

COPY . .

CMD gunicorn --workers 2 -b :5000 wsgi:app  # without shell-script wrapper

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM