具有 4 个节点的 Docker 容器,无法使用 urllib3 建立连接

发布于 2025-01-12 14:36:23 字数 2217 浏览 6 评论 0原文

我正在设置一个 docker 容器,它生成 4 个节点,这些节点将执行两个 python 脚本:区块链和导入。更多详细信息如下:

Docker 文件:

FROM ubuntu:latest

RUN apt-get update -y
RUN apt-get install -y python3 python3-pip python3-dev build-essential

RUN mkdir /myApp
WORKDIR /myApp

COPY blockchain/requirements.txt /myApp/
RUN pip3 install -r requirements.txt

COPY blockchain/blockchain.py /myApp/

ENTRYPOINT ["python3"]
CMD ["blockchain.py"]

Docker Compose (docker-compose.yml):

version: '3'

services:
  node1:
    build: .
    hostname: node1
    ports:
      - "5001:5000"
  node2:
    build: .
    hostname: node2
    ports:
      - "5002:5000"
  node3:
    build: .
    hostname: node3
    ports:
      - "5003:5000"
  node4:
    build: .
    hostname: node4
    ports:
      - "5004:5000"

这意味着我想创建 4 个节点,在导入脚本中我需要使用 POST 方法连接到该节点。在变量 localIP 中,我使用了本地 @IP :

#I've tried to get the @ using Python
localIP = socket.gethostbyname(hostname)

#I've also used windows CMD: ipconfig to get the @IP
#localIP = '192.168.XX.XX'

#I've also tried the @IP we can get from google: what is my IP address
#localIP = '196.12X.XX.XX'

hosts = [ 
    'https://'+localIP+':5001',
    'https://'+localIP+':5002',
    'https://'+localIP+':5003',
    'https://'+localIP+':5004'
    ]

http = urllib3.PoolManager()

# register nodes - for each node, don't add itself the list
for host in hosts:
    data = { "nodes": [x for x in hosts if x != host] }
    data= json.dumps(data).encode('utf-8')
    res = http.request('POST', host, body=data, headers={'Content-Type': 'application/json'})

但出现以下错误:

urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='192.168.XX.XX', port=5001): Max retries exceeded with url: 
/ (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x000001FB210ACBB0>: Failed to establish 
a new connection: [WinError 10061] No connection could be established because the target computer expressly refused it'))

当我尝试从终端获取节点的 @IP 时,它返回空白值:

docker inspect -f '{{ .NetworkSettings.IPAddress }}' NODE-01-ID

问题是,IP 地址是否错误?我们怎样才能提供正确的IP地址?

谢谢

I'm setting up a docker container which generates 4 nodes that will execute two python scripts: blockchain and import. More details below:

Docker File:

FROM ubuntu:latest

RUN apt-get update -y
RUN apt-get install -y python3 python3-pip python3-dev build-essential

RUN mkdir /myApp
WORKDIR /myApp

COPY blockchain/requirements.txt /myApp/
RUN pip3 install -r requirements.txt

COPY blockchain/blockchain.py /myApp/

ENTRYPOINT ["python3"]
CMD ["blockchain.py"]

Docker Compose (docker-compose.yml):

version: '3'

services:
  node1:
    build: .
    hostname: node1
    ports:
      - "5001:5000"
  node2:
    build: .
    hostname: node2
    ports:
      - "5002:5000"
  node3:
    build: .
    hostname: node3
    ports:
      - "5003:5000"
  node4:
    build: .
    hostname: node4
    ports:
      - "5004:5000"

This means that I want to create 4nodes, and on the import script I will need to connect to this nodes using POST method. In the variable localIP I used my local @IP :

#I've tried to get the @ using Python
localIP = socket.gethostbyname(hostname)

#I've also used windows CMD: ipconfig to get the @IP
#localIP = '192.168.XX.XX'

#I've also tried the @IP we can get from google: what is my IP address
#localIP = '196.12X.XX.XX'

hosts = [ 
    'https://'+localIP+':5001',
    'https://'+localIP+':5002',
    'https://'+localIP+':5003',
    'https://'+localIP+':5004'
    ]

http = urllib3.PoolManager()

# register nodes - for each node, don't add itself the list
for host in hosts:
    data = { "nodes": [x for x in hosts if x != host] }
    data= json.dumps(data).encode('utf-8')
    res = http.request('POST', host, body=data, headers={'Content-Type': 'application/json'})

But I get the following error:

urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='192.168.XX.XX', port=5001): Max retries exceeded with url: 
/ (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x000001FB210ACBB0>: Failed to establish 
a new connection: [WinError 10061] No connection could be established because the target computer expressly refused it'))

When I try get from terminal the @IP of the nodes it returns blank value:

docker inspect -f '{{ .NetworkSettings.IPAddress }}' NODE-01-ID

The question is, Is the IP address wrong ? How can we provide the right IP address ?

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

转身以后 2025-01-19 14:36:23

问题是你明确输入了 IP 地址,而这个 @ 是动态的而不是统计的。如果您在本地计算机上工作,则可以将其保留为 localhost。

The problem is that you're putting the IP address explicitly and this @ is dynamic not statis. If you're working in your local machine, you can keep it as localhost.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文