0

So I am running a django node and a regular python script using docker-compose. The python script regularly asks django backend for data. However, when I docker-compose up I get this error:

requests.exceptions.ConnectionError: HTTPConnectionPool(host='0.0.0.0', port=8080): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f3d6e45dc10>: Failed to establish a new connection: [Errno 111] Connection refused'))

Here is my docker-compose.yaml:

version: "3"
services:
  backend:
    command: >
      sh -c "python3 manage.py wait_for_db && 
             python3 manage.py migrate &&
             python3 manage.py runserver 0.0.0.0:8080"
    build:
      context: ./backend
      dockerfile: Dockerfile
    volumes:
      - ./backend:/backend
    ports:
      - "8080:8080"

  monitor:
    command: >
      sh -c "python3 main.py"
    build:
      context: ./monitor
      dockerfile: Dockerfile
    volumes:
      - ./monitor:/monitor
    ports:
      - "8082:8082"

from monitor I do:

response = requests.get(url = 'http://0.0.0.0:8080/')

What is a correct way to communicate between nodes in docker?

P.S Cors origins are allowed in django

1
  • Use the name of the service as the hostname - url='http://backend:8080/' Commented Mar 5, 2022 at 12:19

1 Answer 1

1

It can't connect because from within the monitor container, backend is not at 0.0.0.0 but at a address assigned by docker within the default network it creates. And the service name is the alias for the container ip. So backend service can be accessed via the alias backend

Read more about it here.

To make it work. Change the monitor script to

requests.get(url = 'http://backend:8080')

Sign up to request clarification or add additional context in comments.

8 Comments

I've tried it out, but still gives the same error, just with "backend": requests.exceptions.ConnectionError: HTTPConnectionPool(host='backend', port=8080): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7ffb168bec10>: Failed to establish a new connection: [Errno 111] Connection refused'))
How about if you use http://backend does that work?
Looks somehow for me like a django issue, but django itself is working in browser
Without the port it uses port 80: requests.exceptions.ConnectionError: HTTPConnectionPool(host='backend', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fa55b122160>: Failed to establish a new connection: [Errno 111] Connection refused'))
Yes.. try with port http://backend:8080
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.