具有 4 个节点的 Docker 容器,无法使用 urllib3 建立连接
我正在设置一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是你明确输入了 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.