随着 Docker 的出现 20.10
, host-gateway
应该可以在 Linux 平台上使用(详见 this 精彩答案)。因此,应该可以创建一个 docker-compose
与平台无关的脚本。 (我自己在 Debian 上。)
以下是一些问题和答案的链接,这些问题和答案对我走到这一步很有帮助:here , here , 和 here (以及其他一些答案和评论)
我正在尝试创建一个脚本来运行 The Graph ,这涉及到 ipfs
和 postgres
在 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 启动正常,ipfs
的实例, postgres
,以及 graph-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 用户?谢谢!
最佳答案
在您的主机上运行的应用程序可能绑定(bind)到与 Docker 使用的接口(interface)不同的接口(interface)。
您可以通过 netstat
查询:
$ netstat -pan | grep 8545
tcp6 0 0 127.0.0.1:8545 :::* LISTEN 496150/java
如果正在监听 127.0.0.1
,就像在这个例子中一样,这意味着它只能通过环回接口(interface)访问。解决方案是找出主机网关指向的 IP 地址,并确保服务绑定(bind)到该 IP 地址而不是 127.0.0.1。
如果服务在所有接口(interface)上都可用(包括例如您的wifi网络)都没有问题,您可以绑定(bind)到
0.0.0.0
使其在所有接口(interface)上可用。
关于linux - Linux 上的 docker-compose `host-gateway` 无法连接到 RPC (v20.10.1),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65496504/