gunicorn ModuleNotFound错误,以前可以正常工作

时间:2019-09-05 21:28:43

标签: python docker docker-compose gunicorn

我有一个docker-compose文件,直到今天工作正常,当我尝试使用gunicorn启动Web服务器时,它刚刚开始引发错误。日志如下所示:

In file included from /usr/local/include/pybind11/pytypes.h:12:0,
                 from /usr/local/include/pybind11/cast.h:13,
                 from /usr/local/include/pybind11/attr.h:13,
                 from /usr/local/include/pybind11/pybind11.h:44,
                 from /usr/local/include/pybind11/embed.h:12,
                 from /home/stiv/lpr/trt_cpp_loader/main.cpp:2:
/usr/local/include/pybind11/detail/common.h:112:10: fatal error: Python.h: No such file or directory
 #include <Python.h>
          ^~~~~~~~~~
compilation terminated.

我的dockerfile看起来像

    flask_apis  | [2019-09-05 21:20:04 +0000] [1] [INFO] Starting gunicorn 19.9.0
flask_apis  | [2019-09-05 21:20:04 +0000] [1] [INFO] Listening at: http://0.0.0.0:8000 (1)
flask_apis  | [2019-09-05 21:20:04 +0000] [1] [INFO] Using worker: sync
flask_apis  | [2019-09-05 21:20:04 +0000] [8] [INFO] Booting worker with pid: 8
flask_apis  | [2019-09-05 21:20:04 +0000] [8] [ERROR] Exception in worker process
flask_apis  | Traceback (most recent call last):
flask_apis  |   File "/usr/local/lib/python3.7/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
flask_apis  |     worker.init_process()
flask_apis  |   File "/usr/local/lib/python3.7/site-packages/gunicorn/workers/base.py", line 129, in init_process
flask_apis  |     self.load_wsgi()
flask_apis  |   File "/usr/local/lib/python3.7/site-packages/gunicorn/workers/base.py", line 138, in load_wsgi
flask_apis  |     self.wsgi = self.app.wsgi()
flask_apis  |   File "/usr/local/lib/python3.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
flask_apis  |     self.callable = self.load()
flask_apis  |   File "/usr/local/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 52, in load
flask_apis  |     return self.load_wsgiapp()
flask_apis  |   File "/usr/local/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 41, in load_wsgiapp
flask_apis  |     return util.import_app(self.app_uri)
flask_apis  |   File "/usr/local/lib/python3.7/site-packages/gunicorn/util.py", line 350, in import_app
flask_apis  |     __import__(module)
flask_apis  | ModuleNotFoundError: No module named 'fintech'
flask_apis  | [2019-09-05 21:20:04 +0000] [8] [INFO] Worker exiting (pid: 8)
flask_apis  | [2019-09-05 21:20:04 +0000] [9] [INFO] Booting worker with pid: 9
flask_apis  | [2019-09-05 21:20:04 +0000] [9] [ERROR] Exception in worker process
flask_apis  | Traceback (most recent call last):
flask_apis  |   File "/usr/local/lib/python3.7/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
flask_apis  |     worker.init_process()
flask_apis  |   File "/usr/local/lib/python3.7/site-packages/gunicorn/workers/base.py", line 129, in init_process
flask_apis  |     self.load_wsgi()
flask_apis  |   File "/usr/local/lib/python3.7/site-packages/gunicorn/workers/base.py", line 138, in load_wsgi
flask_apis  |     self.wsgi = self.app.wsgi()
flask_apis  |   File "/usr/local/lib/python3.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
flask_apis  |     self.callable = self.load()
flask_apis  |   File "/usr/local/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 52, in load
flask_apis  |     return self.load_wsgiapp()
flask_apis  |   File "/usr/local/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 41, in load_wsgiapp
flask_apis  |     return util.import_app(self.app_uri)
flask_apis  |   File "/usr/local/lib/python3.7/site-packages/gunicorn/util.py", line 350, in import_app
flask_apis  |     __import__(module)
flask_apis  | ModuleNotFoundError: No module named 'fintech'

还有该服务器的docker-compose文件:

FROM python:3.7-slim

WORKDIR /app

RUN pip install --upgrade pip

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

COPY . .

有趣的是,我可以在docker映像中打开sh:

services:

  api:
    build: 
      context: .
      dockerfile: ./Dockerfile
    image: flask-pywren
    container_name: flask_apis
    command: /usr/local/bin/gunicorn -w 2 -b :8000 fintech.flask_entrypoint:app
    env_file:
     - ./docker.env
    expose:
      - "8000"
    volumes:
      - .:/app/

并运行

docker run -it flask-pywren sh

和Gunicorn开始正常。我唯一想到的是/usr/local/bin/gunicorn -w 2 -b :8000 fintech.flask_entrypoint:app 不是从docker-compose开始的吗?我也将WORKDIR添加到了docker-compose文件中,但这也不起作用。我真的不知道为什么这突然无法按预期工作。

1 个答案:

答案 0 :(得分:0)

如果您在日志中看到ModuleNotFoundError: No module named 'fintech',则非常清楚。

您需要安装fintech

FROM python:3.7-slim

WORKDIR /app

RUN pip install --upgrade pip
RUN pip install fintech

到目前为止docker-compose可以改变docker的行为

    volumes:
      - .:/app/

它将覆盖/app下在构建期间COPY . .复制的所有内容

understanding-docker-and-docker-compose