feat: update dockerfile with a few enhancement (#998)

* feat: refactor dockerfile to add entrypoint script
This commit is contained in:
Jason Yi 2022-07-20 12:07:00 +08:00 committed by GitHub
parent 659f670b9b
commit 37b7ac7b71
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 179 additions and 9 deletions

@ -12,17 +12,44 @@ 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_USER=bsc
ARG BSC_USER_UID=1000
ARG BSC_USER_GID=1000
ENV BSC_HOME=/bsc
ENV HOME=${BSC_HOME}
ENV DATA_DIR=/data
ENV PACKAGES ca-certificates~=20211220 jq~=1.6 \
bash~=5.1.16-r2 bind-tools~=9.16.29-r0 tini~=0.19.0 \
grep~=3.7 curl==7.83.1-r2 sed~=4.8-r0
RUN apk add --no-cache $PACKAGES \
&& 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
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 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}
# rpc ws graphql
EXPOSE 8545 8546 8547 30303 30303/udp
ENTRYPOINT ["geth"]
# Add some metadata labels to help programatic image consumption
ARG COMMIT=""
ARG VERSION=""
ARG BUILDNUM=""
LABEL commit="$COMMIT" version="$VERSION" buildnum="$BUILDNUM"
ENTRYPOINT ["/sbin/tini", "--", "./docker-entrypoint.sh"]

@ -3,6 +3,7 @@
# 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
@ -59,3 +60,6 @@ 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:latest -f Dockerfile .

54
docker-compose.yaml Normal file

@ -0,0 +1,54 @@
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
environment:
- NETWORK=mainnet
restart: unless-stopped
ports:
- 30303:30303
- 30311:30311
- 8545:8545
- 8546:8546
- 8575:8575
- 8576:8576
healthcheck:
test: |
[[ "$NETWORK" == "testnet" ]] && PORT=8575 || PORT=8545;
netstat -tunlp | grep $PORT > /dev/null; if [ 0 != $$? ]; then exit 1; else exit 0; fi;
interval: 5s
retries: 5
start_period: 10s
timeout: 3s
volumes:
- data:/data
- config:/bsc/config
volumes:
config:
driver: local
driver_opts:
type: 'none'
o: 'bind'
device: '/tmp/bsc/config/mainnet'
data:
driver: local
driver_opts:
type: 'none'
o: 'bind'
device: '/tmp/bsc/data/mainnet'

15
docker-entrypoint.sh Normal file

@ -0,0 +1,15 @@
#!/bin/bash
set -e
BSC_CONFIG=${BSC_HOME}/config/config.toml
BSC_GENESIS=${BSC_HOME}/config/genesis.json
# Init genesis state if geth not exist
DATA_DIR=$(cat ${BSC_CONFIG} | grep -A1 '\[Node\]' | grep -oP '\"\K.*?(?=\")')
GETH_DIR=${DATA_DIR}/geth
if [ ! -d "$GETH_DIR" ]; then
geth --datadir ${DATA_DIR} init ${BSC_GENESIS}
fi
exec "geth" "--config" ${BSC_CONFIG} "$@"

70
docs/docker/README.md Normal file

@ -0,0 +1,70 @@
## 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 .
```
Before start the docker, get a copy of the config.toml & genesis.json from the release: https://github.com/bnb-chain/bsc/releases, and make necessary modification. `config.toml` & `genesis.json` should be mounted into `/bsc/config` inside the container. Assume `config.toml` & `genesis.json` are under `./config` in your current working directory, you can start your docker container with the following command:
```bash
docker run -v $(pwd)/config:/bsc/config --rm --name bsc -it bnb-chain/bsc
```
You can also use `ETHEREUM OPTIONS` to overwrite settings in the configuration file
```bash
docker run -v $(pwd)/config:/bsc/config --rm --name bsc -it 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 bnb-chain/bsc /bin/bash
```
We also provide a `docker-compose` file for local testing
To use the container in kubernetes, you can use a configmap or secret to mount the `config.toml` & `genesis.json` into the container
```bash
containers:
- name: bsc
image: bnb-chain/bsc
ports:
- name: p2p
containerPort: 30311
- name: rpc
containerPort: 8545
- name: ws
containerPort: 8546
volumeMounts:
- name: bsc-config
mountPath: /bsc/config
volumes:
- name: bsc-config
configMap:
name: cm-bsc-config
```
Your configmap `bsc-config` should look like this:
```
apiVersion: v1
kind: ConfigMap
metadata:
name: cm-bsc-config
data:
config.toml: |
...
genesis.json: |
...
```