Docker容器无法连接主机Bitcoin RPC的问题排查求助
问题:Docker部署Bitfeed无法连接裸机Bitcoin Core RPC(拒绝连接)
核心需求
通过Docker部署Bitfeed,连接同一主机裸机运行的Bitcoin Core,不使用Docker host网络模式(host模式可正常连接,但不利于Bitcoin Core隔离),解决RPC连接"connection refused"错误。
相关配置
Bitcoin Core配置(bitcoin.conf)
# RaspiBolt: bitcoind configuration # /home/bitcoin/.bitcoin/bitcoin.conf # Bitcoin daemon server=1 txindex=1 # Network listen=1 listenonion=1 proxy=127.0.0.1:9050 bind=127.0.0.1 # Connections rpcport=8332 rpcbind=127.0.0.1 rpcallowip=127.0.0.1 rpcallowip=192.168.50.11 #trying the machine's IP address rpcallowip=172.17.0.1/16 #the IP range for my docker0 network interface rpcallowip=172.18.0.1/16 #the IP range for the bitfeed_default docker network rpcauth=[user:passwordhash] zmqpubrawblock=tcp://127.0.0.1:28332 zmqpubrawtx=tcp://127.0.0.1:28333 zmqpubsequence=tcp://127.0.0.1:28334 whitelist=download@127.0.0.1 # for Electrs # Raspberry Pi optimizations maxconnections=40 maxuploadtarget=5000 # Initial block download optimizations #dbcache=2000 #blocksonly=1
Docker Compose配置(docker-compose.yml)
version: "2.1" services: web: image: ghcr.io/bitfeed-project/bitfeed-client:v2.3.4 restart: on-failure stop_grace_period: 1m depends_on: - 'api' environment: TARGET: 'docker' BACKEND_HOST: 'api' BACKEND_PORT: '6000' expose: - '3080:80' api: image: ghcr.io/bitfeed-project/bitfeed-server:v2.3.4 user: '1001:1001' restart: on-failure stop_grace_period: 1m volumes: - '/home/bitcoin/.bitcoin:/bitcoin:ro' extra_hosts: - 'host.docker.internal:host-gateway' #network_mode: host environment: PORT: '6000' BITCOIN_HOST: 'host.docker.internal' BITCOIN_ZMQ_RAWBLOCK_PORT: '28332' BITCOIN_ZMQ_RAWTX_PORT: '28333' BITCOIN_ZMQ_SEQUENCE_PORT: '28334' BITCOIN_RPC_PORT: '8332' BITCOIN_RPC_COOKIE: /bitcoin/.cookie
已尝试操作
- 使用Docker host网络模式运行Bitfeed,可正常连接Bitcoin RPC,但不符合隔离需求;
- 多次修改bitcoin.conf的rpcallowip规则及ufw防火墙规则,问题未解决。
预期与实际结果
- 预期:Bitfeed的api容器成功连接Bitcoin RPC并同步内存池;
- 实际:api容器尝试RPC连接时抛出"connection refused"错误。
解决方案
1. 修改Bitcoin Core的rpcbind配置
当前rpcbind=127.0.0.1仅让Bitcoin Core监听本地回环地址,Docker容器通过host.docker.internal访问的是主机的物理网卡IP,而非回环地址。需添加主机的局域网IP到rpcbind:
rpcbind=127.0.0.1 rpcbind=192.168.50.11 # 替换为你的主机实际局域网IP
这样Bitcoin Core会同时监听回环地址和局域网IP,允许Docker容器通过主机IP访问RPC端口。
2. 调整ZMQ绑定地址
当前ZMQ仅绑定127.0.0.1,同样无法被Docker容器访问,需修改为绑定主机局域网IP或0.0.0.0(注意:0.0.0.0会允许所有地址访问,仅限可信局域网环境使用):
zmqpubrawblock=tcp://192.168.50.11:28332 zmqpubrawtx=tcp://192.168.50.11:28333 zmqpubsequence=tcp://192.168.50.11:28334
3. 验证防火墙规则
确保主机ufw或其他防火墙允许Docker网络段访问8332(RPC)、28332-28334(ZMQ)端口:
# 允许Docker默认网络段访问RPC端口 ufw allow from 172.17.0.0/16 to any port 8332 # 允许Bitfeed自定义网络段访问RPC端口 ufw allow from 172.18.0.0/16 to any port 8332 # 同理开放ZMQ端口 ufw allow from 172.17.0.0/16 to any port 28332:28334 ufw allow from 172.18.0.0/16 to any port 28332:28334
4. 调整Docker Compose的BITCOIN_HOST配置
将BITCOIN_HOST从host.docker.internal改为主机的实际局域网IP(如192.168.50.11),避免DNS解析或路由问题:
environment: # ...其他配置 BITCOIN_HOST: '192.168.50.11' # 替换为你的主机局域网IP
5. 重启服务
- 重启Bitcoin Core:
sudo systemctl restart bitcoind
- 重启Bitfeed容器:
docker-compose down && docker-compose up -d
内容的提问来源于stack exchange,提问作者TheFury
相关产品推荐
相关产品推荐

