我现在使用Nginx和Docker部署我的Flask应用程序。我的码头工人已经启动并正在运行,但是在进入其中一个端点时,我总是得到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.
因此,根据此answer,这意味着我的Flask服务无法构建,因此Nginx代理的应用程序服务器未运行。但是我不知道哪里出了问题。
这是我的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,我将CMD
中的app\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.py
移到app
文件夹之外,如下所示:
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'
答案 0 :(得分:2)
您能尝试这样的项目结构吗?
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"]
答案 1 :(得分:0)
您是否应该指向wsgi.app?运行此命令是否可以解决问题?
CMD [ "gunicorn", "-w", "4", "--bind", "0.0.0.0:5000", "wsgi:app"]