Docker-Compose与src目录同级时如何启动Gunicorn服务?
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/configsubdirectory withwsgi.pyandsettings.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:
- Corrected module path: Your Django project's config directory directly contains
wsgi.py—so the correct module reference isconfig.wsgi, notconfig.setting.wsgi. The original command had an extra (and incorrect)settingsegment. - Proper working directory: The
--chdir ./src/flag ensures Gunicorn runs from the root of your Django project (where the nested/configpackage lives), allowing Python to correctly locate theconfigmodule.
内容的提问来源于stack exchange,提问作者Jaco

