Maciej Szlosarczyk
Maciej Szlosarczyk

Reputation: 809

Docker-compose: nginx does not work with django and gunicorn

I've been trying to set up an environment in docker-compose where there are several containers:

I've used the following configuration:

 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"

My nginx sites-enabled config file looked like this:

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"';
    }
}

And it doesn't work - nginx always returns 502, but serves static files perfectly. I also tried the same setup with uwsgi, no luck. However, when I combine the Django with nginx and serve everything from the same container, everything works (again, both on uwsgi and gunicorn).

Any idea what am I missing?

Update

Here are the nginx logs:

*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"

Upvotes: 5

Views: 3845

Answers (2)

James Mills
James Mills

Reputation: 19030

So I haven't seen any further feedback from you regarding error logs and what may or may not be hapenning for you; however I've stripped your example down to it's simplest as a demonstration of Docker+Django+NGINX working:

See: docker-django-test

NB: This is running on some infrastructure of mine that uses autodock If you want to replicate this you'll need this snippet of 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

See: A Docker-based mini-PaaS

Upvotes: -1

Maciej Szlosarczyk
Maciej Szlosarczyk

Reputation: 809

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.

Upvotes: 4

Related Questions