3

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
Ignacio Millán
  • 7,480
  • 1
  • 25
  • 28
4ntoine
  • 19,816
  • 21
  • 96
  • 220

1 Answers1

7

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}
Ignacio Millán
  • 7,480
  • 1
  • 25
  • 28
  • The option "command" in docker-compose.yml just pass "/root/init-script.sh --bootnodes= ..." to docker-entrypoint.sh as an argument. I'm afraid entry point can be `geth`. If it is then it will not work (unless we can override entry point cmd too). – 4ntoine May 30 '18 at 07:54
  • 1
    My mistake, just edited the answer. I forgot to modify the entrypoint. – Ignacio Millán May 30 '18 at 07:57
  • thanks for editing, i've checked entry point is really `geth`, so we need to override it too. – 4ntoine May 30 '18 at 07:58
  • i believe in general case running script like this can solve the problem. in this particular case i'm getting "permission denied" for both `/root/init-script.sh` and `/opt/init-script.sh` – 4ntoine May 30 '18 at 08:09
  • 1
    Run chmod +x init-script.sh – Ignacio Millán May 30 '18 at 08:14
  • some required changes: `entrypoint: sh /root/init-script.sh` and/or `#!/bin/sh` in script – 4ntoine May 30 '18 at 08:42