gpt4 book ai didi

python - 由于 ModuleNotFoundError : No module named 'wsgi' ,Flask 无法使用 Docker 启动服务器

转载 作者:行者123 更新时间:2023-12-02 19:00:40 32 4
gpt4 key购买 nike

我现在使用 Nginx 和 Docker 部署我的 Flask 应用程序。我的 docker 已启动并正在运行,但我总是得到 502 Bad Gateway nginx当去端点之一时。

所以我删除了所有 docker 并重新构建它,然后在运行 docker-compose up -d 时出现此错误:

[2020-03-09 15:00:11 +0000] [1] [INFO] Starting gunicorn 19.7.1
flask | [2020-03-09 15:00:11 +0000] [1] [INFO] Listening at: http://0.0.0.0:5000 (1)
flask | [2020-03-09 15:00:11 +0000] [1] [INFO] Using worker: sync
flask | [2020-03-09 15:00:11 +0000] [7] [INFO] Booting worker with pid: 7
flask | [2020-03-09 15:00:11 +0000] [7] [ERROR] Exception in worker process
flask | Traceback (most recent call last):
flask | File "/usr/local/lib/python3.6/site-packages/gunicorn/arbiter.py", line 578, in spawn_worker
flask | worker.init_process()
flask | File "/usr/local/lib/python3.6/site-packages/gunicorn/workers/base.py", line 126, in init_process
flask | self.load_wsgi()
flask | File "/usr/local/lib/python3.6/site-packages/gunicorn/workers/base.py", line 135, in load_wsgi
flask | self.wsgi = self.app.wsgi()
flask | File "/usr/local/lib/python3.6/site-packages/gunicorn/app/base.py", line 67, in wsgi
flask | self.callable = self.load()
flask | File "/usr/local/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 65, in load
flask | return self.load_wsgiapp()
flask | File "/usr/local/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp
flask | return util.import_app(self.app_uri)
flask | File "/usr/local/lib/python3.6/site-packages/gunicorn/util.py", line 352, in import_app
flask | __import__(module)
flask | ModuleNotFoundError: No module named 'wsgi'
flask | [2020-03-09 15:00:11 +0000] [7] [INFO] Worker exiting (pid: 7)
flask | [2020-03-09 15:00:11 +0000] [8] [INFO] Booting worker with pid: 8
flask | [2020-03-09 15:00:11 +0000] [8] [ERROR] Exception in worker process
flask | Traceback (most recent call last):
flask | File "/usr/local/lib/python3.6/site-packages/gunicorn/arbiter.py", line 578, in spawn_worker
flask | worker.init_process()
flask | File "/usr/local/lib/python3.6/site-packages/gunicorn/workers/base.py", line 126, in init_process
flask | self.load_wsgi()
flask | File "/usr/local/lib/python3.6/site-packages/gunicorn/workers/base.py", line 135, in load_wsgi
flask | self.wsgi = self.app.wsgi()
flask | File "/usr/local/lib/python3.6/site-packages/gunicorn/app/base.py", line 67, in wsgi
flask | self.callable = self.load()
flask | File "/usr/local/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 65, in load
flask | return self.load_wsgiapp()
flask | File "/usr/local/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp
flask | return util.import_app(self.app_uri)
flask | File "/usr/local/lib/python3.6/site-packages/gunicorn/util.py", line 352, in import_app
flask | __import__(module)
flask | ModuleNotFoundError: No module named 'wsgi'
flask | [2020-03-09 15:00:11 +0000] [8] [INFO] Worker exiting (pid: 8)
flask | [2020-03-09 15:00:11 +0000] [9] [INFO] Booting worker with pid: 9
flask | [2020-03-09 15:00:11 +0000] [9] [ERROR] Exception in worker process
flask | Traceback (most recent call last):
flask | File "/usr/local/lib/python3.6/site-packages/gunicorn/arbiter.py", line 578, in spawn_worker
flask | worker.init_process()
flask | File "/usr/local/lib/python3.6/site-packages/gunicorn/workers/base.py", line 126, in init_process
flask | self.load_wsgi()
flask | File "/usr/local/lib/python3.6/site-packages/gunicorn/workers/base.py", line 135, in load_wsgi
flask | self.wsgi = self.app.wsgi()
flask | File "/usr/local/lib/python3.6/site-packages/gunicorn/app/base.py", line 67, in wsgi
flask | self.callable = self.load()
flask | File "/usr/local/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 65, in load
flask | return self.load_wsgiapp()
flask | File "/usr/local/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp
flask | return util.import_app(self.app_uri)
flask | File "/usr/local/lib/python3.6/site-packages/gunicorn/util.py", line 352, in import_app
flask | __import__(module)
flask | ModuleNotFoundError: No module named 'wsgi'
flask | [2020-03-09 15:00:11 +0000] [9] [INFO] Worker exiting (pid: 9)
flask | [2020-03-09 15:00:11 +0000] [1] [INFO] Shutting down: Master
flask | [2020-03-09 15:00:11 +0000] [1] [INFO] Reason: Worker failed to boot.

