当 docker-compose 不在同一文件夹中时启动 gunicorn

start gunicorn when docker-compose is not in same folder

我正在尝试使用 Docker、Django、gunicorn、postgres 和 nginx 构建环境。在此环境中,Dockerfiledocker-compose 位于 src 文件夹

的上一层

我的文件夹结构思路如下:

"NN Project" folder within my "Documents/repositories"
└── / config (to place nginx settings within)
└── / deployment (to place environment "secrets" within)
└── / src (folder containing Django project)
    └── / config (Django project name)
        ├── wsgi.py
        ├── settings.py
├── Dockerfile
├── docker-compose
├── license, readme ...
├── build_script.sh

我想将 Dockerfiledocker-compose 放在顶层而不是子文件夹下 /src 的原因是因为我希望能够访问 env 在具有 Django 及其相关导入

的容器中工作时的“秘密”
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())

我遇到的问题是无法启动 web(“src”容器)(找不到它)。但是,如果我将 Dockerfiledocker-compose 放在 /src 文件夹中,效果很好,但我希望 docker-compose 更上一层楼。我知道这是关于相对导入的,但是,我不知道如何告诉它在 ./code

中查找

docker-撰写

version: "3.9"

services:
  db:
    container_name: db
    image: postgres:alpine
    restart: always
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
  web:
    container_name: src
    build: .
    # command: python /code/src/manage.py runserver 0.0.0.0:8000 # works as intended !
    command: gunicorn config.setting.wsgi:application --bind 0.0.0.0:8000 --chdir ./src/
    volumes: 
      - .:/code
    ports:
      - 8000:8000
    depends_on:
      - db
    environment:
      - "DJANGO_SECRET_KEY= *** "
      - "DJANGO_DEBUG=True"

volumes:
  postgres_data:

Docker文件

FROM python:3.8.5
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
WORKDIR /code
COPY /src/requirements/requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code

错误

当我的 docker-compose 使用:

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

它产生以下错误:

Successfully built 8473888f804
Successfully tagged web:latest
Creating src ... done
Attaching to src
src    | [2021-03-09 15:10:44 +0000] [1] [INFO] Starting gunicorn 20.0.4
src    | [2021-03-09 15:10:44 +0000] [1] [INFO] Listening at: http://0.0.0.0:8000 (1)
src    | [2021-03-09 15:10:44 +0000] [1] [INFO] Using worker: sync
src    | [2021-03-09 15:10:44 +0000] [8] [INFO] Booting worker with pid: 8
src    | [2021-03-09 15:10:44 +0000] [8] [ERROR] Exception in worker process
src    | Traceback (most recent call last):
src    |   File "/usr/local/lib/python3.8/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
src    |     worker.init_process()
src    |   File "/usr/local/lib/python3.8/site-packages/gunicorn/workers/base.py", line 119, in init_process
src    |     self.load_wsgi()
src    |   File "/usr/local/lib/python3.8/site-packages/gunicorn/workers/base.py", line 144, in load_wsgi
src    |     self.wsgi = self.app.wsgi()
src    |   File "/usr/local/lib/python3.8/site-packages/gunicorn/app/base.py", line 67, in wsgi
src    |     self.callable = self.load()
src    |   File "/usr/local/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py", line 49, in load
src    |     return self.load_wsgiapp()
src    |   File "/usr/local/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py", line 39, in load_wsgiapp
src    |     return util.import_app(self.app_uri)
src    |   File "/usr/local/lib/python3.8/site-packages/gunicorn/util.py", line 358, in import_app
src    |     mod = importlib.import_module(module)
src    |   File "/usr/local/lib/python3.8/importlib/__init__.py", line 127, in import_module
src    |     return _bootstrap._gcd_import(name[level:], package, level)
src    |   File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
src    |   File "<frozen importlib._bootstrap>", line 991, in _find_and_load
src    |   File "<frozen importlib._bootstrap>", line 961, in _find_and_load_unlocked
src    |   File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
src    |   File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
src    |   File "<frozen importlib._bootstrap>", line 991, in _find_and_load
src    |   File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
src    | ModuleNotFoundError: No module named 'config.setting'
src    | [2021-03-09 15:10:44 +0000] [8] [INFO] Worker exiting (pid: 8)
src    | [2021-03-09 15:10:44 +0000] [1] [INFO] Shutting down: Master
src    | [2021-03-09 15:10:44 +0000] [1] [INFO] Reason: Worker failed to boot.

问:如何启动项目gunicorn

解法: 除了公认的解决方案之外,这个 也起到了作用。

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

使用 --chdir 标志。例如,gunicorn config.wsgi:application --bind 0.0.0.0:8000 --chdir /path/to/project/root/