gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 02:58:13 26 4
gpt4 key购买 nike

我使用 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:

关于python - 如何从 docker 容器向本地主机发出请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59215911/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com