如何向本地主机docker容器发出请求?

时间:2019-12-06 15:23:39

标签: python docker request localhost containers

我使用docker-compose。我想向本地主机发出请求。 但我收到此错误:

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

我的代码:

import requests

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

这是我的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:

1 个答案:

答案 0 :(得分:0)

我找到了答案。您应将network_mode: host添加到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: