Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: refactor dockerfile to add entrypoint script #993

Closed
wants to merge 13 commits into from
51 changes: 42 additions & 9 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,50 @@ ADD . /go-ethereum
RUN cd /go-ethereum && go run build/ci.go install ./cmd/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/

EXPOSE 8545 8546 8547 30303 30303/udp
ENTRYPOINT ["geth"]
COPY docker-entrypoint.sh ./

# Add some metadata labels to help programatic image consumption
ARG COMMIT=""
ARG VERSION=""
ARG BUILDNUM=""
RUN curl -LO https://github.com/bnb-chain/bsc/releases/download/${BSC_VERSION}/mainnet.zip \
Copy link
Contributor

@j75689 j75689 Jul 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the URL can be replaced by .github/release.env.
the mainnet and testnet config of the new release is obtained from here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

release.env still pointing to v1.1.8 and that is why I have a ARG to build on.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually every time a new version is released the additional files come from here, so it will be the same if releas.env doesn't update

The reason I want you to use release.env is that developers will most likely forget to update the Dockerfile when they release a new version

Copy link
Contributor Author

@jasonyic jasonyic Jul 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when each release, no need to update the BSC_VERSION in Dockerfile, the build process should pass in --build-arg for the new version to be used, similar to the new target docker in makefile.

BSC_VERSION=v1.1.10 make docker

Additionally, this won't cause confusion for developer between the regular release in the github repo and configuration version being used in Dockerfile.

&& 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}

# mainnet rpc ws p2p
EXPOSE 8545 8546 30311

# testnet rpc ws p2p
EXPOSE 8575 8576 30303

LABEL commit="$COMMIT" version="$VERSION" buildnum="$BUILDNUM"
ENTRYPOINT ["/sbin/tini", "--", "./docker-entrypoint.sh"]
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
# don't need to bother with make.

.PHONY: geth android ios geth-cross evm all test truffle-test clean
.PHONY: docker

GOBIN = ./build/bin
GO ?= latest
GORUN = env GO111MODULE=on go run

BSC_VERSION ?= v1.1.11

geth:
$(GORUN) build/ci.go install ./cmd/geth
@echo "Done building."
Expand Down Expand Up @@ -59,3 +62,8 @@ devtools:
env GOBIN= go install ./cmd/abigen
@type "solc" 2> /dev/null || echo 'Please install solc'
@type "protoc" 2> /dev/null || echo 'Please install protoc'

docker:
docker build --pull -t bnb-chain/bsc:$(BSC_VERSION) -t bnb-chain/bsc:latest \
--build-arg BSC_VERSION=$(BSC_VERSION) \
-f Dockerfile .
43 changes: 43 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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", "--http.port", "8545", "--http.vhosts", "*", "--verbosity", "3"]
environment:
- BSC_NETWORK=mainnet
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
volumes:
- data:/data

volumes:
data:
driver: local
driver_opts:
type: 'none'
o: 'bind'
device: '/tmp/bsc/data'
25 changes: 25 additions & 0 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/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

# 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

# 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} "$@"
52 changes: 52 additions & 0 deletions docs/docker/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
## 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
```

To build a specific release version, e.g. v1.1.9, with the following commands:
```bash
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:
```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
```