Home > Enterprise >  Docker-compose django-python container communication
Docker-compose django-python container communication

Time:03-06

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

CodePudding user response:

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

  • Related