我继承了一个结构不佳的python应用程序(大部分代码和路由都在一个文件app.py
中。我想对该应用程序进行模块化,但首先,我尝试在一个docker容器以了解我正在处理的内容。我无法使其运行,并且尝试导入一些本地模块失败。以下是目录结构:
├── Dockerfile
├── README.md
├── app
│ ├── app.py
│ ├── config.py
│ ├── database.ini
│ ├── db.py
│ ├── db.sqlite3
│ ├── functions.py
│ ├── gunicorn_config.py
│ ├── static
这是我的Dockerfile的相关摘录:
#copy flask app module
COPY app app
COPY requirements.txt ./
#install packages
RUN pip3 install --upgrade pip
RUN pip3 install -r requirements.txt`
#expose application port
EXPOSE 5000
WORKDIR /app
CMD ["gunicorn", "--config", "gunicorn_config.py", "app:app"]
这是我在运行容器时遇到的错误:
[2018-12-22 05:51:58 +0000] [1] [INFO] Starting gunicorn 19.7.1
[2018-12-22 05:51:58 +0000] [1] [INFO] Listening at: http://0.0.0.0:5000 (1)
[2018-12-22 05:51:58 +0000] [1] [INFO] Using worker: sync
[2018-12-22 05:51:58 +0000] [9] [INFO] Booting worker with pid: 9
[2018-12-22 05:51:58 +0000] [10] [INFO] Booting worker with pid: 10
[2018-12-22 05:51:58 +0000] [11] [INFO] Booting worker with pid: 11
[2018-12-22 05:51:59 +0000] [12] [INFO] Booting worker with pid: 12
[2018-12-22 05:51:59 +0000] [13] [INFO] Booting worker with pid: 13
[2018-12-22 05:51:59 +0000] [10] [ERROR] Exception in worker process
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/gunicorn/arbiter.py", line 578, in spawn_worker
worker.init_process()
File "/usr/local/lib/python3.5/dist-packages/gunicorn/workers/base.py", line 126, in init_process
self.load_wsgi()
File "/usr/local/lib/python3.5/dist-packages/gunicorn/workers/base.py", line 135, in load_wsgi
self.wsgi = self.app.wsgi()
File "/usr/local/lib/python3.5/dist-packages/gunicorn/app/base.py", line 67, in wsgi
self.callable = self.load()
File "/usr/local/lib/python3.5/dist-packages/gunicorn/app/wsgiapp.py", line 65, in load
return self.load_wsgiapp()
File "/usr/local/lib/python3.5/dist-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp
return util.import_app(self.app_uri)
File "/usr/local/lib/python3.5/dist-packages/gunicorn/util.py", line 352, in
import_app
__import__(module)
File "/app/app.py", line 6, in <module>
import db
ImportError: No module named 'db'
我应该怎么做才能使此应用程序在容器中运行。当我使用gunicorn --config gunicorn_config.py app:app
在本地运行时,它将在docker中运行,但不能运行。我忽略了哪些失败了?预先感谢,我确实在这里讨论了大多数话题,但是那些导入是令人困惑的部分。这也是来自app.py的摘录(前15行):
import flask,sys,uuidv
from flask import request, send_from_directory, session, url_for,
redirect, jsonify, make_response, flash, get_flashed_messages
import json
from werkzeug import secure_filename
from jinja2 import Environment, FileSystemLoader
import os, csv, xlrd
import db, functions
app = flask.Flask(__name__, static_url_path="") #template_folder='static',
static_url_path=""
app.secret_key = str(uuid.uuid4())
locale = (os.path.abspath(__file__))
locale = locale.replace('app.py', '')
UPLOAD_FOLDER = os.path.join(locale, 'files/')