[英]start gunicorn when docker-compose is not in same folder
I am attempting to build an environment, using Docker, Django, gunicorn, postgres and nginx.我正在尝试使用 Docker、Django、gunicorn、postgres 和 nginx 构建环境。 In this environment Dockerfile
and docker-compose
sits one level above src
folder在此环境中, Dockerfile
和docker-compose
位于src
文件夹上方一层
My idea of folder structure is as follows:我对文件夹结构的想法如下:
"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
The reason I want to put Dockerfile
and docker-compose
at top-level and not under the sub-folder /src
is because I want the ability to access env
"secrets" when working in a container with Django
and its relative import我想将Dockerfile
和docker-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())
Problem I have is that I cannot get web
(the "src" container) to start (it cannot find it).我遇到的问题是我无法web
(“src”容器)(它找不到它)。 However, it works fine if I place Dockerfile
and docker-compose
within the /src
folder but I would like to have the docker-compose
one level up.但是,如果我将Dockerfile
和docker-compose
放在/src
文件夹中,它工作正常,但我希望docker-compose
上一层。 I understand it is about relative import, however, I cannot figure out how to tell it to look in ./code
我知道这是关于相对导入,但是,我不知道如何告诉它在./code
中查找
docker-compose 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 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
Error错误
When my docker-compose
using:当我的docker-compose
使用:
command: gunicorn config.setting.wsgi:application --bind 0.0.0.0:8000 --chdir ./src/
It produces the following error:它产生以下错误:
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.
Q: How do I get the project to start gunicorn
?问:我如何让项目启动gunicorn
?
Solution: In addition to accepted solution, this SO post also did the trick.解决方案:除了公认的解决方案之外,这个SO 帖子也起到了作用。
command: gunicorn -w 2 -b 0.0.0.0:8000 --chdir ./src/ config.wsgi:application --reload --timeout 900
use the --chdir flag.使用 --chdir 标志。 for example, gunicorn config.wsgi:application --bind 0.0.0.0:8000 --chdir /path/to/project/root/
例如gunicorn config.wsgi:application --bind 0.0.0.0:8000 --chdir /path/to/project/root/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.