如何使用 Docker-compose `geth init` 并开始 geth 挖矿?

How to `geth init` and start geth mining with Docker-compose?

我想用 Docker 创建私有以太坊网络。我已经准备好创世文件,所以我需要 geth init genesis.json 然后像 geth --mine ... 一样开始挖矿。我可以用脚本来完成(像这里:https://github.com/vertigobr/ethereum/blob/master/runminer.sh#L5 and https://github.com/vertigobr/ethereum/blob/master/runnode.sh#L23):

if [ ! -d $DATA_ROOT/keystore ]; then
    echo "$DATA_ROOT/keystore not found, running 'geth init'..."
    docker run --rm \
        -v $DATA_ROOT:/root/.ethereum \
        -v $(pwd)/genesis.json:/opt/genesis.json \
        $IMGNAME init /opt/genesis.json
    echo "...done!"
fi
echo "Running new container $CONTAINER_NAME..."
docker run $DETACH_FLAG --name $CONTAINER_NAME \
    --network ethereum \
    -v $DATA_ROOT:/root/.ethereum \
    -v $DATA_HASH:/root/.ethash \
    -v $(pwd)/genesis.json:/opt/genesis.json \
    $RPC_PORTMAP \
    $IMGNAME --bootnodes=$BOOTNODE_URL $RPC_ARG --cache=512 --verbosity=4 --maxpeers=3 ${@:2}

因为它似乎是两步过程,我如何使用 Docker-compose 来完成?

如果我为挖矿服务覆盖command:,我应该写什么?如果我只写 geth init,那么它不会开始挖矿。如果我尝试加入并写 command: init genesis.json --mine ... 会很痛:

version: "3"

services:
  eth_miner:
    image: ethereum/client-go:v1.7.3
    ports:
      - "8545:8545"
    volumes:
      - ${DATA_ROOT}:/root/.ethereum
      - ${GENESIS_FILE}:/opt/genesis.json
    command: init /opt/genesis.json --rpc --rpcaddr=0.0.0.0 --rpcapi=db,eth,net,web3,personal --rpccorsdomain "*" --nodiscover --cache=512 --verbosity=4 --mine --minerthreads=3 --networkid 15 --etherbase="${ETHERBASE}" --gasprice=${GASPRICE}

日志:

Attaching to 7adbb760_eth_miner_1
eth_miner_1  | Incorrect Usage: flag provided but not defined: -rpc
eth_miner_1  | 
eth_miner_1  | init [command options] [arguments...]
eth_miner_1  | 
eth_miner_1  | The init command initializes a new genesis block and definition for the network.
eth_miner_1  | This is a destructive action and changes the network in which you will be
eth_miner_1  | participating.
eth_miner_1  | 
eth_miner_1  | It expects the genesis file as argument.
eth_miner_1  | 
eth_miner_1  | ETHEREUM OPTIONS:
eth_miner_1  |   --datadir "/root/.ethereum"  Data directory for the databases and keystore
eth_miner_1  | 
eth_miner_1  | DEPRECATED OPTIONS:
eth_miner_1  |   --light  Enable light client mode
eth_miner_1  | 
eth_miner_1  | flag provided but not defined: -rpc
7adbb760_eth_miner_1 exited with code 1

您最好的选择是创建一个 shell 脚本来执行初始化,然后 运行 geth,应该是这样的:

#!/bin/bash
if [ ! -d /root/.ethereum/keystore ]; then
    echo "/root/.ethereum/keystore not found, running 'geth init'..."
    geth init /opt/genesis.json
    echo "...done!"
fi

geth "$@"

和docker-compose.yaml:

version: "3"

services:
  eth_miner:
    image: ethereum/client-go:v1.7.3
    ports:
      - "8545:8545"
    volumes:
      - ${DATA_ROOT}:/root/.ethereum
      - ${GENESIS_FILE}:/opt/genesis.json
      - ./init-script.sh:/root/init-script.sh
    entrypoint: /root/init-script.sh
    command: --bootnodes=$BOOTNODE_URL $RPC_ARG --cache=512 --verbosity=4 --maxpeers=3 ${@:2}