0
0

More than 3 years have passed since last update.

bitcoindをdockerで動かしてみる

Posted at

概要

bitcoind v0.19.1をalpine linux on dockerで動かしてみました
bitcoind周りのサービスをリファクタリングすることにしたので
その準備として、まずは、2020/5/28現在で、最新のbitcoinのバージョン(v0.19.1)のものを
Dockerでビルドして動かしてみることにしました。

せっかくなので、alpine linuxを使って小さめなサイズになるように工夫しました。

実施事項

まずは、以下のDockerfileを作成しました。

Dockerfile

FROM alpine as berkeley-db

RUN sed -i 's/http\:\/\/dl-cdn.alpinelinux.org/https\:\/\/alpine.global.ssl.fastly.net/g' /etc/apk/repositories
RUN apk --no-cache add autoconf
RUN apk --no-cache add automake
RUN apk --no-cache add build-base
RUN apk --no-cache add libressl

ENV BERKELEYDB_VERSION=db-4.8.30.NC
ENV BERKELEYDB_PREFIX=/opt/${BERKELEYDB_VERSION}

RUN wget https://download.oracle.com/berkeley-db/${BERKELEYDB_VERSION}.tar.gz
RUN tar -xzf *.tar.gz
RUN sed s/__atomic_compare_exchange/__atomic_compare_exchange_db/g -i ${BERKELEYDB_VERSION}/dbinc/atomic.h
RUN mkdir -p ${BERKELEYDB_PREFIX}

WORKDIR /${BERKELEYDB_VERSION}/build_unix

RUN ../dist/configure --enable-cxx --disable-shared --with-pic --prefix=${BERKELEYDB_PREFIX}
RUN make -j4
RUN make install
RUN rm -rf ${BERKELEYDB_PREFIX}/docs


FROM alpine as bitcoin-core

# copy berkeley-db from berkeley-db
COPY --from=berkeley-db /opt /opt

RUN sed -i 's/http\:\/\/dl-cdn.alpinelinux.org/https\:\/\/alpine.global.ssl.fastly.net/g' /etc/apk/repositories
RUN apk --no-cache add autoconf
RUN apk --no-cache add automake
RUN apk --no-cache add boost-dev
RUN apk --no-cache add build-base
RUN apk --no-cache add chrpath
RUN apk --no-cache add file
RUN apk --no-cache add gnupg
RUN apk --no-cache add libevent-dev
RUN apk --no-cache add libressl
RUN apk --no-cache add libressl-dev
RUN apk --no-cache add libtool
RUN apk --no-cache add linux-headers
RUN apk --no-cache add protobuf-dev
RUN apk --no-cache add zeromq-dev
RUN set -ex \
  && for key in \
    90C8019E36C2E964 \
  ; do \
    gpg --batch --keyserver keyserver.ubuntu.com --recv-keys "$key" || \
    gpg --batch --keyserver pgp.mit.edu --recv-keys "$key" || \
    gpg --batch --keyserver keyserver.pgp.com --recv-keys "$key" || \
    gpg --batch --keyserver ha.pool.sks-keyservers.net --recv-keys "$key" || \
    gpg --batch --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys "$key" ; \
  done

ENV BITCOIN_VERSION=0.19.1
ENV BITCOIN_PREFIX=/opt/bitcoin-${BITCOIN_VERSION}

RUN wget https://bitcoin.org/bin/bitcoin-core-${BITCOIN_VERSION}/SHA256SUMS.asc
RUN wget https://bitcoin.org/bin/bitcoin-core-${BITCOIN_VERSION}/bitcoin-${BITCOIN_VERSION}.tar.gz
RUN gpg --verify SHA256SUMS.asc
RUN grep " bitcoin-${BITCOIN_VERSION}.tar.gz\$" SHA256SUMS.asc | sha256sum -c -
RUN tar -xzf *.tar.gz

WORKDIR /bitcoin-${BITCOIN_VERSION}

RUN sed -i '/AC_PREREQ/a\AR_FLAGS=cr' src/univalue/configure.ac
RUN sed -i '/AX_PROG_CC_FOR_BUILD/a\AR_FLAGS=cr' src/secp256k1/configure.ac
RUN sed -i s:sys/fcntl.h:fcntl.h: src/compat.h
RUN ./autogen.sh
RUN ./configure LDFLAGS=-L`ls -d /opt/db*`/lib/ CPPFLAGS=-I`ls -d /opt/db*`/include/ \
    --prefix=${BITCOIN_PREFIX} \
    --mandir=/usr/share/man \
    --disable-tests \
    --disable-bench \
    --disable-ccache \
    --with-gui=no \
    --with-utils \
    --with-libs \
    --with-daemon
RUN make -j4
RUN make install
# delete symbols from executables for security
RUN strip ${BITCOIN_PREFIX}/bin/bitcoin-cli
RUN strip ${BITCOIN_PREFIX}/bin/bitcoin-tx
RUN strip ${BITCOIN_PREFIX}/bin/bitcoind
RUN strip ${BITCOIN_PREFIX}/lib/libbitcoinconsensus.a
RUN strip ${BITCOIN_PREFIX}/lib/libbitcoinconsensus.so.0.0.0


FROM alpine
LABEL maintainer="Hiroki Tanaka"

# copy applications from bitcoin-core
COPY --from=bitcoin-core /opt /opt

ARG USER_ID
ARG GROUP_ID

ENV BITCOIN_VERSION=0.19.1
ENV HOME /bitcoin

# add user with specified (or default) user/group ids
ENV USER_ID ${USER_ID:-1000}
ENV GROUP_ID ${GROUP_ID:-1000}
ENV PATH=/opt/bitcoin-${BITCOIN_VERSION}/bin:${PATH}

RUN sed -i 's/http\:\/\/dl-cdn.alpinelinux.org/https\:\/\/alpine.global.ssl.fastly.net/g' /etc/apk/repositories
RUN apk --no-cache add \
  boost \
  boost-program_options \
  libevent \
  libressl \
  libzmq \
  su-exec \
  bash

RUN addgroup -g ${GROUP_ID} bitcoin; \
    adduser -D -u ${USER_ID} -G bitcoin -h /bitcoin -s "/bin/bash" bitcoin;

# add utilities
ADD ./bin /usr/local/bin
RUN chmod +x /usr/local/bin/btc_*

VOLUME ["/bitcoin"]

EXPOSE 8332 8333 18332 18333 18444 28333

WORKDIR /bitcoin

CMD ["su-exec", "bitcoin", "bitcoind"]

こちらをビルドしました。

% docker build -t bitcoind:0.19.1 -f Dockerfile .

そうしたら・・以下のようなエラーに遭遇

g++: fatal error: Killed signal terminated program cc1plus

調べてみたら・・・原因は「メモリ不足」のようでした・・・

Docker Desktopから Preferences > Resources > Memory を大きく(3GBから8GB)にして再度ビルドしてみます
(最近ローカルの開発マシンをMac Miniにしたので、ここら辺は十分リソースがあって嬉しいです)

今度は、うまくビルドできました。

あとは、docker-compose-data/testnet/bitcoin.conf を作成しておき、以下のように実行します。

% docker run --rm -it -p 18332:18332 -v `pwd`/docker-compose-data/testnet:/bitcoin/.bitcoin bitcoind:0.19.1 bash
# su-exec bitcoin bitcoind

こちらで、ちゃんと動作するのを確認しました。

以下でも同様のTipsを記載しています。
https://kumano-te.com/activities/developed-bitcoind-with-alpine-docker

以上になります。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0