Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make request to localhost form docker container?

I use docker-compose. I want to make request to localhost. But I'm getting this error:

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

My code:

import requests

response = requests.get('http://127.0.0.1:9011')
print(response.content)

This is my docker-compose.yml.

version: '3.4'

services:
  web:
    build: .
    command: runserver
    env_file:
      - .env
    volumes:
      - 'static:/opt/app/static:rw'
    ports:
      - 8000:8000

volumes:
  static:
like image 268
Dmitry Rusanov Avatar asked Oct 27 '25 03:10

Dmitry Rusanov


1 Answers

I have find the answer. You should add network_mode: host to docker-compose.yml

version: '3.4'

services:
  web:
    build: .
    command: runserver
    env_file:
      - .env
    volumes:
      - 'static:/opt/app/static:rw'
    ports:
      - 8000:8000
    network_mode: host  # Added this


volumes:
  static:
like image 59
Dmitry Rusanov Avatar answered Oct 29 '25 07:10

Dmitry Rusanov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!