You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker-Compose与src目录同级时如何启动Gunicorn服务?

Fixing ModuleNotFoundError for Gunicorn in Your Dockerized Django Environment

Context of Your Setup

You're building a dev stack with Docker, Django, Gunicorn, Postgres, and Nginx, with this directory structure under Documents/repositories/NN Project:

  • /config: Stores Nginx configurations
  • /deployment: Holds environment secrets
  • /src: Contains your Django project, which has a nested /config subdirectory with wsgi.py and settings.py
  • Top-level files: Dockerfile, docker-compose.yml, license, docs, and build_script.sh

You've placed Dockerfile and docker-compose.yml at the top level (instead of inside /src) to let your Django container access secrets via relative paths using this code:

NON_VERSION_PATH = Path(__file__).resolve().parent.parent.parent
with open(NON_VERSION_PATH / 'deployment/etc/application/.env_vars', 'r') as f:
    secrets = json.loads(f.read())

The Problem You're Facing

Your web container (pointing to /src) fails to start with a ModuleNotFoundError when using Gunicorn. When you move Dockerfile and docker-compose.yml into /src, everything works—but you want to keep them at the top level.

You noticed that running python /code/src/manage.py runserver 0.0.0.0:8000 works fine, but switching to this Gunicorn command throws an error:

gunicorn config.setting.wsgi:application --bind 0.0.0.0:8000 --chdir ./src/

The error states that the config.setting module can't be found.

The Solution

Besides official recommended approaches, this adjusted Gunicorn command will resolve the issue:

gunicorn -w 2 -b 0.0.0.0:8000 --chdir ./src/ config.wsgi:application --reload --timeout 900

Why This Works

The key fixes here are:

  1. Corrected module path: Your Django project's config directory directly contains wsgi.py—so the correct module reference is config.wsgi, not config.setting.wsgi. The original command had an extra (and incorrect) setting segment.
  2. Proper working directory: The --chdir ./src/ flag ensures Gunicorn runs from the root of your Django project (where the nested /config package lives), allowing Python to correctly locate the config module.

内容的提问来源于stack exchange,提问作者Jaco

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 10:27:37