Linux上的docker compose"主机网关"无法连接到RPC(v220.10.1)



随着Docker20.10的出现,host-gateway应该可以在Linux平台上使用(正如这个精彩的答案中详细介绍的那样(。因此,应该可以创建一个与平台无关的docker-compose脚本。(我自己在Debian上。(

以下是一些有助于我走到这一步的问题和答案的链接:这里、这里和这里(以及其他一些答案和评论(

我正在尝试创建一个运行The Graph的脚本,其中包括让ipfspostgres在Docker容器内运行,并连接到Docker之外的区块链实例(在端口8545上(。这是脚本:

version: '3'
services:
graph-node:
extra_hosts:
- "host.docker.internal:host-gateway"
image: graphprotocol/graph-node
ports:
- '8000:8000'
- '8001:8001'
- '8020:8020'
- '8030:8030'
- '8040:8040'
depends_on:
- ipfs
- postgres
environment:
postgres_host: postgres
postgres_user: graph-node
postgres_pass: let-me-in
postgres_db: graph-node
ipfs: 'ipfs:5001'
ethereum: 'localhost:http://host.docker.internal:8545'
RUST_LOG: info
ipfs:
image: ipfs/go-ipfs:v0.4.23
ports:
- '5001:5001'
volumes:
- ./data/ipfs:/data/ipfs
postgres:
image: postgres
ports:
- '5432:5432'
command: ["postgres", "-cshared_preload_libraries=pg_stat_statements"]
environment:
POSTGRES_USER: graph-node
POSTGRES_PASSWORD: let-me-in
POSTGRES_DB: graph-node
volumes:
- ./data/postgres:/var/lib/postgresql/data

Docker启动很好,ipfspostgresgraph-node的实例都启动得很好,但随后graph-node的RPC调用(对区块链(都失败了,错误如下:

WARN Trying again after eth_getBlockByNumber(0, false) RPC call failed (attempt #18) with result Err(Transport error: Error(Connect, Os { code: 111, kind: ConnectionRefused, message: "Connection refused" }))

我用错extra-hosts了吗?我可以做些什么来让这个脚本既能在我的Linux机器上运行,也能在Mac和Windows用户上运行?

谢谢!

主机上运行的应用程序可能绑定到与Docker使用的接口不同的接口。

您可以使用netstat:进行检查

$ netstat -pan | grep 8545
tcp6       0      0 127.0.0.1:8545          :::*                    LISTEN      496150/java

如果它正在侦听127.0.0.1,就像本例中一样,这意味着它只能通过环回接口访问。

解决方案是找出主机网关指向的IP地址,并确保服务绑定到该IP地址,而不是127.0.0.1。

如果该服务在所有接口上都可用(例如,包括您的wifi网络(时没有问题,您可以绑定到0.0.0.0,使其在所有接口都可用。

如果主机系统是Linux,则主机防火墙规则可能应用于容器之间的通信。您可以检查sysctl是否是这种情况,对于以下设置,它将返回1

$ sysctl net.bridge.bridge-nf-call-arptables
1
$ sysctl net.bridge.bridge-nf-call-iptables
1
$ sysctl net.bridge.bridge-nf-call-ip6tables

您可以通过将这些值设置为"0"来修复此行为:

$ sudo sysctl net.bridge.bridge-nf-call-arptables=0
net.bridge.bridge-nf-call-arptables = 0
$ sudo sysctl net.bridge.bridge-nf-call-iptables=0
net.bridge.bridge-nf-call-iptables = 0
$ sudo sysctl net.bridge.bridge-nf-call-ip6tables=0
net.bridge.bridge-nf-call-ip6tables = 0

最新更新