Author

Topic: How to connect docker container to bitcoin node? (Read 177 times)

legendary
Activity: 1568
Merit: 6660
bitcoincleanup.com / bitmixlist.org
host.docker.internal is a domain name not an IP address. You need the IP address of the Docker container because bitcoin-cli doesn't do address resolution when connecting to the RPC server. You can find it using such tools as "ip addr" that show all the network interfaces - it will be called "docker0" or something like that.
legendary
Activity: 3332
Merit: 3116
Another thing could be your server configuration because you need to one the port in the farewal to give access to the RPC calls.

Reference: https://stackoverflow.com/questions/11585377/python-socket-error-errno-111-connection-refused

The way i did to personally fix this kind of issues in my local servers was with the command:

Code:
firewall-cmd --zone=public --add-port=8332/tpc --permanent

And to verify your open ports you can use nmap:

Code:
nmap localhost
legendary
Activity: 1932
Merit: 1273
You should use the internal IP on the Python container, not localhost.

Code:
connection_url="http://%s:%[email protected]:8332/wallet/testwallet" 
credentials=('testuser','testpassword')
rpc_connection = AuthServiceProxy(connection_url % credentials)
rpc_connection.getnewaddress()


If it still not works, try refer to https://www.howtogeek.com/devops/how-to-connect-to-localhost-within-a-docker-container/

internal ip like 127.0.01? but that will connect to docker localhost network not the server localhost?

I meant the internal IP of Docker.

Since you already set host.docker.internal:host-gateway, you can try to use host.docker.internal as the IP, to access the IP outside Docker container.

Try to use the modified code as I quoted above.
newbie
Activity: 5
Merit: 0
when I make call to this script I get this error in return [Errno 111] Connection refused

Ps: when I run the same script outside of docker container it works fine Any solutions??

I only use docker to run certain application, but AFAIK you got this error because you didn't perform port forwarding. On terminal, i'd use parameter -p HOST_PORT:CONTAINER_PORT (should be -p 8332:8332 in your case) to perform port forwarding. But i don't know the equivalent for Dockerfile.

++ is it safe to run the node and connect to it using a script like this? locally?

Since you're the one who create the script, it should be safe. But if you have very serious security concern or writing script for business/enterprise usage, i'd recommend you to check whether library you use is malicious or not.

So running on local and connecting to rpc on local is safe, just i have to check the python library that's all?
How do i know that this library "https://github.com/jgarzik/python-bitcoinrpc" is safe?
newbie
Activity: 5
Merit: 0
You should use the internal IP on the Python container, not localhost.

Code:
connection_url="http://%s:%[email protected]:8332/wallet/testwallet" 
credentials=('testuser','testpassword')
rpc_connection = AuthServiceProxy(connection_url % credentials)
rpc_connection.getnewaddress()


If it still not works, try refer to https://www.howtogeek.com/devops/how-to-connect-to-localhost-within-a-docker-container/

internal ip like 127.0.01? but that will connect to docker localhost network not the server localhost?
legendary
Activity: 1932
Merit: 1273
You should use the internal IP on the Python container, not localhost.

Code:
connection_url="http://%s:%[email protected]:8332/wallet/testwallet" 
credentials=('testuser','testpassword')
rpc_connection = AuthServiceProxy(connection_url % credentials)
rpc_connection.getnewaddress()


If it still not works, try refer to https://www.howtogeek.com/devops/how-to-connect-to-localhost-within-a-docker-container/
newbie
Activity: 5
Merit: 0
I have a python script inside docker container and a bitcoin node outside of docker in my local machine , this is my bitcoin.conf information

Code:
rpcuser=testuser
rpcpassword=testpassword
testnet=1
prune=550
server=1
[test]
rpcallowip=0.0.0.0/0
rpcport=8332
bind=127.0.0.1
minconf=1

and this is my docker compose conf file



Code:
app:
build:
  context: .
restart: always
volumes:
  - .:/app
extra_hosts:
  - "host.docker.internal:host-gateway"

then in my python script i'm trying to connect to the bitcoin node using this:

Code:
connection_url="http://%s:%s@localhost:8332/wallet/testwallet" 
credentials=('testuser','testpassword')
rpc_connection = AuthServiceProxy(connection_url % credentials)
rpc_connection.getnewaddress()

when I make call to this script I get this error in return [Errno 111] Connection refused

Ps: when I run the same script outside of docker container it works fine Any solutions?? ++ is it safe to run the node and connect to it using a script like this? locally?
Jump to: