Docker 容器内的 Django + uWSGI/nginx - ImportError : No module named . wsgi

标签 django nginx docker uwsgi

这是一个古老的 Django (v1.3.7) 应用程序,我正在迁移到 Docker 设置。
基本框架来自this dockerfiles repo .

我可以使用 Django 的内置开发服务器 (./manage.py runserver) 使应用程序运行良好,但我希望在生产中使用 uWSGI/Nginx 来提供此服务。

当我从容器中的 bash 提示符调用它时,我可以让 uwsgi 正常运行,如下所示:uwsgi --http :8000 --wsgi-file /home/docker/code/siteweb/glrimon.wsgi但是,当我尝试从 Django shell 导入我的 wsgi 文件时,我得到了相同的导入错误。

以下是我启动容器时记录的内容(通过 supervisord 调用 /usr/local/bin/uwsgi --ini /home/docker/code/uwsgi.ini):

[uWSGI] getting INI configuration from /home/docker/code/uwsgi.ini
*** Starting uWSGI 2.0.15 (64bit) on [Tue May  9 13:35:14 2017] ***
compiled with version: 5.4.0 20160609 on 05 May 2017 18:06:55
os: Linux-3.16.0-77-generic #99~14.04.1-Ubuntu SMP Tue Jun 28 19:17:10 UTC 2016
nodename: 222b58f8d3ea
machine: x86_64
clock source: unix
detected number of CPU cores: 8
current working directory: /
detected binary path: /usr/local/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
chdir() to /home/docker/code/siteweb/
your memory page size is 4096 bytes
detected max file descriptor number: 524288
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to UNIX address /home/docker/code/app.sock fd 3
Python version: 2.7.12 (default, Nov 19 2016, 06:48:10)  [GCC 5.4.0 20160609]
2017/05/09 13:35:14 [debug] 11#11: epoll add event: fd:6 op:1 ev:00002001
2017/05/09 13:35:14 [debug] 12#12: epoll add event: fd:6 op:1 ev:00002001
2017/05/09 13:35:14 [debug] 11#11: epoll del event: fd:6 op:2 ev:00000000
Python main interpreter initialized at 0x8f11c0
python threads support enabled
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 415360 bytes (405 KB) for 8 cores
*** Operational MODE: preforking+threaded ***
added /usr/local/lib/python2.7/dist-packages/ to pythonpath.
added /usr/lib/python2.7/ to pythonpath.
added ./siteweb to pythonpath.
ImportError: No module named glrimon.wsgi
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
*** uWSGI is running in multiple interpreter mode ***

这是我的 uwsgi.ini 文件:
[uwsgi]
# this config will be loaded if nothing specific is specified
# load base config from below
ini = :base

# %d is the dir this configuration file is in
socket = %dapp.sock
master = true
processes = 4
threads = 2

#logging
logto=/var/log/uwsgi.log

[dev]
ini = :base
# socket (uwsgi) is not the same as http, nor http-socket
socket = :8001


[local]
ini = :base
http = :8000


[base]
# chdir to the folder of this config file, plus app/website
chdir = %dsiteweb/
# allow anyone to connect to the socket. This is very permissive
chmod-socket=666
# explicitly set python path
pythonpath = /usr/local/lib/python2.7/dist-packages
pythonpath = /usr/lib/python2.7
pythonpath = ./siteweb
# also referencing here: http://uwsgi-docs.readthedocs.io/en/latest/WSGIquickstart.html#deploying-django
env = DJANGO_SETTINGS_MODULE=siteweb.settings
# load the module from wsgi.py, it is a python path from
# the directory above.
module=glrimon.wsgi:application

这是我的 wsgi 文件的样子:
import sys
import os

sys.path.append(os.path.dirname(os.path.dirname(__file__)))
sys.path.append(os.path.dirname(__file__))

os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

Dockerfile:
FROM ubuntu:16.04

MAINTAINER Dockerfiles

# Install required packages and remove the apt packages cache when done.

