Skip to content

Commit

Permalink
Merge pull request #334 from pmienk/master
Browse files Browse the repository at this point in the history
Dockerfile/image building for bx.
  • Loading branch information
pmienk authored Jan 11, 2025
2 parents c5d89ca + 34b26c8 commit c9aa0b9
Show file tree
Hide file tree
Showing 4 changed files with 333 additions and 7 deletions.
24 changes: 24 additions & 0 deletions docker/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
# dockerized libbitcoin

## bitcoin explorer

A linux image can be generated with the script below. Currently, this is only tested against the `version3` branch with or without the `v3.8.0` tag.

### linux

The following outlines usage of the `bx-linux` artifacts.

#### Image construction

The image construction can then continue via an other invocatio with the following parameters:

```
./bx-linux.sh --build-dir=<image build dir> --source=<gsl xml source>
```

#### Container usage

The generated image can be used to execute the `bx` tool via the following:

```
docker run --interactive --tty --rm --user $(id -u):$(id -g) libbitcoin/bitcoin-explorer:<version>
```

## bitcoin server

A linux image can be generated with the script below. Currently, this is only tested against the `version3` branch with or without the `v3.8.0` tag.
Expand Down
50 changes: 43 additions & 7 deletions docker/bs-linux.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
FROM alpine:latest AS build

ENV OPTIMIZATION="-O3"
ENV BUILD_DEPS="build-base linux-headers gcc make autoconf automake libtool pkgconf git wget bash"
ENV CPPFLAGS="-O3"

ENV CFLAGS="${OPTIMIZATION}"
ENV CXXFLAGS="${OPTIMIZATION}"

RUN apk update && \
apk add --update ${BUILD_DEPS}
Expand All @@ -10,33 +13,66 @@ RUN mkdir -p /build/src /build/obj /build/prefix

COPY developer_setup.sh /build
COPY src/ /build/src

RUN /build/developer_setup.sh \
--build-target=dependencies \
--build-src-dir=/build/src \
--build-obj-dir=/build/obj \
--prefix=/build/prefix \
--build-mode=configure \
--disable-shared \
--enable-static \
--enable-isystem \
--without-consensus \
--with-icu \
--build-icu \
--build-boost \
--build-zmq \
--build-zmq

RUN /build/developer_setup.sh \
--build-target=libbitcoin \
--build-src-dir=/build/src \
--build-obj-dir=/build/obj \
--prefix=/build/prefix \
--build-mode=configure \
--build-target=all \
--disable-shared \
--enable-static \
--enable-isystem \
--without-consensus \
--with-icu \
--build-icu \
--build-boost \
--build-zmq

RUN /build/developer_setup.sh \
--build-target=project \
--build-src-dir=/build/src \
--build-obj-dir=/build/obj \
--prefix=/build/prefix \
--build-mode=configure \
--disable-shared \
--enable-static && \
rm -rf /build/src /build/obj
--enable-static \
--enable-isystem \
--without-consensus \
--with-icu \
--build-icu \
--build-boost \
--build-zmq

RUN rm -rf /build/src /build/obj



FROM alpine:latest AS runtime

ENV BUILD_DEPS="build-base linux-headers gcc make autoconf automake libtool pkgconf git wget bash"
ENV RUNTIME_DEPS="bash gcc"

RUN apk update && \
apk add --update ${BUILD_DEPS}
apk add --update ${RUNTIME_DEPS}

COPY --from=build /build/prefix/bin/bs /bitcoin/bs
COPY bs-linux-startup.sh /bitcoin/startup.sh

# Bitcoin P2P
EXPOSE 8333/tcp
EXPOSE 8333/udp
Expand Down
74 changes: 74 additions & 0 deletions docker/bx-linux.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
FROM alpine:latest AS build

ENV OPTIMIZATION="-O3"
ENV BUILD_DEPS="build-base linux-headers gcc make autoconf automake libtool pkgconf git wget bash"

ENV CFLAGS="${OPTIMIZATION}"
ENV CXXFLAGS="${OPTIMIZATION}"

RUN apk update && \
apk add --update ${BUILD_DEPS}

RUN mkdir -p /build/src /build/obj /build/prefix

COPY developer_setup.sh /build
COPY src/ /build/src

RUN /build/developer_setup.sh \
--build-target=dependencies \
--build-src-dir=/build/src \
--build-obj-dir=/build/obj \
--prefix=/build/prefix \
--build-mode=configure \
--disable-shared \
--enable-static \
--enable-isystem \
--with-icu \
--build-icu \
--build-boost \
--build-zmq

RUN /build/developer_setup.sh \
--build-target=libbitcoin \
--build-src-dir=/build/src \
--build-obj-dir=/build/obj \
--prefix=/build/prefix \
--build-mode=configure \
--disable-shared \
--enable-static \
--enable-isystem \
--with-icu \
--build-icu \
--build-boost \
--build-zmq

RUN /build/developer_setup.sh \
--build-target=project \
--build-src-dir=/build/src \
--build-obj-dir=/build/obj \
--prefix=/build/prefix \
--build-mode=configure \
--disable-shared \
--enable-static \
--enable-isystem \
--with-icu \
--build-icu \
--build-boost \
--build-zmq \
--without-tests

RUN rm -rf /build/src /build/obj



FROM alpine:latest AS runtime

ENV RUNTIME_DEPS="gcc"

RUN apk update && \
apk add --update ${RUNTIME_DEPS}

COPY --from=build /build/prefix/bin/bx /bitcoin/bx

WORKDIR /bitcoin
ENTRYPOINT [ "/bitcoin/bx" ]
192 changes: 192 additions & 0 deletions docker/bx-linux.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
#!/bin/bash

set -e

SCRIPT_IDENTITY=$(basename "$0")

display_message()
{
printf "%s\n" "$@"
}

display_error()
{
>&2 printf "%s\n" "$@"
}

fatal_error()
{
display_error "$@"
display_error ""
display_help
exit 1
}

display_help()
{
display_message "Usage: ./${SCRIPT_IDENTITY} [OPTION]..."
display_message "Manage the generation of a docker image of bitcoin-explorer (bx)."
display_message "Script options:"
display_message " --build-dir=<value> Directory for building docker image"
display_message " --source=<value> xml file for gsl generation."
display_message " --tag=<value> git tag to utilize with source instructions."
display_message " --build-only docker build without reinitializing build-dir."
display_message " --init-only Initialize build-dir for docker image building."
display_message ""
}

parse_command_line_options()
{
for OPTION in "$@"; do
case ${OPTION} in
# Specific
(--build-dir=*) DIR_BUILD="${OPTION#*=}";;
(--source=*) SOURCE="${OPTION#*=}";;
(--tag=*) TAG="${OPTION#*=}";;
(--build-only) BUILD_ONLY="yes";;
(--init-only) INIT_ONLY="yes";;
# Standard
(--help) DISPLAY_HELP="yes";;
esac
done
}

display_state()
{
display_message "Parameters:"
display_message " DIR_BUILD : ${DIR_BUILD}"
display_message " SOURCE : ${SOURCE}"
display_message " TAG : ${TAG}"
display_message " BUILD_ONLY : ${BUILD_ONLY}"
display_message " INIT_ONLY : ${INIT_ONLY}"
display_message "Deduced:"
display_message " DIR_BUILD_PROJ : ${DIR_BUILD_PROJ}"
display_message " VERSION : ${VERSION}"
}

dispatch_help()
{
if [[ ${DISPLAY_HELP} ]]; then
display_help
exit 0
fi
}

validate_parameterization()
{
if [ -z ${DIR_BUILD} ]; then
fatal_error " --build-dir=<value> required."
fi

if [ -z ${SOURCE} ]; then
fatal_error " --source=<value> required."
fi
}

initialize_environment()
{
# https://stackoverflow.com/questions/59895/how-do-i-get-the-directory-where-a-bash-script-is-located-from-within-the-script
local DIR_SCRIPT=$(cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )

# initialize DIR_BUILD_PROJ
pushd "${DIR_SCRIPT}/.."
DIR_BUILD_PROJ="$(pwd)"
popd

if [ -f ${DIR_BUILD_PROJ}/${SOURCE} ]; then
SOURCE_VERSION=$(grep '<repository' ${DIR_BUILD_PROJ}/${SOURCE} | \
grep libbitcoin-explorer | \
sed -n s/.*version=\"//p | \
sed -n s/\".*//p)
else
fatal_error " --source=<value> must be a valid filename in the expected path."
fi

# rationalize intended version
if [ -z ${TAG} ]; then
VERSION="${SOURCE_VERSION}"
else
if [[ "${TAG}" =~ ^v.* ]]; then
VERSION=$(echo "${TAG}" | sed -n s/v//p)
else
VERSION="${TAG}"
fi
fi
}

generate_instructions()
{
display_message "Generate developer_setup.sh..."

# create developer_setup.sh
pushd ${DIR_BUILD_PROJ}
eval gsl -q -script:templates/gsl.developer_setup.sh ${SOURCE}
chmod +x output/libbitcoin-*/developer_setup.sh
popd
}

clean_build_directory()
{
# cleanup build directory
if [ -d ${DIR_BUILD} ]; then
display_message "Directory '${DIR_BUILD}' found, emptying..."
pushd ${DIR_BUILD}
rm -rf developer_setup.sh src/
mkdir -p "${DIR_BUILD}/src"
popd
else
display_message "Directory '${DIR_BUILD}' not found, building..."
mkdir -p "${DIR_BUILD}/src"
fi
}

initialize_build_directory()
{
if [[ $BUILD_ONLY ]]; then
return 0
fi

clean_build_directory
generate_instructions

display_message "Initialize build directory contents..."
cp ${DIR_BUILD_PROJ}/output/libbitcoin-explorer/developer_setup.sh ${DIR_BUILD}

pushd ${DIR_BUILD}

local BUILD_TAG_PARAM=""
if [ -z ${TAG} ]; then
BUILD_TAG_PARAM=""
else
BUILD_TAG_PARAM="--build-tag=${TAG}"
fi

./developer_setup.sh ${BUILD_TAG_PARAM} \
--build-sync-only \
--build-src-dir="${DIR_BUILD}/src" \
--build-target=all

popd
}

dockerize()
{
if [[ $INIT_ONLY ]]; then
return 0
fi

pushd ${DIR_BUILD}
display_message "----------------------------------------------------------------------"
display_message "docker build : libbitcoin/bitcoin-explorer:${VERSION}"
display_message "----------------------------------------------------------------------"
docker build -t libbitcoin/bitcoin-explorer:${VERSION} -f ${DIR_BUILD_PROJ}/docker/bx-linux.Dockerfile .
popd
}

parse_command_line_options "$@"
dispatch_help
validate_parameterization
initialize_environment
initialize_build_directory
display_state
dockerize

0 comments on commit c9aa0b9

Please sign in to comment.