Search code examples
dockerdocker-composeethereumgeth

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


I want to create private ethereum network with Docker. I've prepared genesis file so i need to geth init genesis.json and then start mining like geth --mine .... I can do it with scripts (like here: 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}

Since it seems to be 2-step process how can i do it with Docker-compose?

If i override command: for mining service, what should i write? If i write just geth init, then it will not start mining. If i try to join and write command: init genesis.json --mine ... it hurts:

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}

log:

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

Solution

  • Your best option is creating a shell script which do the initialization and then run geth, should be something like this:

    #!/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 "$@"
    

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