如何向 localhost 表单 docker 容器发出请求?
How to make request to localhost form docker container?
我使用 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:
我已经找到答案了。您应该将 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:
我使用 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:
我已经找到答案了。您应该将 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: