来自带有 Gunicorn 的 Docker 容器的 ModuleNotFoundError

Posted

技术标签:

【中文标题】来自带有 Gunicorn 的 Docker 容器的 ModuleNotFoundError【英文标题】:ModuleNotFoundError from Docker container with Gunicorn 【发布时间】:2021-12-26 02:04:10 【问题描述】:

我正在学习如何 dockerize 一个 Django 项目。所以,我用 Django 创建了一个简单的 hello world。 在此之后,我创建了我的 Dockerfile:

# syntax=docker/dockerfile:1

# Official Ubuntu Image as Layer
FROM ubuntu:20.04 as os
# Disable Prompt During Packages Installation
ARG DEBIAN_FRONTEND=noninteractive
# Update&Upgrade Ubuntu
RUN apt update && apt upgrade -y

# OS as Layer
FROM os as gis-os
# Disable Prompt During Packages Installation
ARG DEBIAN_FRONTEND=noninteractive
# Set Python environment variables
# Prevents Python from writing pyc files to disc
ENV PYTHONDONTWRITEBYTECODE 1
# Prevents Python from buffering stdout and stderr
ENV PYTHONUNBUFFERED 1
# Install python and upgrade pip
RUN apt install -y python3-pip build-essential
RUN pip3 install --upgrade pip
# Installing Geospatial libraries
RUN apt install -y libpq-dev
# Install PROJ
RUN apt install -y libproj-dev proj-data proj-bin unzip
# Install GEOS
RUN apt install -y libgeos-dev
# Install GDAL
RUN apt install -y tzdata
RUN apt install -y libgdal-dev python3-gdal gdal-bin

# GIS-OS as Layer
FROM gis-os as djangoproject
# Update&Upgrade Layer
RUN apt update && apt upgrade -y
# Set work directory
WORKDIR /app
# Copy project data
COPY . ./djangodocker
# Create staticfiles directory
RUN mkdir -p static-folder
# Create media directory
RUN mkdir -p media-folder
# Install project dependencies
RUN pip3 install --no-cache-dir -r ./djangodocker/requirements.txt
## Active entrypoint.sh
RUN chmod +x /app/djangodocker/entrypoint.sh
# Run entrypoint.sh
ENTRYPOINT ["/app/djangodocker/entrypoint.sh"]

# DJANGOPROJECT as Layer
FROM djangoproject as gunicorn-setting-up
# Update&Upgrade Layer
RUN apt update && apt upgrade -y
# Install Gunicorn
RUN pip3 install gunicorn

注意:我的目标是获得一个 Geodjango 项目,所以我需要很多与 GIS 领域相关的东西。

除了 Dockerfile 我还有 entrypoint.sh

在 docker-compose 文件下方:

version: '3.7'

services:
  website:
    container_name: web_project
    image: dev-project/django
    build: djangodocker
    restart: always
    env_file: dev.env
    command: python3 djangodocker/manage.py runserver 0.0.0.0:8000
    ports:
      - 8000:8000

当我运行sudo docker-compose -f docker-compose-dev.yml up -d --build 时,我可以在我的项目中导航。但是当我使用时:

command: gunicorn djangodocker.wsgi:application --bind 0.0.0.0:8000

代替:

command: python3 djangodocker/manage.py runserver 0.0.0.0:8000

我看到以下错误:

web_project | [2021-11-14 15:28:01 +0000] [1] [INFO] Starting gunicorn 20.1.0
web_project | [2021-11-14 15:28:01 +0000] [1] [INFO] Listening at: http://0.0.0.0:8000 (1)
web_project | [2021-11-14 15:28:01 +0000] [1] [INFO] Using worker: sync
web_project | [2021-11-14 15:28:01 +0000] [9] [INFO] Booting worker with pid: 9
web_project | [2021-11-14 15:28:01 +0000] [9] [ERROR] Exception in worker process
web_project | Traceback (most recent call last):
web_project |   File "/usr/local/lib/python3.8/dist-packages/gunicorn/arbiter.py", line 589, in spawn_worker
web_project |     worker.init_process()
web_project |   File "/usr/local/lib/python3.8/dist-packages/gunicorn/workers/base.py", line 134, in init_process
web_project |     self.load_wsgi()
web_project |   File "/usr/local/lib/python3.8/dist-packages/gunicorn/workers/base.py", line 146, in load_wsgi
web_project |     self.wsgi = self.app.wsgi()
web_project |   File "/usr/local/lib/python3.8/dist-packages/gunicorn/app/base.py", line 67, in wsgi
web_project |     self.callable = self.load()
web_project |   File "/usr/local/lib/python3.8/dist-packages/gunicorn/app/wsgiapp.py", line 58, in load
web_project |     return self.load_wsgiapp()
web_project |   File "/usr/local/lib/python3.8/dist-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
web_project |     return util.import_app(self.app_uri)
web_project |   File "/usr/local/lib/python3.8/dist-packages/gunicorn/util.py", line 359, in import_app
web_project |     mod = importlib.import_module(module)
web_project |   File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
web_project |     return _bootstrap._gcd_import(name[level:], package, level)
web_project |   File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
web_project |   File "<frozen importlib._bootstrap>", line 991, in _find_and_load
web_project |   File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
web_project | ModuleNotFoundError: No module named 'djangodocker.wsgi'
web_project | [2021-11-14 15:28:01 +0000] [9] [INFO] Worker exiting (pid: 9)
web_project | [2021-11-14 15:28:01 +0000] [1] [INFO] Shutting down: Master
web_project | [2021-11-14 15:28:01 +0000] [1] [INFO] Reason: Worker failed to boot.

下图显示了项目树。 这是我与 Docker 的第一个项目,我不明白为什么 Gunicorn 看不到 djangodocker.wsgi,我试过 djangodocker.djangodocker.wsgi 没有财富。

【问题讨论】:

【参考方案1】:

问题出在command:

command: sh -c "cd djangodocker/ &&
                gunicorn djangodocker.wsgi:application --bind 0.0.0.0:8000"

我已经按照上面的说明解决了。

【讨论】:

以上是关于来自带有 Gunicorn 的 Docker 容器的 ModuleNotFoundError的主要内容,如果未能解决你的问题,请参考以下文章

django 和 gunicorn 在 docker 容器内运行时,无法在 ec2 上使用 nginx 提供静态文件

来自带有 Spring Security (Jhispter) 的 docker 容器的 Keycloak redirect_uri

使用 Docker、nginx 和 gunicorn 提供 Django 静态文件

尝试在 Docker 上使用 NGINX + Gunicorn 时 NGINX 给出 502 Bad Gateway

带有 gunicorn+uvicorn 的 openbsd httpd -- 远程协议错误:非法请求行

将 django docker 容器部署到弹性 beantalk