5

我一直在尝试在 docker-compose 中设置一个环境,其中有几个容器:

  • 姜戈
  • Nginx
  • Postgres
  • 数据库数据
  • 贮存

我使用了以下配置:

 app:
  restart: always
  build: src
  expose:
    - "8000"
  links:
    - postgres:postgres
  volumes_from:
    - storage_files_1
  env_file: .env
  command: gunicorn barbell.wsgi:application \
            -b 0.0.0.0:8000 -w 4

nginx:
  restart: always
  build: nginx
  ports:
    - "80:80"
    - "443:443"
  volumes_from:
    - storage_files_1
  links:
    - app:app

postgres:
  restart: always
  image: postgres:latest
  volumes_from:
    - storage_data_1
  ports:
    - "5432:5432"

我的支持 nginx 站点的配置文件如下所示:

server {

    listen 80;
    server_name localhost;
    error_log /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;

    location /static {
        alias /static/;
        autoindex on;
    }

    location / {
         proxy_pass http://app:8000;
         proxy_set_header X-Forwarded-Host $server_name;
         proxy_set_header X-Real-IP $remote_addr;
         add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
    }
}

而且它不起作用 - nginx 总是返回 502,但完美地提供静态文件。我也尝试了与 uwsgi 相同的设置,但没有运气。但是,当我将 Django 与 nginx 结合使用并从同一个容器中提供所有内容时,一切正常(同样,在 uwsgi 和 gunicorn 上)。

知道我错过了什么吗?

更新

以下是 nginx 日志:

*1 connect() failed (111: Connection refused) while connecting to upstream,
client: 172.17.42.1, server: 0.0.0.0, request: "GET / HTTP/1.1", upstream:   
"http://172.17.1.75:8000/", host: "localhost"
4

2 回答 2

4

It turned out that Gunicorn was the culprit. Putting its configuration into a file resolved the issue.

gunicorn_config.py put in the same folder as manage.py:

bind = "0.0.0.0:8000"
loglevel = "INFO"
workers = "4"
reload = True

errorlog = "/var/log/gunicorn/error.log"
accesslog = "/var/log/gunicorn/access.log"

And some changes in docker-compose.yml:

app:
  restart: always
  build: src
  expose:
    - "8000"
  links:
    - postgres:postgres
  volumes_from:
    - storage_files_1
  env_file: .env
  command: gunicorn --config=gunicorn_config.py barbell.wsgi:application

Now it works as it should.

于 2015-04-26T09:49:42.277 回答
-1

因此,我没有看到您对错误日志以及您可能会或可能不会发生的事情的任何进一步反馈;但是,我已将您的示例简化为最简单的Docker+Django+NGINX工作演示:

请参阅:docker-django-test

注意:这是在我的一些使用autodock的基础设施上运行的如果你想复制它,你需要这个片段docker-compose.yml

autodock:
    image: prologic/autodock
    ports:
        - "1338:1338/udp"
        - "1338:1338/tcp"
    volumes:
        - /var/run/docker.sock:/var/run/docker.sock

autodockhipache:
    image: prologic/autodock-hipache
    links:
        - autodock
        - hipache:redis

hipache:
    image: hipache
    ports:
        - 80:80
        - 443:443

参见:基于 Docker 的 mini-PaaS

于 2015-04-23T01:15:00.460 回答