Dockerizing Django应用程序:ModuleNotFoundError:没有名为“ X”的模块

时间:2019-05-26 10:03:39

标签: django docker gunicorn

我一直在尝试遵循此指南对项目进行Docker化

Deploy and Run Django in Dokcer Container

这是基本文件结构:

WFMBCM
|-WFMBCM
| |-__init__.py
| |-__pycache__
| |-setting.py
| |-url.py
| |-wsgi.py
|
|-WFMBCM_App
|-WFMBCM_db
|-manage.py
|-Dockerfile
|-requirements.txt
|-runWFMBCM.sh

Dockerfile:

FROM python:3.7
ADD WFMBCM /usr/src/app
ADD WFMBCM_App /usr/src/app
ADD WFMBCM_db /usr/src/app
ADD manage.py /usr/src/app
WORKDIR /usr/src/app
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
EXPOSE 8000
CMD exec gunicorn wfmbcm.wsgi:application --bind 0.0.0.0:8000 --workers 3

requirements.txt:

Django==2.2.1
gunicorn==19.9.0

runWFMBCM.sh:

cd /home/admin/WFMBCM
echo -e "\033[0;34mGo to WFMBCM directory.\n \033[0;37m"
echo -e "\033[0;34mRemove old WFMBCM container\n \033[0;37m"
docker rm -v wfmbcm_container 
echo -e "\033[0;34m\nBuild WFMBCM Docker image.\n \033[0;37m"
sudo docker build -t wfmbcm .
echo -e "\033[0;34m\nRun WFMBCM Docker image.\n \033[0;37m"
sudo docker run --name wfmbcm_container -p 8000:8000 -i -t wfmbcm
echo -e "\033[0;34m\n\nWFMBCM run-script ended!.\n \033[0;37m"

运行runWFMBCM.sh时,得到以下输出:

Go to WFMBCM directory.

Remove old WFMBCM container

Error response from daemon: No such container: wfmbcm_container

Build WFMBCM Docker image.

Sending build context to Docker daemon  265.2kB
Step 1/10 : FROM python:3.7
 ---> cc971a68c3e4
Step 2/10 : ADD WFMBCM /usr/src/app
 ---> Using cache
 ---> 302346d017e2
Step 3/10 : ADD WFMBCM_App /usr/src/app
 ---> Using cache
 ---> 18bc8eb69946
Step 4/10 : ADD WFMBCM_db /usr/src/app
 ---> Using cache
 ---> 0835393b22dc
Step 5/10 : ADD manage.py /usr/src/app
 ---> Using cache
 ---> 3ef5477e4b26
Step 6/10 : WORKDIR /usr/src/app
 ---> Using cache
 ---> 2edb019a8257
Step 7/10 : COPY requirements.txt ./
 ---> Using cache
 ---> df3ac922b7a0
Step 8/10 : RUN pip install --no-cache-dir -r requirements.txt
 ---> Using cache
 ---> ba6cf54e7dcc
Step 9/10 : EXPOSE 8000
 ---> Using cache
 ---> 94f3f4fb38c0
Step 10/10 : CMD exec gunicorn wfmbcm.wsgi:application --bind 0.0.0.0:8000 --workers 3
 ---> Using cache
 ---> db6cf2149949
Successfully built db6cf2149949
Successfully tagged wfmbcm:latest

Run WFMBCM Docker image.

[2019-05-26 09:59:36 +0000] [1] [INFO] Starting gunicorn 19.9.0
[2019-05-26 09:59:36 +0000] [1] [INFO] Listening at: http://0.0.0.0:8000 (1)
[2019-05-26 09:59:36 +0000] [1] [INFO] Using worker: sync
[2019-05-26 09:59:36 +0000] [9] [INFO] Booting worker with pid: 9
[2019-05-26 09:59:36 +0000] [10] [INFO] Booting worker with pid: 10
[2019-05-26 09:59:36 +0000] [9] [ERROR] Exception in worker process
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
    worker.init_process()
  File "/usr/local/lib/python3.7/site-packages/gunicorn/workers/base.py", line 129, in init_process
    self.load_wsgi()
  File "/usr/local/lib/python3.7/site-packages/gunicorn/workers/base.py", line 138, in load_wsgi
    self.wsgi = self.app.wsgi()
  File "/usr/local/lib/python3.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
    self.callable = self.load()
  File "/usr/local/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 52, in load
    return self.load_wsgiapp()
  File "/usr/local/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 41, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/usr/local/lib/python3.7/site-packages/gunicorn/util.py", line 350, in import_app
    __import__(module)
ModuleNotFoundError: No module named 'wfmbcm'
[2019-05-26 09:59:36 +0000] [9] [INFO] Worker exiting (pid: 9)
[2019-05-26 09:59:36 +0000] [11] [INFO] Booting worker with pid: 11
[2019-05-26 09:59:36 +0000] [10] [ERROR] Exception in worker process
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
    worker.init_process()
  File "/usr/local/lib/python3.7/site-packages/gunicorn/workers/base.py", line 129, in init_process
    self.load_wsgi()
  File "/usr/local/lib/python3.7/site-packages/gunicorn/workers/base.py", line 138, in load_wsgi
    self.wsgi = self.app.wsgi()
  File "/usr/local/lib/python3.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
    self.callable = self.load()
  File "/usr/local/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 52, in load
    return self.load_wsgiapp()
  File "/usr/local/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 41, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/usr/local/lib/python3.7/site-packages/gunicorn/util.py", line 350, in import_app
    __import__(module)
ModuleNotFoundError: No module named 'wfmbcm'
[2019-05-26 09:59:36 +0000] [10] [INFO] Worker exiting (pid: 10)
[2019-05-26 09:59:36 +0000] [11] [ERROR] Exception in worker process
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
    worker.init_process()
  File "/usr/local/lib/python3.7/site-packages/gunicorn/workers/base.py", line 129, in init_process
    self.load_wsgi()
  File "/usr/local/lib/python3.7/site-packages/gunicorn/workers/base.py", line 138, in load_wsgi
    self.wsgi = self.app.wsgi()
  File "/usr/local/lib/python3.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
    self.callable = self.load()
  File "/usr/local/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 52, in load
    return self.load_wsgiapp()
  File "/usr/local/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 41, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/usr/local/lib/python3.7/site-packages/gunicorn/util.py", line 350, in import_app
    __import__(module)
ModuleNotFoundError: No module named 'wfmbcm'
[2019-05-26 09:59:36 +0000] [11] [INFO] Worker exiting (pid: 11)
[2019-05-26 09:59:37 +0000] [1] [INFO] Shutting down: Master
[2019-05-26 09:59:37 +0000] [1] [INFO] Reason: Worker failed to boot.


WFMBCM run-script ended!.

为什么找不到wfmbcm模块?我一直疯狂地盯着谷歌搜索,没有成功,并且看着不同的堆栈溢出问题。

2 个答案:

答案 0 :(得分:1)

我找不到任何名为wfmbcm的python模块,因此我认为它是您项目中的一个应用程序。

如果您使用区分大小写的文件系统,则文件布局将显示某些文件夹的所有大写字母,这可能是问题的一部分。

此项目是否在docker外部运行?

如果确实如此,那么我认为最好的选择是启动docker容器,然后在容器内手动安装整个应用程序,并按照自己的步骤更新dockerfile。

每次测试新更改时,请确保使用--no-cache选项进行构建,否则您可能未在测试自己的想法。

答案 1 :(得分:0)

将DockerFile更改为此可以解决我的问题:

FROM python:3.7
ADD WFMBCM /usr/src/app/WFMBCM
ADD WFMBCM_App /usr/src/app/WFMBCM_App
ADD WFMBCM_db /usr/src/app/WFMBCM_db
ADD manage.py /usr/src/app
WORKDIR /usr/src/app
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
EXPOSE 8000
CMD manage.py migrate
CMD exec gunicorn WFMBCM.wsgi:application --bind 0.0.0.0:8000 --workers 10