Backend Helpers | Automation and Software Development for Cloud Applicationses

Building Bitcoin Core from Source Code Using Alpine Linux and Docker

Bitcoin protocol is compounded from a set of different technologies. Each technology has a particular functionality that implements one more concepts about cryptography, distributed systems, consensus algorithms, and so on. This post shows how to compile the source code of the referent client known as Bitcoin Core using Alpine Linux and Docker.

Steps to Build Bitcoin from Source

Step 1: Install Docker

The following Link contains the instructions for install docker in case you do not have it already in your system.

Step 2. Create a Bitcoin Configuration File

Bitcoin is a very complex system has too many configuration parameters. A full configuration example can be found at this link. We will use a reduced version of the configuration file. To do this, we need to create a file named bitcoin.conf and copy the following configuration parameters:


server=1
keypool=1000
testnet=0
rpcauth=alice:c36d66fce611afc562db21c5798ddc89$ae58c4c45ae887b63b308d7e283a15fa230ab4ccfa00778ea88076cf8c66aff0
listen=1
rpcport=8332
rpcconnect=127.0.0.1
maxconnections=2
shrinkdebugfile=1
  

Step 3. Create a Script for Building Bitcoin from Code

Create a file named build_bitcoin_core.sh and copy the following instructions to that file for cloning the bitcoin code and generating the build:


#/bin/sh
cd /src
git clone https://github.com/bitcoin/bitcoin.git 
cd bitcoin
./autogen.sh
./configure --with-incompatible-bdb  --with-gui=no 
make
make install
  

Step 4. Create Docker File

Create a file named Dockerfile and copy the following code:



FROM alpine:latest
MAINTAINER Backend Helpers <info@backendhelpers.co>

USER root

ENV GIT_SSH=/tmp/ssh
ENV GIT_TRACE=1

RUN apk add --update alpine-sdk
RUN apk add --no-cache gcc git libffi musl-dev  libffi-dev autoconf automake
RUN apk add --no-cache openssh-client make db-dev openssl openssl-dev
RUN apk add --no-cache boost boost-dev libtool libevent libevent-dev

ADD ./build_bitcoin_core.sh /
RUN /build_bitcoin_core.sh
ADD ./bitcoin.conf /bitcoin.conf

CMD ["/usr/local/bin/bitcoind" , "-conf /bitcoin.conf", "-datadir=/btdata"]

Step 5. Create the Docker Image

The following command will create a Docker image with the instructions provided in Dockerfile:

docker build . -t bitcoin-core

Step 6. Running a Docker Container With a External Volume

Docker containers have a very small of portion of disk that is not enough to deal with the Bitcoin protocol, nonetheless, Docker provides the concept of volume that may be used to share a directory in your local machine with the running container. The following command runs a container with a local directory attached as a volume:

docker run --rm -v  /tmp/btdata:/btdata -dt bitcoin-core:latest