'gunicorn shows connection error [Errno 111]

Hi im building a Django app with docker, gunicorn and nginx and having serious issues calling a post request from the django view.

my login function -

 with schema_context(restaurant_schema):

        app = Application.objects.first()
        #return Response({'domain': domain_name, 'client_id': app.client_id, 'client_secret': app.client_secret,})
        domain_name = 'http://localhost:1337/o/token/'
        

        headers = {'content-type': 'application/json' }

        r = requests.post(domain_name,headers=headers,
                          data={
                              'grant_type': 'password',
                              'username': email,
                              'password': password,
                              'client_id': app.client_id,
                              # 'scope': 'read',
                              'client_secret': app.client_secret,
                          },)

This works fine on on manage.py runserver. but whenever Gunicorn and nginx are added to the mix it throws

ConnectionError at /login HTTPConnectionPool(host='localhost', port=1337): Max retries exceeded with url: /o/token/ (Caused by NewConnectionError(': Failed to establish a new connection

nginx.conf :

upstream example {
    server web:8000;
}


server {

    listen 80;
    #server_name localhost;

    

    location / {
        proxy_pass http://example;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
        proxy_read_timeout 300;
        proxy_connect_timeout 300;
        proxy_send_timeout 300;
    }

}

docker-compose file:

services:
  web:
    build:
      context: ./
      dockerfile: Dockerfile.prod
    command: gunicorn ExampleLiteB.wsgi:application --bind 0.0.0.0:8000 --workers 3
    # volumes:
    #   - static_volume:/home/app/web/staticfiles
    #   - media_volume:/home/app/web/mediafiles
    expose:
      - 8000
    env_file:
      - ./.env.prod
    depends_on:
      - db
    
      
  db:
    image: postgres:14.0-alpine
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    env_file:
      - ./.env.prod.db
    

  nginx:
    build: ./nginx
    volumes:
      - static_volume:/home/app/web/staticfiles
    ports:
      - 1337:80
    depends_on:
      - web


networks:
    djangonetwork:
          driver: bridge

volumes:
  postgres_data:
  static_volume:

i have tried increasing the gunicorn workers but it doesnt work.



Solution 1:[1]

command: gunicorn ExampleLiteB.wsgi:application --bind 0.0.0.0:8000 --workers 3

change to

command: gunicorn ExampleLiteB.wsgi:application --bind 0.0.0.0:8000 --workers 3 -t 0

or my be its -T 0 Check which works and edit this post

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 nude