RUN apt-get update && \
    apt-get upgrade -y && \
    apt-get install -y \
    git \
    # for python package lxml
    libxml2-dev libxslt1-dev zlib1g-dev \
    # for python package PIL
    libtiff5-dev libjpeg8-dev \
    # for python package psycopg2
    build-essential libpq-dev \
    # for geodjango
    libgeos-dev \
    python \
    python-dev \
    python-setuptools \
    python-pip \
    nginx \
    supervisor \
    sqlite3 && \
    pip install -U pip setuptools && \
    rm -rf /var/lib/apt/lists/*

# install uwsgi now because it takes a little while
RUN pip install uwsgi

# setup uwsgi logging directory
RUN mkdir -p /var/log/uwsgi

# forward request and error logs to docker log collector
RUN ln -sf /dev/stdout /var/log/nginx/access.log \
    && ln -sf /dev/stderr /var/log/nginx/error.log \
    && ln -sf /dev/stdout /var/log/uwsgi.log
#    && ln -sf /dev/stdout /var/log/uwsgi/req.log

# setup all the configfiles
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
COPY nginx-app.conf /etc/nginx/sites-available/default
COPY supervisor-app.conf /etc/supervisor/conf.d/

# COPY requirements.txt and RUN pip install BEFORE adding the rest of your code, this will cause Docker's caching mechanism
# to prevent re-installinig (all your) dependencies when you made a change a line or two in your app.

COPY requirements.txt /home/docker/code/
RUN pip install -r /home/docker/code/requirements.txt
# workaround for missing distribution for pkg_resources
# ref: http://stackoverflow.com/questions/7446187/no-module-named-pkg-resources
#RUN pip install --upgrade setuptools

# deal with Geodjango GEOSexception error
# (ref: http://stackoverflow.com/a/19811665/6072959)
RUN sed -i 's/d+)\$/d+)\.\*\$/g' \
    /usr/local/lib/python2.7/dist-packages/django/contrib/gis/geos/libgeos.py

# add (the rest of) our code
# skip this step since the code is actually referenced via
# symlink since it's gigantic
# connect a volume to the code during docker run instead
COPY . /home/docker/code/

WORKDIR /home/docker/code

# install django, normally you would remove this step because your project would already
# be installed in the code/siteweb/ directory
#RUN django-admin.py startproject website /home/docker/code/siteweb/

EXPOSE 80
CMD ["supervisord", "-n"]

任何想法/想法总是受到赞赏。

最佳答案

两个想法。

  • 我想知道 pythonpath = ./siteweb是相对于初始当前工作目录的?当你手动运行它时,你是否已经在 /home/docker/code 中? ?尝试将以下内容添加到 Dockerfile :
    WORKDIR /home/docker/code
    
  • module 是 ini 文件的正确选项吗?根据 uwsgi-docs.readthedocs.io/en/latest/WSGIquickstart.html 如果你在命令行上使用 --wsgi-file/home/docker/code/siteweb/glrimon.wsgi 你应该也有 wsgi-file/home/docker/code/siteweb/glrimon.wsgi 在 ini 文件中。他们的例子也有`module = django.core.handlers.wsgi:WSGIHandler()。但是由于您正在加载 wsgi 文件,因此可能不需要(可能是其中之一)。
  • 关于Docker 容器内的 Django + uWSGI/nginx - ImportError : No module named . wsgi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43872860/

    相关文章:

    python - 为什么值列表在 Django 中保留过滤器?

    python - 显式 cursor.close() 的必要性

    php - Codeigniter 的 NGINX 服务器配置

    php - 安装 nginx 后找不到 Laravel 路由

    docker - 如何在 Visual Studio 2017 版本 15.3.2 中暂时禁用来自 asp.net core 2.0 项目的 docker 支持?

    docker - Docker Compose YAML版本1和版本2有什么区别?

    django - 如何在 Django 中从多对多关系中检索数据

    django - "Cannot filter a query once a slice has been taken"

    node.js - 增加 nginx 上的缓冲区超时大小

    wordpress - 错误 : Get https://registry-1. docker.io/v2/: net/http: 请求在等待连接时被取消-Docker 与 Wordpress