From 28815f759a571ed2c6f31069a75e9479b017965f Mon Sep 17 00:00:00 2001 From: Jason Yi Date: Thu, 14 Jul 2022 20:15:48 +0800 Subject: [PATCH 01/12] 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 From c65f44b328480e3eab37fe2cf8baca876c20223f Mon Sep 17 00:00:00 2001 From: Jason Yi Date: Fri, 15 Jul 2022 11:40:45 +0800 Subject: [PATCH 02/12] fix: bsc version in makefile Signed-off-by: Jason Yi --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 0544448d09..bcee39d4d1 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,7 @@ GOBIN = ./build/bin GO ?= latest GORUN = env GO111MODULE=on go run -BSC_VERSION ?= 1.1.11 +BSC_VERSION ?= v1.1.11 geth: $(GORUN) build/ci.go install ./cmd/geth From 59a316ed488fbbdd6f64463fbb9cb14b7fe42bcb Mon Sep 17 00:00:00 2001 From: Jason Yi Date: Fri, 15 Jul 2022 16:27:20 +0800 Subject: [PATCH 03/12] fix: ignore init if data exist Signed-off-by: Jason Yi --- docker-compose.yaml | 32 ++++++++++++++++++++++++++------ docker-entrypoint.sh | 13 ++++++++++--- 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/docker-compose.yaml b/docker-compose.yaml index edcb43e8e4..905011315d 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -1,14 +1,24 @@ version: '3.6' services: + init: + image: alpine + command: + - /bin/sh + - -c + - | + chown -R 1000:1000 /data + + echo "done grany data directory permission" + volumes: + - data:/data + bsc: build: context: . - dockerfile: Dockerfile - command: ["--http.addr", "0.0.0.0", "--ws.addr", "0.0.0.0"] + dockerfile: Dockerfile.debug + command: ["--http.addr", "0.0.0.0", "--http.port", "8545", "--http.vhosts", "*", "--verbosity", "3"] environment: - - LOG_LEVEL=3 - - DIFF_SYNC=true - - CACHE=4096 + - BSC_NETWORK=mainnet restart: unless-stopped ports: - 30311:30311 @@ -20,4 +30,14 @@ services: interval: 5s retries: 5 start_period: 10s - timeout: 3s \ No newline at end of file + timeout: 3s + volumes: + - data:/data + +volumes: + data: + driver: local + driver_opts: + type: 'none' + o: 'bind' + device: '/tmp/bsc/data' \ No newline at end of file diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh index 429b4f9fb3..404ab7010a 100644 --- a/docker-entrypoint.sh +++ b/docker-entrypoint.sh @@ -12,7 +12,14 @@ 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 +# TO-DO: remove after the default value in config.toml updated to empty +# Issue: https://github.com/bnb-chain/bsc/issues/994 +sed -i 's/^HTTPHost.*/HTTPHost = "0.0.0.0"/' ./${NETWORK}/config.toml -exec "/usr/local/bin/geth" "--config" ${BSC_CONFIG} "--datadir" ${DATA_DIR} "$@" \ No newline at end of file +# Init genesis state if geth not exist +GETH_DIR=${DATA_DIR}/geth +if [ ! -d "$GETH_DIR" ]; then + geth --datadir ${DATA_DIR} init genesis.json +fi + +exec "geth" "--config" ${BSC_CONFIG} "--datadir" ${DATA_DIR} "$@" \ No newline at end of file From 2fd462052a622a65adfbb4ec5d7dba5c214601ef Mon Sep 17 00:00:00 2001 From: Jason Yi Date: Fri, 15 Jul 2022 16:39:59 +0800 Subject: [PATCH 04/12] fix: add build arg to makefile Signed-off-by: Jason Yi --- Makefile | 4 +++- docker-compose.yaml | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index bcee39d4d1..bdc7a52be2 100644 --- a/Makefile +++ b/Makefile @@ -160,4 +160,6 @@ geth-windows-amd64: @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 + docker build --pull -t bnb-chain/bsc:$(BSC_VERSION) -t bnb-chain/bsc:latest \ + --build-arg BSC_VERSION=$(BSC_VERSION) \ + -f Dockerfile . \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml index 905011315d..2905450936 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -15,7 +15,7 @@ services: bsc: build: context: . - dockerfile: Dockerfile.debug + dockerfile: Dockerfile command: ["--http.addr", "0.0.0.0", "--http.port", "8545", "--http.vhosts", "*", "--verbosity", "3"] environment: - BSC_NETWORK=mainnet From 2ee4d6aca7a8a38acc6987ef823734d230cce31e Mon Sep 17 00:00:00 2001 From: Jason Yi Date: Fri, 15 Jul 2022 16:41:03 +0800 Subject: [PATCH 05/12] fix: lint Signed-off-by: Jason Yi --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index bdc7a52be2..89b7e6ea6b 100644 --- a/Makefile +++ b/Makefile @@ -162,4 +162,4 @@ geth-windows-amd64: docker: docker build --pull -t bnb-chain/bsc:$(BSC_VERSION) -t bnb-chain/bsc:latest \ --build-arg BSC_VERSION=$(BSC_VERSION) \ - -f Dockerfile . \ No newline at end of file + -f Dockerfile . From fd46540b864a38370e466e89f96b0c2c0bcc1559 Mon Sep 17 00:00:00 2001 From: Jason Yi Date: Fri, 15 Jul 2022 16:44:10 +0800 Subject: [PATCH 06/12] fix: lint Signed-off-by: Jason Yi --- Dockerfile | 8 ++++---- docker-compose.yaml | 2 +- docker-entrypoint.sh | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Dockerfile b/Dockerfile index 8e219e3c6c..c28b4a310e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -32,11 +32,11 @@ WORKDIR ${BSC_HOME} COPY --from=builder /go-ethereum/build/bin/geth /usr/local/bin/ -COPY docker-entrypoint.sh ./ +COPY docker-entrypoint.sh .github/release.env ./ -RUN curl -LO https://github.com/bnb-chain/bsc/releases/download/${BSC_VERSION}/mainnet.zip \ +RUN curl -LO $(cat release.env | cut -d'=' -f2 | head -n 1) \ && unzip mainnet.zip -d mainnet && rm mainnet.zip \ - && curl -LO https://github.com/bnb-chain/bsc/releases/download/${BSC_VERSION}/testnet.zip \ + && curl -LO $(cat release.env | cut -d'=' -f2 | sed -n 2p) \ && unzip testnet.zip -d testnet && rm testnet.zip RUN chmod +x docker-entrypoint.sh \ @@ -49,4 +49,4 @@ USER ${BSC_USER_UID}:${BSC_USER_GID} EXPOSE 8545 8546 8547 30303 30303/udp -ENTRYPOINT ["/sbin/tini", "--", "./docker-entrypoint.sh"] \ No newline at end of file +ENTRYPOINT ["/sbin/tini", "--", "./docker-entrypoint.sh"] diff --git a/docker-compose.yaml b/docker-compose.yaml index 2905450936..8ed5d733a8 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -40,4 +40,4 @@ volumes: driver_opts: type: 'none' o: 'bind' - device: '/tmp/bsc/data' \ No newline at end of file + device: '/tmp/bsc/data' diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh index 404ab7010a..738a03bd6c 100644 --- a/docker-entrypoint.sh +++ b/docker-entrypoint.sh @@ -22,4 +22,4 @@ if [ ! -d "$GETH_DIR" ]; then geth --datadir ${DATA_DIR} init genesis.json fi -exec "geth" "--config" ${BSC_CONFIG} "--datadir" ${DATA_DIR} "$@" \ No newline at end of file +exec "geth" "--config" ${BSC_CONFIG} "--datadir" ${DATA_DIR} "$@" From 85621a9218535d59e44cfe4aba209d42669fc3f5 Mon Sep 17 00:00:00 2001 From: Jason Yi Date: Fri, 15 Jul 2022 17:36:24 +0800 Subject: [PATCH 07/12] feat: add readme for docker Signed-off-by: Jason Yi --- docs/docker/README.md | 47 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 docs/docker/README.md diff --git a/docs/docker/README.md b/docs/docker/README.md new file mode 100644 index 0000000000..8620c493e7 --- /dev/null +++ b/docs/docker/README.md @@ -0,0 +1,47 @@ +## Docker Image + +Included in this repo is a Dockerfile that you can launch BSC node for trying it out. Docker images are available on `ghcr.io/bnb-chain/bsc`. + +You can build the docker image with the following commands: +```bash +make docker +``` + +If your build machine has an ARM-based chip, like Apple silicon (M1), the image is built for `linux/arm64` by default. To build for `x86_64`, apply the --platform arg: +```bash +docker build --platform linux/amd64 -t bnb-chain/bsc -f Dockerfile . +``` + +You can start your docker container with the following command: +```bash +docker run --rm --name bsc -it bnb-chain/bsc +``` +This will start a full node in `testnet` by default. + +To start a full node in `mainnet`, you can pass in env variable `BSC_NETWORK`: +```bash +docker run --rm --name bsc -it -e BSC_NETWORK=mainnet bnb-chain/bsc +``` + +Another env variable `BSC_DATA_DIR` is also avaiable to overwrite the default DataDir. You can also use `--datadir` to overwrite DataDir too. +To overwrite default `DataDir` with env `BSC_DATA_DIR`: +```bash +docker run --rm --name bsc -it -e BSC_DATA_DIR=/data2 bnb-chain/bsc +``` + +To overwrite default `DataDir` with `ETHEREUM OPTIONS` `--datadir`: +```bash +docker run --rm --name bsc -it bnb-chain/bsc --datadir /data2 +``` +`--datadir` will overrides `BSC_DATA_DIR` + + +To start a node with geth command with `ETHEREUM OPTIONS`: +```bash +docker run --rm --name bsc -it -e BSC_NETWORK=mainnet bnb-chain/bsc --http.addr 0.0.0.0 --http.port 8545 --http.vhosts '*' --verbosity 3 +``` + +If you need to open another shell, just do: +```bash +docker exec -it bsc /bin/bash +``` From e9968f89455a577719961df511a40367bb745d1b Mon Sep 17 00:00:00 2001 From: Jason Yi Date: Fri, 15 Jul 2022 17:51:05 +0800 Subject: [PATCH 08/12] fix: update readme for docker Signed-off-by: Jason Yi --- docs/docker/README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs/docker/README.md b/docs/docker/README.md index 8620c493e7..687966ac20 100644 --- a/docs/docker/README.md +++ b/docs/docker/README.md @@ -7,6 +7,11 @@ You can build the docker image with the following commands: make docker ``` +To build a specific release versiob, e.g. v1.1.9, with the following commands: +```bash +BSC_VERSION=v1.1.10 make docker +``` + If your build machine has an ARM-based chip, like Apple silicon (M1), the image is built for `linux/arm64` by default. To build for `x86_64`, apply the --platform arg: ```bash docker build --platform linux/amd64 -t bnb-chain/bsc -f Dockerfile . From 6f5291511848e5253f9e7d80457c12ebffc5e7d6 Mon Sep 17 00:00:00 2001 From: Jason Yi Date: Fri, 15 Jul 2022 17:51:30 +0800 Subject: [PATCH 09/12] fix: update readme for docker Signed-off-by: Jason Yi --- docs/docker/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/docker/README.md b/docs/docker/README.md index 687966ac20..6e601e6605 100644 --- a/docs/docker/README.md +++ b/docs/docker/README.md @@ -7,9 +7,9 @@ You can build the docker image with the following commands: make docker ``` -To build a specific release versiob, e.g. v1.1.9, with the following commands: +To build a specific release version, e.g. v1.1.9, with the following commands: ```bash -BSC_VERSION=v1.1.10 make docker +BSC_VERSION=v1.1.9 make docker ``` If your build machine has an ARM-based chip, like Apple silicon (M1), the image is built for `linux/arm64` by default. To build for `x86_64`, apply the --platform arg: From 5a47be5ca1eafb560ee4acde610e4a094149f1a0 Mon Sep 17 00:00:00 2001 From: Jason Yi Date: Sat, 16 Jul 2022 15:14:08 +0800 Subject: [PATCH 10/12] feat: use release package --- Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 1086cf91a5..a5a7af9188 100644 --- a/Dockerfile +++ b/Dockerfile @@ -39,9 +39,9 @@ COPY --from=builder /go-ethereum/build/bin/geth /usr/local/bin/ COPY docker-entrypoint.sh .github/release.env ./ -RUN curl -LO $(cat release.env | cut -d'=' -f2 | head -n 1) \ +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 $(cat release.env | cut -d'=' -f2 | sed -n 2p) \ + && 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 \ From 8c40eacfc841d6bcc4f7d2d53b81b33f711bfb8c Mon Sep 17 00:00:00 2001 From: Jason Yi Date: Sat, 16 Jul 2022 15:16:06 +0800 Subject: [PATCH 11/12] fix: rm unused file --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index a5a7af9188..67f084d0e7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -37,7 +37,7 @@ WORKDIR ${BSC_HOME} COPY --from=builder /go-ethereum/build/bin/geth /usr/local/bin/ -COPY docker-entrypoint.sh .github/release.env ./ +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 \ From 4e82a8cafe36f7f29a820e1f7a4fca4928fcdedd Mon Sep 17 00:00:00 2001 From: Jason Yi Date: Sat, 16 Jul 2022 22:05:48 +0800 Subject: [PATCH 12/12] fix: update node port --- Dockerfile | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 67f084d0e7..a5a0c77fa2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -52,6 +52,10 @@ VOLUME ${DATA_DIR} USER ${BSC_USER_UID}:${BSC_USER_GID} -EXPOSE 8545 8546 8547 30303 30303/udp +# mainnet rpc ws p2p +EXPOSE 8545 8546 30311 + +# testnet rpc ws p2p +EXPOSE 8575 8576 30303 ENTRYPOINT ["/sbin/tini", "--", "./docker-entrypoint.sh"]