Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Docker-compose django-python container communication

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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')

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading