Django:在Docker容器中运行时找不到模块

时间:2019-07-01 10:32:06

标签: python django docker prometheus

我正在Docker容器中运行Django项目,我想添加一个模块(特别是django-prometheus

我运行:pip install django-prometheusdocker run -p 9090:9090 prom/prometheus成功,并对settings.pyurls.py文件进行了必要的更改,为specified in the README

然后我重建了项目并重新启动它,但是它给了我错误ModuleNotFoundError: No module named 'django_prometheus'

(完整的错误报告:

Traceback (most recent call last):
api_1           |   File "/usr/local/lib/python3.7/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
api_1           |     worker.init_process()
api_1           |   File "/usr/local/lib/python3.7/site-packages/gunicorn/workers/base.py", line 129, in init_process
api_1           |     self.load_wsgi()
api_1           |   File "/usr/local/lib/python3.7/site-packages/gunicorn/workers/base.py", line 138, in load_wsgi
api_1           |     self.wsgi = self.app.wsgi()
api_1           |   File "/usr/local/lib/python3.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
api_1           |     self.callable = self.load()
api_1           |   File "/usr/local/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 52, in load
api_1           |     return self.load_wsgiapp()
api_1           |   File "/usr/local/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 41, in load_wsgiapp
api_1           |     return util.import_app(self.app_uri)
api_1           |   File "/usr/local/lib/python3.7/site-packages/gunicorn/util.py", line 350, in import_app
api_1           |     __import__(module)
api_1           |   File "/usr/src/app/project/wsgi.py", line 16, in <module>
api_1           |     application = get_wsgi_application()
api_1           |   File "/usr/local/lib/python3.7/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
api_1           |     django.setup(set_prefix=False)
api_1           |   File "/usr/local/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
api_1           |     apps.populate(settings.INSTALLED_APPS)
api_1           |   File "/usr/local/lib/python3.7/site-packages/django/apps/registry.py", line 89, in populate
api_1           |     app_config = AppConfig.create(entry)
api_1           |   File "/usr/local/lib/python3.7/site-packages/django/apps/config.py", line 90, in create
api_1           |     module = import_module(entry)
api_1           |   File "/usr/local/lib/python3.7/importlib/__init__.py", line 127, in import_module
api_1           |     return _bootstrap._gcd_import(name[level:], package, level)
api_1           | ModuleNotFoundError: No module named 'django_prometheus'
api_1           | [2019-07-01 12:19:36 +0200] [16] [INFO] Worker exiting (pid: 16)

在我添加此模块之前,该项目运行良好,我缺少什么?

1 个答案:

答案 0 :(得分:2)

在Dockerfile中,应确保您具有pip install命令。请看下面的例子。 Prometheus模块应列在requirements.txt文件中。

RUN apt-get update \
&& apt-get install -y --no-install-recommends software-properties-common \
&& apt-get update \
&& apt-get install -q -y --no-install-recommends python3 python3-dev python3-pip python3-setuptools python3-wheel gcc \
&& apt-get install -q -y vim \
&& apt-get clean 

ADD requirements.txt /app/requirements.txt
RUN pip3 install -r /app/requirements.txt

# Add the application source code.
ADD . /app