Gunicorn找不到带有docker的主模块

时间:2016-11-11 17:50:03

标签: python django docker gunicorn

我试图在此tutorial之后使用docker设置django,但是当gunicorn找不到我的主模块时它失败了。

文件夹结构:

/project
  .env
  docker-compose.yml
  /web
    /static
    manage.py
    /django_project
      /apps_directories...
      /templates
      __init__.py
      settings.py
      wsgi.py

docker-compose.yml的网页部分

web:
  restart: always
  build: ./web
  expose:
    - "8000"
  links:
    - postgres:postgres
  volumes:
    - ./web:/usr/src/app
  env_file: .env
  command: /usr/local/bin/gunicorn django_project.wsgi:application -w 2 -b :8000

" django_project / wsgi.py"的内容:

import os
from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_project.settings")
application = get_wsgi_application()

启动容器时抛出错误:

[2016-11-01 16:39:48 +0000] [7] [INFO] Worker exiting (pid: 7)
[2016-11-01 16:39:48 +0000] [1] [INFO] Shutting down: Master
[2016-11-01 16:39:48 +0000] [1] [INFO] Reason: Worker failed to boot.
[2016-11-01 16:39:49 +0000] [1] [INFO] Starting gunicorn 19.6.0
[2016-11-01 16:39:49 +0000] [1] [INFO] Listening at: http://0.0.0.0:8000 (1)
[2016-11-01 16:39:49 +0000] [1] [INFO] Using worker: sync
[2016-11-01 16:39:49 +0000] [7] [INFO] Booting worker with pid: 7
[2016-11-01 16:39:49 +0000] [7] [ERROR] Exception in worker process
Traceback (most recent call last):
  File "/usr/local/lib/python3.4/site-packages/gunicorn/arbiter.py", line 557, in spawn_worker
    worker.init_process()
  File "/usr/local/lib/python3.4/site-packages/gunicorn/workers/base.py", line 126, in init_process
    self.load_wsgi()
  File "/usr/local/lib/python3.4/site-packages/gunicorn/workers/base.py", line 136, in load_wsgi
    self.wsgi = self.app.wsgi()
  File "/usr/local/lib/python3.4/site-packages/gunicorn/app/base.py", line 67, in wsgi
    self.callable = self.load()
  File "/usr/local/lib/python3.4/site-packages/gunicorn/app/wsgiapp.py", line 65, in load
    return self.load_wsgiapp()
  File "/usr/local/lib/python3.4/site-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/usr/local/lib/python3.4/site-packages/gunicorn/util.py", line 357, in import_app
    __import__(module)
ImportError: No module named 'django_project'

为什么找不到该模块?

1 个答案:

答案 0 :(得分:0)

如果没有看到你的Dockerfile和完整的设置,很难100%确定,但我认为这个问题是docker-compose.yml的这一部分:

volumes:
    - ./web:/usr/src/app

在教程的回购链接中,您链接到等效部分:

volumes:
    - /usr/src/app
    - /usr/src/app/static

这是因为通过使用python:3.5-onbuild图像,您的代码已经被烘焙到图像中,所以不需要将其添加为卷。

我认为您想要将源代码添加为主机卷装的原因是,您所做的更改会立即反映在正在运行的映像中以用于开发目的。如果是这种情况,那么我认为您应该能够通过将有问题的部分更改为:

来实现这一目标
volumes:
    - .:/usr/src/app

这将为整个网站部分提供此信息:

web:
  restart: always
  build: ./web
  expose:
    - "8000"
  links:
    - postgres:postgres
  volumes:
    - .:/usr/src/app
  env_file: .env
  command: /usr/local/bin/gunicorn django_project.wsgi:application -w 2 -b :8000

唯一的问题是容器中的进程以root身份运行,因此您可能会遇到在容器内创建的任何文件的文件权限问题。如果这成为一个问题,那么有记录的解决方法(这取决于你需要实现的目标),或者你总是可以在这里发布另一个问题。