所以意味着我的 Flask 服务构建失败,因此 Nginx 代理的应用程序服务器没有运行,根据这个 answer .但我不知道哪里出了问题。

这是我的 docker-compose.yml :
  version: '3'
services:

flask:
build:
context: app
dockerfile: Dockerfile
container_name: flask
image: digitalocean.com/flask-python:3.6
restart: unless-stopped
environment:
APP_ENV: "prod"
APP_DEBUG: "False"
APP_PORT: 5000

service_2:
...

service_3:
...

这是我的 app/Dockerfile
FROM python:3.6.8-alpine3.9

WORKDIR /var/www/

ADD . /var/www/
RUN apk add --no-cache build-base libffi-dev openssl-dev ncurses-dev

RUN python -m pip install --upgrade pip
RUN pip install -r requirements.txt // gunicorn is install inside requirements.txt

EXPOSE 5000

CMD [ "gunicorn", "-w", "4", "--bind", "0.0.0.0:5000", "wsgi"]

这是我的项目目录的样子:
home
-project_root
-docker-compose.yml
--app
---all other files
---Dockerfile
---requirements.txt
---wsgi.py

这是我的 wsgi.py :
import os
from app import create_app
from flask_cors import CORS

app = create_app()
cors = CORS(app)

if __name__ == '__main__':
ENVIRONMENT_DEBUG = os.environ.get("APP_DEBUG", True)
ENVIRONMENT_PORT = os.environ.get("APP_PORT", 5000)
app.run(host='0.0.0.0', port=ENVIRONMENT_PORT, debug=ENVIRONMENT_DEBUG)

我试过的:

据此 answer ,我改了 CMDapp\Dockerfile由此:
CMD [ "gunicorn", "-w", "4", "--bind", "0.0.0.0:5000", "wsgi"]

对此:
CMD [ "gunicorn", "-w", "4", "--bind", "0.0.0.0:5000", "/home/project_root/app/wsgi.py"]

但是在运行 docker-compose up -d 时,我仍然遇到同样的错误。 .我是 docker 的新手,所以完全不知道是什么导致了错误以及我到底做错了什么。有人请帮忙。

更新:

据此 answer ,以及下面的答案,我尝试像这样运行命令:
CMD [ "gunicorn", "-w", "4", "--bind", "0.0.0.0:5000", "wsgi:app"]

仍然得到同样的错误。

如果我搬家 wsgi.pyapp像这样的文件夹:
home
-project_root
-docker-compose.yml
-wsgi.py
--app
---all other files
---Dockerfile
---requirements.txt

也有同样的错误。

更新 2:

我改变 CMD 如下:
CMD [ "gunicorn", "-w", "4", "--bind", "0.0.0.0:5000", "/var/www/app/wsgi.py"]

现在我得到这样的错误:
flask        | ModuleNotFoundError: No module named '/var/www/app/wsgi.py'

最佳答案

您可以尝试将您的项目结构变成这样吗

home
-project_root
-docker-compose.yml
-wsgi.py
-Dockerfile
-requirements.txt
--app
---all other files

还有你的 Dockerfile变成这样
FROM python:3.6.8-alpine3.9

WORKDIR /var/www/

ADD . /var/www/
RUN apk add --no-cache build-base libffi-dev openssl-dev ncurses-dev

RUN python -m pip install --upgrade pip
RUN pip install -r requirements.txt // gunicorn is install inside requirements.txt

EXPOSE 5000

CMD [ "gunicorn", "-w", "4", "--bind", "0.0.0.0:5000", "wsgi:app"]

关于python - 由于 ModuleNotFoundError : No module named 'wsgi' ,Flask 无法使用 Docker 启动服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60603593/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com