【问题标题】:Max retries exceeded with url: Failed to establish a new connection: [Errno 111] Connection refused'超过 url 的最大重试次数:无法建立新连接:[Errno 111] 连接被拒绝\'
【发布时间】:2023-01-31 14:02:35
【问题描述】:

我不断收到此错误: HTTPConnectionPool(host='127.0.0.1', port=8001): Max retries exceeded with url: /api/v1/auth/sign_in (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f0f8cbdd430>: Failed to establish a new connection: [Errno 111] Connection refused'))

我搜索了 stackoverflow,但找不到对我有帮助的解决方案。

这是我的代码示例:

host = 'http://127.0.0.1:8001'

response = requests.request(method=request_data['method'],
                            url=f'{host}/{settings.ACCOUNTS_API_PREFIX}{request_data["url"]}',
                            json=json_data,
                            params=params,
                            headers=headers,
                            )

基本上我正在尝试发送一个 POST 请求以在服务上验证我自己,但是我不断收到上述错误。

我有 2 个容器——一个是 Web 应用程序 (Django),另一个是存储用户所有详细信息以对他们进行身份验证的帐户。

两个容器都已启动并正在运行,我可以打开网站,可以为帐户打开 API swagger,但是我无法发送 POST 请求并获得任何响应。

容器设置如下:

  container_1:
    build:
      context: ./container_1
      dockerfile: Dockerfile
    env_file:
      - '.env'
    stdin_open: true 
    tty: true        
    ports:
      - '8000:8000'
    expose:
      - 8000
    volumes:
      - ./data:/data
    working_dir: /data
    command: [ "./start.sh" ]
    networks:
      - web

  container_2:
        context: ./container_2
        dockerfile: Dockerfile
      env_file: 'accounts/settings/.env'
      stdin_open: true 
      tty: true        
      environment:
        - 'DJANGO_SETTINGS_MODULE=project.settings'
      expose:
        - 8000
      ports:
        - "8001:8000"
      volumes:
        - ./data:/app
      networks:
        - web

有人可以帮我弄清楚吗?

【问题讨论】:

  • 您需要进行一些 docker 网络设置,以便容器可以相互通信,或者使用 --net=host
  • 是的,在 docker-compose 中我为所有容器设置了网络,检查它我可以看到所有容器都在同一个网络中
  • 拥有关于容器的代码也会很有帮助。
  • 我添加了容器设置。
  • host = 'http://127.0.0.1:8001' 更改为 host = 'http://container_2:8000'

标签: python django docker api python-requests


【解决方案1】:

@jordanm 的回答是正确的,它解决了我的问题:

host = 'http://127.0.0.1:8001'
host = 'http://container_2:8000'

【讨论】:

    猜你喜欢
    • 2021-11-13
    • 2017-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-13
    • 2020-11-22
    • 2014-09-14
    • 2021-06-08
    相关资源
    最近更新 更多