app.py
. 我想模块化的应用程序,但首先我要尝试在一个docker容器中运行它,以了解我在处理什么。我一直无法让它运行,它无法尝试导入一些本地模块。目录结构如下:
âââ Dockerfile
âââ README.md
âââ app
â  âââ app.py
â  âââ config.py
â  âââ database.ini
â  âââ db.py
â  âââ db.sqlite3
â  âââ functions.py
â  âââ gunicorn_config.py
â  âââ static
#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/')