gpt4 book ai didi

django - Django + Gunicorn + NGINX的docker-compose.yml设置不适用于 `docker-compose up`

转载 作者:行者123 更新时间:2023-12-02 20:11:41 25 4
gpt4 key购买 nike

这是我的文件夹结构。

./awesome_app
├── awesome_app
│   ├── celery.py
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── awesome_app_to_do_list
├── db.sqlite3
├── docker-compose.yml
├── Dockerfile
├── logs
│   ├── nginx-access.log
│   └── nginx-error.log
├── manage.py
├── nginx
│   └── nginx.conf
├── requirements.txt
├── run
└── start.sh

这是我的nginx.conf。
upstream awesome_app {
server unix:/home/notalentgeek/Downloads/awesome_app/run/gunicorn.sock fail_timeout=10s;
}

server {
client_max_body_size 4G;
listen 8080;

access_log /home/notalentgeek/Downloads/awesome_app/logs/nginx-access.log;
error_log /home/notalentgeek/Downloads/awesome_app/logs/nginx-error.log warn;

location /static/ {
autoindex on;
alias /home/notalentgeek/Downloads/awesome_app/static/;
}

location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;

if (!-f $request_filename) {
proxy_pass http://awesome_app;
break;
}
}
}

这是我的docker-compose.yml。
version: "3"  
services:
nginx:
image: nginx:latest
container_name: nginx_awesome_app
ports:
- "8080:8080"
volumes:
- ./:/src
- ./nginx:/etc/nginx/conf.d
depends_on:
- web
web:
build: ./
container_name: django_awesome_app
volumes:
- ./:/src
expose:
- "8080"

这是我的start.sh。
#!/bin/bash

# PENDING: From the source here,
# http://tutos.readthedocs.io/en/latest/source/ndg.html it says that it is a
# common practice to have a specific user to handle the webserver.

SCRIPT=$(readlink -f "$0")
BASEDIR=$(dirname "$SCRIPT")
DJANGO_SETTINGS_MODULE=awesome_app.settings
DJANGO_WSGI_MODULE=awesome_app.wsgi
NAME="awesome_app"
NUM_WORKERS=3

VENV_BIN=${BASEDIR}"/venv/bin"
SOCKFILE=${BASEDIR}"/run/gunicorn.sock"

echo $SOCKFILE

SOCKFILEDIR="$(dirname "$SOCKFILE")"
VENV_ACTIVATE=${VENV_BIN}"/activate"
VENV_GUNICORN=${VENV_BIN}"/gunicorn"

# Activate the virtual environment.
cd $BASEDIR
source $VENV_ACTIVATE
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
export PYTHONPATH=$PYTHONPATH:$BASEDIR

# Create run directory if they does not exists.
test -d $SOCKFILEDIR || mkdir -p $SOCKFILEDIR

# Start Gunicorn!
# Programs meant to be run under supervisor should not daemonize themselves
# (do not use --daemon).
exec ${VENV_GUNICORN} ${DJANGO_WSGI_MODULE}:application \
--bind=unix:$SOCKFILE \
--name $NAME \
--workers $NUM_WORKERS

通过这些命令运行。
  • docker-compose build
  • docker-compose up -d
  • docker-compose up

  • 给我这些错误。
    Starting django_awesome_app ... 
    Starting django_awesome_app ... done
    Starting nginx_awesome_app ...
    Starting nginx_awesome_app ... done
    Attaching to django_awesome_app, nginx_awesome_app
    django_awesome_app | //run/gunicorn.sock
    django_awesome_app | /start.sh: line 25: //venv/bin/activate: No such file or directory
    django_awesome_app | /start.sh: line 35: //venv/bin/gunicorn: No such file or directory
    nginx_awesome_app | 2017/09/27 17:21:31 [emerg] 1#1: open() "/home/notalentgeek/Downloads/awesome_app/logs/nginx-access.log" failed (2: No such file or directory)
    nginx_awesome_app | nginx: [emerg] open() "/home/notalentgeek/Downloads/awesome_app/logs/nginx-access.log" failed (2: No such file or directory)
    django_awesome_app exited with code 127
    nginx_awesome_app exited with code 1

    我想这有什么问题:
  • 如何激活virtualenv(或完全不激活)。
  • 如何在docker-compose.yml中传输volumes

  • 另外,将消息转到容器时未检测到消息日志,因为我的nginx.conf中仍然有 access_log /home/notalentgeek/Downloads/awesome_app/logs/nginx-access.log;

    我该如何解决这些问题?

    最佳答案

    首先,您的nginx.conf引用的是看起来像主机路径的内容,而不是容器路径。 nginx服务将没有/home/notalentgeek/

    您尚未包括Dockerfile,但我想知道问题在于Dockerfile中未安装virtualenv。这就是为什么出现错误/start.sh: line 25: //venv/bin/activate: No such file or directory的原因。

    您可能会发现研究一个示例将有助于进一步了解Docker以及一般容器的工作方式。不幸的是,在过去的几年中,Docker的工作方式和最佳实践发生了很多变化,因此很多示例都已经过时了。最好通读一些文档以及Docker官方文档,以构成一个工作系统。查看活跃的Github项目可以有所帮助,以了解开源系统如何处理Docker。

    我发现nginx-proxy项目有助于将nginx放在Docker容器之前。该项目的自述文件提供了有关如何将其用作docker-compose.yml的良好说明。使用该容器将处理nginx方面,您只需要修复Django应用Dockerfile。

    关于django - Django + Gunicorn + NGINX的docker-compose.yml设置不适用于 `docker-compose up`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46453958/

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