Nginx – HTTPConnectionPool(host=’0.0.0.0′, port=5000): Max retries exceeded with url

djangodocker-composenginxpspdfkitpython-requests

I am encountering this error, when i try to make a call to a service that is deployed using docker-compose on port 5000 from a django application also deployed using docker-compose on port 8000. I am also using nginx.

 ConnectionError at /documents/
 HTTPConnectionPool(host='localhost', port=5000): Max retries exceeded with url: 
 /api/documents (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 
 0x7f976607f290>: Failed to establish a new connection: [Errno 111] Connection refused'))

Here's the request object right before the request is made

      kwargs  
  {'data': None,
   'files': {'file': ('XXX',
                      <InMemoryUploadedFile: XXX.pdf (application/pdf)>,
                      'application/pdf',
                      {})},
   'headers': {'Authorization': 'Token token=XXX',
               'Content-Type': 'application/pdf',
               'PSPDFKit-API-Version': '2020.1.3'},
   'json': None}
  method  
  'post'
  session 
  <requests.sessions.Session object at 0x7f954da972d0>
  url 
  'http://127.0.0.1:5000/api/documents'

Here are the relevant files

docker-compose

version: '3.7'

services:
  web:
    build:
      context: ./www
      dockerfile: Dockerfile.prod
    command: gunicorn app.wsgi:application --bind 0.0.0.0:8000
    volumes:
      - ./www:/usr/src/app
      - static_volume:/home/app/web/staticfiles
      - media_volume:/home/app/web/mediafiles
    expose:
      - 8000
    env_file: env.prod
    depends_on:
      - db
  db:
    image: postgres:12.0-alpine
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    env_file: env.prod
  nginx:
    build: ./nginx
    volumes:
      - static_volume:/home/app/web/staticfiles
      - media_volume:/home/app/web/mediafiles
    ports:
      - 1337:80
    depends_on:
      - web
  pspdfkit:
    image: "pspdfkit/pspdfkit:2020.1"
    environment:
      PGUSER: XXX
      PGPASSWORD: XXX
      PGDATABASE: XXX
      PGHOST: db
      PGPORT: 5432

      # Activation key for your PSPDFKit Server installation.
      ACTIVATION_KEY: XXXX

      # Secret token used for authenticating API requests.
      API_AUTH_TOKEN: XXXX

      # Base key used for deriving secret keys for the purposes of authentication.
      SECRET_KEY_BASE: XXXX

      # Public key used for verification of JWTs from web clients. It has to be in the PEM format.
      JWT_PUBLIC_KEY: |
        -----BEGIN PUBLIC KEY-----
        XXXXTYYYYY
        -----END PUBLIC KEY-----
      JWT_ALGORITHM: RS256

      # Credentials to access the admin dashboard.
      DASHBOARD_USERNAME: XXX
      DASHBOARD_PASSWORD: YYY
    depends_on:
      - db
    restart: always
    ports:
      - "5000:5000"
    volumes:
      - asset_storage:/srv/asset_storage

volumes:
  postgres_data:
  static_volume:
  media_volume:
  asset_storage:

nginx.conf

upstream app {
    server web:8000;
}


server {
    client_max_body_size 20M;
    listen 80;

    location / {
        proxy_pass http://app;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }

    location /staticfiles/ {
        alias /home/app/web/staticfiles/;
    }
    location /mediafiles/ {
        alias /home/app/web/mediafiles/;
    }

}

Settings.py

  PSPDFKIT_SERVER_HOST = "http://127.0.0.1:5000"
  PSPDFKIT_EXTERNAL_SERVER = "http://127.0.0.1:5000"

I've tried changing the url to localhost, 0.0.0.0, pspdfkit(from the docker-compose file), and also the public ip of the server, but i end up getting the above error in all cases except when i used the public ip (It couldn't establish connection)

I'm not well versed in nginx, so I might be missing some important configuration step that is causing this issue. Help would be appreciated!

Best Answer

The error comes from the use of 0.0.0.0

Docker introduces a virtual network, so each docker-isntance sees itself as 127 not the host machine

(thx @Jmons)


To launch your code locally, use your fixed IPv4 address.

For Windows users: open a command prompt (cmd) and run the ipconfig command, retrieve the @IP which is atIPv4 address. . . . . . . . . . . . . .:it is generally of type 192.168.x.x IPv4 address in command prompt

Related Topic