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

Docker Container with 4 Nodes, No connection could be established using urllib3

我正在设置一个 docker 容器,它生成 4 个节点,这些节点将执行两个 python 脚本:blockchain 和 import。更多详情如下:

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 撰写 (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 时,它returns 空白值:

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

问题是,IP地址是不是错了?我们如何提供正确的 IP 地址?

谢谢

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