From 881faee7ffb0418d91f54fc71036c82b7b9a33dd Mon Sep 17 00:00:00 2001 From: weichen-qsnetwork Date: Thu, 20 Apr 2023 09:58:05 -0400 Subject: [PATCH 1/2] QB-1793 Update Dockerfile --- Dockerfile | 50 +++++++++++++++++++++++++++++++------------------- Makefile | 13 ++++++++++--- entrypoint.sh | 3 +++ 3 files changed, 44 insertions(+), 22 deletions(-) create mode 100644 entrypoint.sh diff --git a/Dockerfile b/Dockerfile index e9f9d443..ba6af0fe 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,41 +1,53 @@ -# Simple usage with a mounted data directory: -# > docker build -t stratos-chain . -# > docker run -it -p 46657:46657 -p 46656:46656 -v ~/.stratos-chain:/stratos-chain/.stratos-chain stratos-chain stratos-chain init -# > docker run -it -p 46657:46657 -p 46656:46656 -v ~/.stratos-chain:/stratos-chain/.stratos-chain stratos-chain stratos-chain start -FROM golang:1.15-alpine AS build-env +FROM golang:1.19-alpine AS build-env # Set up dependencies -ENV PACKAGES curl make git libc-dev bash gcc linux-headers eudev-dev python3 +ENV PACKAGES curl make git libc-dev bash gcc linux-headers eudev-dev python3 \ + gmp-dev flex bison + +# Install minimum necessary dependencies +RUN apk add --no-cache $PACKAGES +# Install pdc +RUN wget https://crypto.stanford.edu/pbc/files/pbc-0.5.14.tar.gz \ + && tar -xf pbc-0.5.14.tar.gz \ + && cd pbc-0.5.14/ \ + && ./configure \ + && make \ + && make install \ + && ldconfig / \ + && cd .. && rm -rf pbc-0.5.14/ pbc-0.5.14.tar.gz # Set working directory for the build WORKDIR /go/src/github.com/stratosnet/stratos-chain # Add source files COPY . . +RUN make install -RUN go version - -# Install minimum necessary dependencies, build Cosmos SDK, remove packages -RUN apk add --no-cache $PACKAGES && \ - make install # Final image FROM alpine:edge -ENV STRATOS /stchaind +ENV WORK_DIR /stchaind +ENV RUN_AS_USER stratos # Install ca-certificates -RUN apk add --update ca-certificates +RUN apk add --update ca-certificates gmp-dev -RUN addgroup stratos && \ - adduser -S -G stratos stratos -h "$STRATOS" +ARG uid=2048 +ARG gid=2048 -USER stratos +RUN addgroup --gid $gid "$RUN_AS_USER" && \ + adduser -S -G "$RUN_AS_USER" --uid $uid "$RUN_AS_USER" -h "$WORK_DIR" -WORKDIR $STRATOS +WORKDIR $WORK_DIR # Copy over binaries from the build-env COPY --from=build-env /go/bin/stchaind /usr/bin/stchaind +COPY --from=build-env /usr/local/lib/libpbc.so.1.0.0 /usr/local/lib/libpbc.so.1.0.0 + +RUN cd /usr/local/lib && { ln -s -f libpbc.so.1.0.0 libpbc.so.1 || { rm -f libpbc.so.1 && ln -s libpbc.so.1.0.0 libpbc.so.1; }; } \ + && cd /usr/local/lib && { ln -s -f libpbc.so.1.0.0 libpbc.so || { rm -f libpbc.so && ln -s libpbc.so.1.0.0 libpbc.so; }; } -# Run stchaind by default, omit entrypoint to ease using container with stchaincli -CMD ["stchaind"] +COPY entrypoint.sh /usr/bin/entrypoint.sh +RUN chmod +x /usr/bin/entrypoint.sh +ENTRYPOINT ["/usr/bin/entrypoint.sh"] diff --git a/Makefile b/Makefile index 29cce82c..f5bff671 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,13 @@ +#!/usr/bin/make -f + BUILDDIR ?= $(CURDIR)/build APP_VER := v0.9.1 -COMMIT := $(GIT_COMMIT_HASH) -TEST_DOCKER_REPO=stratos-chain-e2e +COMMIT := $(shell git log -1 --format='%H') +TEST_DOCKER_REPO=stratos-chain ifeq ($(COMMIT),) - VERSION := $(APP_VER) + VERSION := $(APP_VER) else VERSION := $(APP_VER)-$(COMMIT) endif @@ -66,4 +68,9 @@ build-docker-e2e: @docker tag ${TEST_DOCKER_REPO}:$(shell git rev-parse --short HEAD) ${TEST_DOCKER_REPO}:$(shell git rev-parse --abbrev-ref HEAD | sed 's#/#_#g') @docker tag ${TEST_DOCKER_REPO}:$(shell git rev-parse --short HEAD) ${TEST_DOCKER_REPO}:latest +build-docker: + @docker build -f Dockerfile -t ${TEST_DOCKER_REPO}:$(shell git rev-parse --short HEAD) --build-arg uid=$(shell id -u) --build-arg gid=$(shell id -g) . + @docker tag ${TEST_DOCKER_REPO}:$(shell git rev-parse --short HEAD) ${TEST_DOCKER_REPO}:$(shell git rev-parse --abbrev-ref HEAD | sed 's#/#_#g') + @docker tag ${TEST_DOCKER_REPO}:$(shell git rev-parse --short HEAD) ${TEST_DOCKER_REPO}:latest + .PHONY: build-linux build-mac build clean diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 00000000..8798ab1b --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,3 @@ +#!/bin/sh +chown -R $RUN_AS_USER $WORK_DIR +su -s /bin/sh - $RUN_AS_USER -c "$@" From 38e4fde261b7f9107a71e39e7843123ea76afee2 Mon Sep 17 00:00:00 2001 From: weichen-qsnetwork Date: Tue, 25 Apr 2023 09:22:27 -0400 Subject: [PATCH 2/2] Download genesis and config in entrypoint.sh --- Dockerfile | 7 ++++++- entrypoint.sh | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index ba6af0fe..e697fe19 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.19-alpine AS build-env +FROM golang:1.18-alpine AS build-env # Set up dependencies ENV PACKAGES curl make git libc-dev bash gcc linux-headers eudev-dev python3 \ @@ -33,12 +33,16 @@ ENV RUN_AS_USER stratos # Install ca-certificates RUN apk add --update ca-certificates gmp-dev +ARG chain_id +ARG moniker ARG uid=2048 ARG gid=2048 RUN addgroup --gid $gid "$RUN_AS_USER" && \ adduser -S -G "$RUN_AS_USER" --uid $uid "$RUN_AS_USER" -h "$WORK_DIR" +ENV CHAIN_ID=${chain_id:-DEFAULT} +ENV MONIKER=${moniker:-stratos-node} WORKDIR $WORK_DIR # Copy over binaries from the build-env @@ -51,3 +55,4 @@ RUN cd /usr/local/lib && { ln -s -f libpbc.so.1.0.0 libpbc.so.1 || { rm -f libpb COPY entrypoint.sh /usr/bin/entrypoint.sh RUN chmod +x /usr/bin/entrypoint.sh ENTRYPOINT ["/usr/bin/entrypoint.sh"] +CMD ["stchaind start"] diff --git a/entrypoint.sh b/entrypoint.sh index 8798ab1b..6bcf264e 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,3 +1,28 @@ #!/bin/sh chown -R $RUN_AS_USER $WORK_DIR + +if [ ! -d "$WORK_DIR/.stchaind" ] +then + echo "[entrypoint] Init stratos node..." + su -s /bin/sh - $RUN_AS_USER -c "stchaind init stratos-node" + + if [ "$CHAIN_ID" == "DEFAULT" ] + then + genesis_file_URL=https://raw.githubusercontent.com/stratosnet/stratos-chain-testnet/main/genesis.json + config_file_URL=https://raw.githubusercontent.com/stratosnet/stratos-chain-testnet/main/config.toml + else + genesis_file_URL=https://raw.githubusercontent.com/stratosnet/stratos-chain-testnet/main/$CHAIN_ID/genesis.json + config_file_URL=https://raw.githubusercontent.com/stratosnet/stratos-chain-testnet/main/$CHAIN_ID/config.toml + fi + + echo "[entrypoint] Download genesis.json from $genesis_file_URL" + su -s /bin/sh - $RUN_AS_USER -c "wget $genesis_file_URL -O $WORK_DIR/.stchaind/config/genesis.json" + + echo "[entrypoint] Download config.toml from $config_file_URL" + su -s /bin/sh - $RUN_AS_USER -c "wget $config_file_URL -O $WORK_DIR/.stchaind/config/config.toml" + + echo "[entrypoint] Set the node moniker to '$MONIKER'" + su -s /bin/sh - $RUN_AS_USER -c "sed -i 's/moniker = \".*\"/moniker = \"'$MONIKER'\"/g' $WORK_DIR/.stchaind/config/config.toml" +fi + su -s /bin/sh - $RUN_AS_USER -c "$@"