Friday, 22 June 2018

Run Bitcoind in Docker container


Use Docker container to run Bitcoin core

0) Install Docker
 Docker can be installed on Windows, Linux, MacOS

1) Create new directory for running bitcoin core in Docker
 mkdir bitcoin-core-docker
 cd bitcoin-core-docker

2) Create new dir  to store bitcoin.conf
 mkdir node; cd node

3) Create bitcoin.conf and add the contents
server=1
regtest=1
port=12000
rpcport=12001
rpcallowip=0.0.0.0/0
rpcuser=username
rpcpassword=password
daemon=1
txindex=1

4) Build Docker container 
  cd .. ( to bitcoin-core-docker)
  create Dockerfile

5) Add contents to Docker
# Dockerfile must start with a FROM instruction
# FROM instruction specifies the Base Image from which you are building
# FROM <image>[:<tag>]
FROM ubuntu:16.04

ENV BTCVERSION=0.15.1

ENV BTCPREFIX=/bitcoin-prefix

RUN apt-get update && apt-get install -y git build-essential wget pkg-config curl libtool autotools-dev automake libssl-dev libevent-dev bsdmainutils libboost-system-dev libboost-filesystem-dev libboost-chrono-dev libboost-program-options-dev libboost-test-dev libboost-thread-dev

WORKDIR /

RUN mkdir -p /berkeleydb

#download berkeley db
RUN git clone -b 0.15 --single-branch  https://github.com/bitcoin/bitcoin.git

WORKDIR /berkeleydb

RUN wget http://download.oracle.com/berkeley-db/db-4.8.30.NC.tar.gz && tar -xvf db-4.8.30.NC.tar.gz && rm db-4.8.30.NC.tar.gz && mkdir -p db-4.8.30.NC/build_unix/build

ENV BDB_PREFIX=/berkeleydb/db-4.8.30.NC/build_unix/build
WORKDIR /berkeleydb/db-4.8.30.NC/build_unix

RUN ../dist/configure --disable-shared --enable-cxx --with-pic --prefix=$BDB_PREFIX

RUN make install

RUN apt-get update && apt-get install -y libminiupnpc-dev libzmq3-dev  libprotobuf-dev protobuf-compiler libqrencode-dev

WORKDIR /bitcoin

RUN git checkout v${BTCVERSION} && mkdir -p /bitcoin/bitcoin-${BTCVERSION}

WORKDIR /bitcoin

RUN ./autogen.sh

RUN ./configure CPPFLAGS="-I${BDB_PREFIX}/include/ -O2" LDFLAGS="-L${BDB_PREFIX}/lib/ -static-libstdc++" --prefix=${BTCPREFIX}

RUN make

RUN make install DESTDIR=/bitcoin/bitcoin-${BTCVERSION}

RUN mv /bitcoin/bitcoin-${BTCVERSION}${BTCPREFIX} /bitcoin-${BTCVERSION} && strip /bitcoin-${BTCVERSION}/bin/* && rm -rf /bitcoin-${BTCVERSION}/lib/pkgconfig && find /bitcoin-${BTCVERSION} -name "lib*.la" -delete && find /bitcoin-${BTCVERSION} -name "lib*.a" -delete

WORKDIR /

RUN tar cvf bitcoin-${BTCVERSION}.tar bitcoin-${BTCVERSION}

# copy bitcoin,conf
ADD . /bitcoin-${BTCVERSION}

# expose rpc port for the node to allow access from outside container
EXPOSE 12001

WORKDIR /bitcoin-${BTCVERSION}

6)build the docker image
   docker build -t bitcoin-docker .
  • The -t flag sets a name for the image.
  • The . tells Docker to looker for Dockerfile in the current directory

 6.1)list the built images:
  docker images

7) run the image in container
docker run -it -p 5000:12001 bitcoin-docker
  • -it is required for interactive processes (like a bash shell)
  • -p maps Ubuntu port 5000 to the container’s exposed port 12001, which is where Bitcoin rpc will be listening 

if everything works, docker present a bash shell. Both the node directory and the bitcoin.conf file were copied to the container by the ADD instruction in the Dockerfile, so they will be present in the current working directory. 

In the bash shell, run
7.1) bitcoind -datadir=node -daemon
7.2) bitcoin-cli -datadir=node getinfo

8) Connect to bitcoind from outside Docker (open second terminal window)
curl --user username:password --data '{"method": "getinfo"}' http://127.0.0.1:5000

9) In first terminal window, run this to stop bitcoind
 bitcoin-cli -datadir=node stop
9.1) In second terminal window, exit the docker container
 exit

Use Docker to run Bitcoin core as service
TBC…

PS: we can push the image to docker hub and share with others

# tag the image

docker tag  bitcoin <username>/bitcoin:custom
# push to docker hub

docker push <username>/bitcoin:custom
# run the image, if image is not available locally, docker pull from repo

docker run -it -p 5000:12001 <username>/bitcoin:custom

No comments:

Post a Comment