Send an http request from django server inside docker container to another server that is located on localhost not inside docker [duplicate]

I have a django project inside docker container. I have several containers for backend, frontend, redis, celery and db thar are served by docker compose. I need to send an http request made by python lib requests to a server that is outside of docker but is started on the same computer (on localhost). This code worked perfectly when django server wasn't inside docker container. There is 500 status code from this function at the moment. How can i do this?

Function that should send request:

class ConnectLocalHost(APIView):     
    response = requests.post('http://127.0.0.1:9001')     
    return Httpresponse('Message sent', status=status.HTTP_200_OK)

docker-compose.yml file:

# docker-compose.yml

version: "3"

volumes:
  db-data:
  storage:

services:
  backend:
    working_dir: /backend
    build:
      context: ./backend
      dockerfile: Dockerfile
    ports:
      - "8000:8000"
    command: bash -c "
            ./wait-for-postgres.sh database user user123 postgres &&
            python manage.py makemigrations --settings=test_project.prod_settings &&
            python manage.py makemigrations task_manager_app --settings=test_project.prod_settings &&
            python manage.py migrate --settings=test_project.prod_settings &&
            python manage.py migrate task_manager_app --settings=test_project.prod_settings &&
            python manage.py collectstatic --noinput --settings=test_project.prod_settings &&
            python manage.py auto_superuser --settings=test_project.prod_settings &&
            gunicorn test_project.wsgi:application --bind 0.0.0.0:8000"
    volumes:
      - storage:/backend/storage/
    depends_on:
      - database
      - redis
    environment:
      REDIS_HOST: redis
      POSTGRES_USER: user
      POSTGRES_PASSWORD: user123
      POSTGRES_DB: postgres
      POSTGRES_PORT: 5432
    restart: always

  frontend:
    working_dir: /frontend
    build:
      context: ./frontend
      dockerfile: Dockerfile
    command: yarn start
    ports:
      - "9000:9000"
    depends_on:
      - backend
    restart: always

  redis:
    image: redis:alpine
    expose:
      - 6379
    restart: always

  celery:
    working_dir: /backend
    build:
      context: ./backend
    entrypoint: /backend/worker.sh

    volumes:
      - .:/app
      - storage:/backend/storage/
    environment:
      CELERY_BROKER_URL: "redis://redis:6379/0"
      CELERY_RESULT_BACKEND: "redis://redis:6379/0"
      POSTGRES_HOST: database
      POSTGRES_NAME: postgres
      POSTGRES_USER: user
      POSTGRES_PASSWORD: user123
      POSTGRES_PORT: 5432
    depends_on:
      - database
      - backend
      - redis
    restart: always

  database:
    image: postgres:14.2
    volumes:
      - db-data:/var/lib/postgresql/data/
    environment:
      POSTGRES_USER: user
      POSTGRES_PASSWORD: user123
      POSTGRES_DB: postgres
    ports:
      - "5432:5432"
    restart: always

I have already tried to switch network to host, but some errors occured while connection to database. Also i have tried to add host.docker.internal:host-gateway, but nothing changed.

Back to Top