Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect to a Bitcoin testnet running in a docker container

I am testing some Bitcoin related code and in order to test it have installed bitcoin-testnet-box within a docker container.

It's running fine, and, within the container I can execute commands and see the results.

The Dockerfile is exposing port 19001, which I am mapping to port 49155 as the RPC port for one of the bitcond instances and I am trying to communicate with it using node-bitcoin.

I've written a simple test which aims simply to get the current difficulty.

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();
    });
  });
});

This was failing (see update below) with the error:

{ [Error: connect ECONNREFUSED] code: 'ECONNREFUSED', errno: 'ECONNREFUSED', syscall: 'connect' }

A quick look at docker ps shows

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            

I tried changing the host to both "localhost" and to "0.0.0.0" but got the same result.

Clearly I am missing something simple as the node-bitcoin tests are not really doing anything different.

The command used to run the bitcoin-testnet-box was

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

What might I be doing wrong?

Update

I changed bitcoin.conf as suggested below and now the error message is

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

My bitcoin.conf looks like

# 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

another update

It's worth explaining that I am running docker on my Mac using boot2docker so the IP number I am referring to is the IP that is displayed when I run docker ip, not the IP of my Mac itself. I'm running the test using NodeJS on my Mac, not in the boot2docker VM or the actual Docker container. So, I've tried adding rpcallowip=192.168.1.4 (where 192.168.1.4 is my Mac's IP) to my bitcoind.conf files too just in case. Alas that made no difference, I am still getting the { [Error: Invalid params, response status code: 403] code: -32602 } response.

I have also triple-checked my username and password against what's in the bitcoin.conf file.

Per Chris McKinnel's suggestion below I have run netstat -tunlp within the docker container and it shows:

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     

So I also added rpcallowip=0.0.0.0 to my bitcoin.conf file. Alas still no difference.

finally a solution

Thanks again to Chris McKinnel below setting rpcallowip=* solved the problem. Of course this raises a whole new problem but I'll burn that bridge when I get to it. For now I can test my Bitcoin processes quite happily.

like image 665
Dave Sag Avatar asked Oct 21 '22 04:10

Dave Sag


2 Answers

I think you'll need to add rpcallowip=192.168.59.103 to both of your bitcoin.conf files for the nodes. By default bitcoind will only listen for RPC connections on localhost (according to the docs).

Once you've added your IP to the allow list, you can check to see if it has worked by doing a telnet 192.168.59.103 19001.

To see a list of what your PCs open ports (and from where they're accepting connections), do a netstat -tunlp.

like image 163
Chris McKinnel Avatar answered Oct 27 '22 14:10

Chris McKinnel


I would just change

rpcallowip=192.168.*.*

That way it as at least in a C class range

like image 26
Sam Hodge Avatar answered Oct 27 '22 12:10

Sam Hodge