Dmitry Rusanov
Dmitry Rusanov

Reputation: 551

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:

Upvotes: 4

Views: 5776

Answers (1)

Dmitry Rusanov
Dmitry Rusanov

Reputation: 551

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:

Upvotes: 6

Related Questions