与Django + Gunicorn + NGINX的docker-compoes.yml设置无法在`docker-compose up上工作

时间:2017-09-27 17:32:54

标签: django docker nginx docker-compose

这是我的文件夹结构。

./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;

我该如何解决这些问题?

1 个答案:

答案 0 :(得分:0)

首先,您的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。