Docker Nginx does not listen to browser

Viewed 1863

I have this docker-compose.yml file:

version: '2'
services:
  nginx:
    image: nginx:latest
    container_name: nz01
    ports:
      - "8001:8000"
    volumes:
      - ./src:/src
      - ./config/nginx:/etc/nginx/conf.d
    depends_on:
      - web
  web:
    build: .
    container_name: dz01
    depends_on:
      - db
    volumes:
      - ./src:/src
    expose:
      - "8000"
  db:
    image: postgres:latest
    container_name: pz01
    ports:
        - "5433:5432"
    volumes:
      - postgres_database:/var/lib/postgresql/data:Z
volumes:
    postgres_database:
        external: true

And this dockerfile:

FROM python:3.5
ENV PYTHONUNBUFFERED 1
RUN mkdir /src
RUN mkdir /static
WORKDIR /src
ADD ./src /src
RUN pip install -r requirements.pip
CMD python manage.py collectstatic --no-input;python manage.py migrate; gunicorn computationalMarketing.wsgi -b 0.0.0.0:8000

The web and postgres server does not return an error log, just the success when I run docker-compose build and docker-compose up -d. At this moment the three containers are running, but when I go to the browser and navigate to: localhost:8001 it does not work.

It shows the "connection has been restarted" error message.

Despite that, the web server still does not return any error, so I guess that I have everything properly configurated in my Django app. I really believe that the problem is related to Nginx, because when I review the Nginx log (using kinematic) it is still empty.

Why wouldn't Nginx be listening to connections?

Hint: This error is happening in a new project. I tried to understand whether I have anything wrong, I'm running an old project and it works perfectly. I tried to copy the working project in my new folder and remove all existent containers and then try to run this old project in a new folder and there is the surprise. It does not work now, despite being an exact copy of the project that works in the other folder...

EDIT In my repo I have a config/nginx folder with the helloworld.conf file:

upstream web {  
  ip_hash;
  server web:8000;
}

server {

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

    location / {
        proxy_pass http://web/;
    }
    listen 8001;
    server_name localhost;
}

Still with the same error... I do not see any log error.

Django container log

Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  No migrations to apply.
[2018-11-05 13:00:09 +0000] [8] [INFO] Starting gunicorn 19.7.1
[2018-11-05 13:00:09 +0000] [8] [INFO] Listening at: http://0.0.0.0:8000 (8)
[2018-11-05 13:00:09 +0000] [8] [INFO] Using worker: sync
[2018-11-05 13:00:09 +0000] [11] [INFO] Booting worker with pid: 11
2 Answers

You nginx configure should be like:

upstream web {  
   ip_hash;
   server web:8000;
}

server {
     location / {
         proxy_pass http://web/;
     }
     listen 8001;
     server_name localhost;
}

Since this kind of problems usually are difficult to debug/reproduce I have created dummy example to just run Django app and serve it via Nginx. You can try to adjust it to your needs. Please forgive me if I have missed something, or done that shouldn't be, but I'm unfamiliar with Django framework.

Dockerfile for Django container:

FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code && \
    pip install django
WORKDIR /code
ADD helloworld /code

docker-compose.yml:

version: '3'

services:
  nginx:
    image: nginx:latest
    container_name: nginx
    ports:
      - "80:80"
    volumes:
      - ./config/nginx:/etc/nginx/conf.d
    depends_on:
      - web
  web:
    build: .
    container_name: django
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - ./helloworld:/code
    expose:
      - "8000"

config/nginx/django.conf:

upstream web {  
   ip_hash;
   server web:8000;
}

server {
     listen 80;
     server_name localhost;

     location / {
         proxy_pass http://web/;
     }
}

Django app is inside helloworld folder.

For this example, traffic is simply passed. Proper way would be using Unix sockets instead of ports but again I'm unfamiliar with Django.

Related