From 28815f759a571ed2c6f31069a75e9479b017965f Mon Sep 17 00:00:00 2001 From: Jason Yi Date: Thu, 14 Jul 2022 20:15:48 +0800 Subject: [PATCH] feat: refactor dockerfile to add entrypoint script Signed-off-by: Jason Yi --- Dockerfile | 42 +++++++++++++++++++++++++++++++++++++++--- Makefile | 6 ++++++ docker-compose.yaml | 23 +++++++++++++++++++++++ docker-entrypoint.sh | 18 ++++++++++++++++++ 4 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 docker-compose.yaml create mode 100644 docker-entrypoint.sh diff --git a/Dockerfile b/Dockerfile index 437fb2737a..8e219e3c6c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,10 +7,46 @@ ADD . /go-ethereum RUN cd /go-ethereum && make geth # Pull Geth into a second stage deploy alpine container -FROM alpine:latest +FROM alpine:3.16.0 + +ARG BSC_VERSION=v1.1.11 +ARG BSC_USER=bsc +ARG BSC_USER_UID=1000 +ARG BSC_USER_GID=1000 + +ENV BSC_HOME=/bsc +ENV HOME=${BSC_HOME} +ENV DATA_DIR=/data + +RUN apk add --no-cache ca-certificates~=20211220 jq~=1.6 bash~=5.1.16-r2 bind-tools~=9.16.29-r0 tini~=0.19.0 curl==7.83.1-r2 sed~=4.8-r0 \ + && rm -rf /var/cache/apk/* \ + && addgroup -g ${BSC_USER_GID} ${BSC_USER} \ + && adduser -u ${BSC_USER_UID} -G ${BSC_USER} --shell /sbin/nologin --no-create-home -D ${BSC_USER} \ + && addgroup ${BSC_USER} tty \ + && sed -i -e "s/bin\/sh/bin\/bash/" /etc/passwd + +RUN echo "[ ! -z \"\$TERM\" -a -r /etc/motd ] && cat /etc/motd" >> /etc/bash/bashrc \ + && echo "Version: ${BSC_VERSION}" > /etc/motd + +WORKDIR ${BSC_HOME} -RUN apk add --no-cache ca-certificates curl jq tini COPY --from=builder /go-ethereum/build/bin/geth /usr/local/bin/ +COPY docker-entrypoint.sh ./ + +RUN curl -LO https://github.com/bnb-chain/bsc/releases/download/${BSC_VERSION}/mainnet.zip \ + && unzip mainnet.zip -d mainnet && rm mainnet.zip \ + && curl -LO https://github.com/bnb-chain/bsc/releases/download/${BSC_VERSION}/testnet.zip \ + && unzip testnet.zip -d testnet && rm testnet.zip + +RUN chmod +x docker-entrypoint.sh \ + && mkdir -p ${DATA_DIR} \ + && chown -R ${BSC_USER_UID}:${BSC_USER_GID} ${BSC_HOME} ${DATA_DIR} + +VOLUME ${DATA_DIR} + +USER ${BSC_USER_UID}:${BSC_USER_GID} + EXPOSE 8545 8546 8547 30303 30303/udp -ENTRYPOINT ["geth"] \ No newline at end of file + +ENTRYPOINT ["/sbin/tini", "--", "./docker-entrypoint.sh"] \ No newline at end of file diff --git a/Makefile b/Makefile index 03f92e6f43..0544448d09 100644 --- a/Makefile +++ b/Makefile @@ -7,11 +7,14 @@ .PHONY: geth-linux-arm geth-linux-arm-5 geth-linux-arm-6 geth-linux-arm-7 geth-linux-arm64 .PHONY: geth-darwin geth-darwin-386 geth-darwin-amd64 .PHONY: geth-windows geth-windows-386 geth-windows-amd64 +.PHONY: docker GOBIN = ./build/bin GO ?= latest GORUN = env GO111MODULE=on go run +BSC_VERSION ?= 1.1.11 + geth: $(GORUN) build/ci.go install ./cmd/geth @echo "Done building." @@ -155,3 +158,6 @@ geth-windows-amd64: $(GORUN) build/ci.go xgo -- --go=$(GO) --targets=windows/amd64 -v ./cmd/geth @echo "Windows amd64 cross compilation done:" @ls -ld $(GOBIN)/geth-windows-* | grep amd64 + +docker: + docker build --pull -t bnb-chain/bsc:$(BSC_VERSION) -t bnb-chain/bsc:latest -f Dockerfile . \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000000..edcb43e8e4 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,23 @@ +version: '3.6' +services: + bsc: + build: + context: . + dockerfile: Dockerfile + command: ["--http.addr", "0.0.0.0", "--ws.addr", "0.0.0.0"] + environment: + - LOG_LEVEL=3 + - DIFF_SYNC=true + - CACHE=4096 + restart: unless-stopped + ports: + - 30311:30311 + - 6060:6060 + - 8545:8545 + - 8546:8546 + healthcheck: + test: netstat -tunlp | grep 8545 > /dev/null; if [ 0 != $$? ]; then exit 1; else exit 0; fi; + interval: 5s + retries: 5 + start_period: 10s + timeout: 3s \ No newline at end of file diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh new file mode 100644 index 0000000000..429b4f9fb3 --- /dev/null +++ b/docker-entrypoint.sh @@ -0,0 +1,18 @@ +#!/bin/bash +set -e + +NETWORK=${BSC_NETWORK:-testnet} +DATA_DIR=${BSC_DATA_DIR:-/data} +BSC_CONFIG=${BSC_HOME}/config.toml + +# Create symlink to network config.toml & genesis.json +ln -sf ./${NETWORK}/config.toml config.toml +ln -sf ./${NETWORK}/genesis.json genesis.json + +# Default log to console +sed -i '/Node\.LogConfig/,/^$/d' ./${NETWORK}/config.toml + +# Init genesis state: +geth --datadir ${DATA_DIR} init genesis.json + +exec "/usr/local/bin/geth" "--config" ${BSC_CONFIG} "--datadir" ${DATA_DIR} "$@" \ No newline at end of file