Python requests cannot resolve Docker-Compose services

Question:

I have 2 docker-compose set ups in separate repos. When I run them I want them to talk to each other on the same network. Both apps are up and running and are on the same network by using docker inspect.

But when I try to make requests from nhec_ter_app to vtn_app using python requests it’s not able to resolve.

docker-compose.yml 1

version: '3.2'

services:

  vtn_app: &app
    tty: true
    cap_add:
     - SYS_PTRACE
    build:
      context: .
      args:
        requirements: requirements/development.txt
    container_name: vtn
    environment:
      - DEBUG=False
      - PYTHONUNBUFFERED=1
      - TESTING_USER=oh_hey_there
      - SQL_USER=postgres
      - SQL_HOST=postgres     
      - SQL_PASSWORD=postgres
      - SQL_DB=postgres
      - SQL_PORT=5432
    restart: always
    volumes:
      - .:/app:delegated
    depends_on:
      - postgres
    ports:
      - 8000:80
      - 8080:8080
      - 8081:8081
    command: make adev
    networks:
      - nhec-ter_ter-web-net

  postgres:
      image: postgres:10
      container_name: vtn_postgres
      environment:
          - POSTGRES_USER=postgres
          - POSTGRES_PASSWORD=postgres
          - POSTGRES_DB=postgres
      ports:
          - 5432:5432
      networks:
        - nhec-ter_ter-web-net

networks:
  nhec-ter_ter-web-net:
    external:
      name: nhec-ter_ter-web-net

docker-compose.yml 2

version: "3.9"
   
services:
  nhec_ter_app:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    container_name: nhec_ter
    environment:
      - DEBUG=True
      - PYTHONUNBUFFERED=1
      - TER_BASIC_AUTH_USER=pricesAdmin
      - TER_BASIC_AUTH_PASSWORD=Barry0325!
      - CELERY_BROKER_URL=redis://broker:6379/0
      - VTN_API_URL=http://vtn_app/
      - VTN_API_TOKEN=NHEC_UTILITY
    volumes:
      - .:/code
    ports:
      - "9000:8000"
    depends_on:
      - db
      - celery-worker
      - broker
    networks:
      - nhec-ter_ter-web-net
  nhec_ter_test:
    build: .
    command: python manage.py test
    container_name: nhec_ter_test
    environment:
      - DEBUG=True
      - PYTHONUNBUFFERED=1
    volumes:
      - .:/code
    depends_on:
      - db

  db:
    image: postgres:10
    container_name: nhec_ter_postgres
    ports:
      - 6000:5432
    volumes:
      - db_data:/var/lib/postgresql/data
    environment:
      POSTGRES_DB: nhec
      POSTGRES_USER: nhec
      POSTGRES_PASSWORD: nhec
    networks:
      - nhec-ter_ter-web-net
  celery-worker:
    build: .
    command: celery -A nhec_ter worker -l DEBUG
    depends_on:
      - db
      - broker
    environment:
      CELERY_BROKER_URL: redis://broker:6379/0
    networks:
      - nhec-ter_ter-web-net
  broker:
    image: redis:6-alpine
    networks:
      - nhec-ter_ter-web-net

networks:
  nhec-ter_ter-web-net:
    name: nhec-ter_ter-web-net
    driver: bridge

volumes: 
  db_data: {}

My celery task hangs cause it can’t make a request call to vtn_app
Here’s what I’ve tried to do in my python manage.py shell to illustrate – if I can get this to work like it does in our prod env (not dockerized) then I know the tasks will work.

python manage.py shell:

>>> import requests
>>> resp = requests.get('http://vtn_app/')
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/urllib3/connection.py", line 174, in _new_conn
    conn = connection.create_connection(
  File "/usr/local/lib/python3.8/site-packages/urllib3/util/connection.py", line 72, in create_connection
    for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
  File "/usr/local/lib/python3.8/socket.py", line 918, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/urllib3/connectionpool.py", line 703, in urlopen
    httplib_response = self._make_request(
  File "/usr/local/lib/python3.8/site-packages/urllib3/connectionpool.py", line 398, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/usr/local/lib/python3.8/site-packages/urllib3/connection.py", line 239, in request
    super(HTTPConnection, self).request(method, url, body=body, headers=headers)
  File "/usr/local/lib/python3.8/http/client.py", line 1256, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/usr/local/lib/python3.8/http/client.py", line 1302, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/usr/local/lib/python3.8/http/client.py", line 1251, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/usr/local/lib/python3.8/http/client.py", line 1011, in _send_output
    self.send(msg)
  File "/usr/local/lib/python3.8/http/client.py", line 951, in send
    self.connect()
  File "/usr/local/lib/python3.8/site-packages/urllib3/connection.py", line 205, in connect
    conn = self._new_conn()
  File "/usr/local/lib/python3.8/site-packages/urllib3/connection.py", line 186, in _new_conn
    raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x7fab9dded0a0>: Failed to establish a new connection: [Errno -2] Name or service not known
Asked By: Kyle Calica-St

||

Answers:

I solved this; I am using aiohttp-devtools and running this application in docker using adev

This means the server runs on development ports.

I was able to run requests.get('http://vtn_app:8080/') and received a proper status code from the server!

Answered By: Kyle Calica-St