gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 02:44:09 24 4
gpt4 key购买 nike

我正在尝试使用 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 下的原因是因为我希望在使用 Django 及其相关导入

的容器中工作时能够访问 env“ secret ”
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-compose

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:

Dockerfile

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

解决方案:除了公认的解决方案,这个 SO post也成功了。

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/

关于python - 当 docker-compose 不在同一文件夹中时启动 gunicorn,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66548363/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com