【问题标题】:How to connect to a Bitcoin testnet running in a docker container如何连接到在 docker 容器中运行的比特币测试网
【发布时间】:2014-09-26 19:18:29
【问题描述】:

我正在测试一些与比特币相关的代码,为了测试它已经在 docker 容器中安装了 bitcoin-testnet-box

它运行良好,并且在容器内我可以执行命令并查看结果。

Dockerfile 是exposing port 19001,我将其映射到端口49155 作为bitcond 实例之一的RPC 端口,我正在尝试使用node-bitcoin 与其通信。

我写了一个简单的测试,目的只是为了得到当前的难度。

var bitcoin = require('bitcoin'),
    client = new bitcoin.Client({
      host: "192.168.59.103",
      port: 49155,
      user: "admin1",
      pass: "123"
    });

describe("Core Wallet Functions", function() {

  it("can get the current bitcoin difficulty", function(done){
    client.getDifficulty(function(err, difficulty){
      console.log("got response", err, difficulty);
      expect(err).to.equal(null);
      expect(difficulty).to.equal(1);
      done();
    });
  });
});

这失败了(请参阅下面的更新)并出现错误:

{ [错误:连接 ECONNREFUSED] 代码:'ECONNREFUSED', 错误号:'ECONNREFUSED', 系统调用:“连接”}

快速浏览docker ps 节目

CONTAINER ID        IMAGE                                COMMAND             CREATED             STATUS              PORTS                                                NAMES
8b04ed26d9e3        freewil/bitcoin-testnet-box:latest   /bin/bash           3 hours ago         Up 8 minutes        0.0.0.0:49155->19001/tcp, 0.0.0.0:49156->19011/tcp   bitcoind            

我尝试将主机更改为“localhost”和“0.0.0.0”,但结果相同。

显然我错过了一些简单的事情,因为 node-bitcoin tests 并没有真正做任何不同的事情。

用于运行bitcoin-testnet-box的命令是

docker run -ti --name bitcoind -P -p 49155:19001 freewil/bitcoin-testnet-box

我可能做错了什么?

更新

我按照下面的建议更改了bitcoin.conf,现在错误消息是

[Error: Invalid params, response status code: 403]

我的bitcoin.conf 看起来像

# testnet-box functionality
testnet=1
dnsseed=0
upnp=0

rpcallowip=192.168.59.103
rpcallowip=192.168.1.4
rpcallowip=0.0.0.0

# listen on different ports than default testnet
port=19000
rpcport=19001

# always run a server, even with bitcoin-qt
server=1

# enable SSL for RPC server
#rpcssl=1

rpcuser=admin1
rpcpassword=123

另一个更新

值得解释的是,我在我的 Mac 上使用 boot2docker 运行 docker,所以我指的 IP 号是我运行 docker ip 时显示的 IP,而不是我的 Mac 本身的 IP。我在我的 Mac 上使用 NodeJS 运行测试,而不是在 boot2docker VM 或实际的 Docker 容器中。因此,为了以防万一,我尝试将rpcallowip=192.168.1.4(其中192.168.1.4 是我的Mac 的IP)添加到我的bitcoind.conf 文件中。唉,这没什么区别,我仍然收到 { [Error: Invalid params, response status code: 403] code: -32602 } 回复。

我还根据 bitcoin.conf 文件中的内容对我的用户名和密码进行了三次检查。

根据下面 Chris McKinnel 的建议,我在 docker 容器中运行了 netstat -tunlp,它显示:

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:19000           0.0.0.0:*               LISTEN      65/bitcoind     
tcp6       0      0 :::19000                :::*                    LISTEN      65/bitcoind     
tcp6       0      0 :::19001                :::*                    LISTEN      65/bitcoind     
tcp6       0      0 :::19011                :::*                    LISTEN      75/bitcoind     

所以我还将rpcallowip=0.0.0.0 添加到我的bitcoin.conf 文件中。唉,还是没有区别。

终于找到解决方案了

再次感谢 Chris McKinnel 下面的设置 rpcallowip=* 解决了这个问题。当然,这提出了一个全新的问题,但当我遇到它时,我会烧掉那座桥。现在我可以非常愉快地测试我的比特币流程了。

【问题讨论】:

  • 您是尝试从 VM 内部还是从 VM 外部连接到 docker 客户端?
  • 我正在尝试从 VM 外部连接

标签: node.js docker bitcoin bitcoind bitcoin-testnet


【解决方案1】:

我认为您需要将 rpcallowip=192.168.59.103 添加到节点的两个 bitcoin.conf 文件中。默认情况下,bitcoind 只会监听 localhost 上的 RPC 连接(根据the docs)。

将您的 IP 添加到允许列表后,您可以通过 telnet 192.168.59.103 19001 来检查它是否有效。

要查看您的电脑打开哪些端口(以及它们从哪里接受连接)的列表,请发送netstat -tunlp

【讨论】:

  • 感谢克里斯的帮助。 telnet 192.168.59.103 49155 能够连接但被退回。再次运行我的测试返回了[Error: Invalid params, response status code: 403],这更好,但还不够。
  • 403 表示禁止。你确定你按照它想要的方式传递密码/用户名吗?
  • 感谢@tkone 我已经三次检查了user: "admin1"pass: "123" 值,并将它们与bitcoin.conf 文件中的rpcuser=admin1rpcpassword=123 行进行了比较,它们完全匹配。我还交叉引用了test over at the node-bitcoin project,但我看不出有什么不同。
  • 啊,您使用的是boot2docker - 您是否将 docker 端口范围从您的boot2docker VM 转发到您的主机? netstat -tunlp 在您的容器中看起来像什么?尝试在您的配置中设置rpcallowip=* 看看是否会有所不同。
  • 哇!感谢@ChrisMcKinnel 设置rpcallowip=* 工作。所以现在我需要弄清楚如何缩小范围。然而,这是一个完全不同的问题。
【解决方案2】:

我只是改变

rpcallowip=192.168.*.*

这样至少在 C 类范围内

【讨论】:

  • 好主意 Sam - 我正在寻找一种方法来准确地告诉 IP bitcoind 看到了什么,以便我可以收紧;但出于我的测试目的,目前还可以。
猜你喜欢
  • 2020-04-22
  • 2014-03-25
  • 2016-10-05
  • 1970-01-01
  • 2019-07-26
  • 2017-03-16
  • 2018-09-21
  • 2023-03-21
  • 2022-10-13
相关资源
最近更新 更多