Here's the steps I used to create a Docker image that recreates a consistent test environment:
- Create a Docker file (see listing below) with empty geth data directory and files as noted in the Docker file. Also set up directory structure as noted in Docker file below.
docker build -t ethereum/client-go:test . to build an image
- start a container, login by overriding entrypoint with bash:
docker run -i -t --entrypoint "/bin/bash" ethereum/client-go:test
- Start geth using the script noted in the dockerfile
- create required accounts for your testing environment (e.g. via console)
- edit genesis.json so that those accounts are pre-allocated with ether
- verify via geth console that the accounts are there, with ether allocated
- stop geth.
docker cp to copy off the entire contents of
/root/.ethereum to your host to the directory noted in the Dockerfile below.
- stop and remove your container.
docker build to create a new image. The new image now has the saved off starting state that can be reproduced as needed for your test environment.
- I suggest using a volume for /root/.ethereum so that you don't lose data if geth restarts for some reason. See Docker documentation
# our own custom bult geth that mines really fast
COPY geth /usr/bin/geth
# script that invokes with all those
# command line options
COPY rungeth.docker /usr/bin/rungeth
# these two files and directory of geth state belong together and must be
# kept in sync if changes are ever made
# Note we are taking advantage of Docker's copy-on-mount feature
COPY geth.password /root/geth.password
COPY genesis.json /root/genesis.json
COPY ethereum /root/.ethereum
# use non-standard ports so don't accidently connect to real servers
# XXX Docker inheritance doesn't override, it extends the port list...
I will attempt at some point to put an image on dockerhub so you don't have to do the above save run it.
Here's the script I use to build a docker container:
docker build -t ethereum/client-go:test .
Here's the script I use to run a docker container instance.
docker run --name geth -d -p 8110:8110 --entrypoint "usr/bin/rungeth" --volumes-from gethdb ethereum/client-go:test
and the script that runs geth in the container:
/usr/bin/geth --datadir /root/.ethereum --password /root/geth.password --unlock "0 1 2 3 4 5" --port 30310 --rpc --rpcaddr "0.0.0.0" --rpcport 8110 --networkid 4567890 --dev --lightkdf --nodiscover --maxpeers 0 --vmdebug --verbosity 6 --pprof --genesis /root/genesis.json --gpomin "50" --gpomax "50" --pprofport 6110 --mine --minerthreads 1 2> /tmp/geth.log