方法一:利用现有开源
有一个现成的ethereum/client-go:test工具镜像。
1 下载工具
地址:https://github.com/pragmaticcoders/docker-geth-dev
将项目通过zip包下载下来,解压到将要执行docker命令的一个目录下。目录的结构与github上面上的目录结构一样
2 构建镜像
执行以下命令,构建镜像,执行的过程中需要特别注意命令最后是有一个“.”的,否则会出现错误:
[root@i-nxhzfrjz pgeth]# cd /root/pgeth/docker-geth-dev-master
[root@i-nxhzfrjz docker-geth-dev-master]# docker build -t ethereum/client-go:test .
Sending build context to Docker daemon 231.9 kB
Step 1 : FROM ethereum/client-go
---> 55b07edb552c
Step 2 : COPY rungeth.docker /usr/bin/rungeth
---> 3e485e2eff45
Removing intermediate container 510f2c24fde5
Step 3 : COPY geth.password /root/geth.password
---> 010367885703
Removing intermediate container e2b7fcd2cff8
Step 4 : COPY genesis.json /root/genesis.json
---> f6dac8264b7b
Removing intermediate container f0024ad206f2
Step 5 : COPY ethereum /root/.ethereum
---> b8caf6171332
Removing intermediate container 47970853367a
Step 6 : ENTRYPOINT /usr/bin/rungeth
---> Running in 978112e673ba
---> be7a5b4d2f48
Removing intermediate container 978112e673ba
Step 7 : EXPOSE 8110
---> Running in 3dcd44b81c24
---> 8af6494a1ada
Removing intermediate container 3dcd44b81c24
Step 8 : EXPOSE 30310
---> Running in 8cb2080288cb
---> 82ff692d638a
Removing intermediate container 8cb2080288cb
Step 9 : EXPOSE 6110
---> Running in 7fa0c0b4ef0d
---> 9f4c2abd84aa
Removing intermediate container 7fa0c0b4ef0d
Successfully built 9f4c2abd84aa
3 启动镜像
执行以下命令启动镜像:
[root@i-nxhzfrjz docker-geth-dev-master]# docker run --name geth -d -p 8110:8110 ethereum/client-go:test
/usr/bin/docker-current: Error response from daemon: Conflict. The name "/geth" is already in use by container 92f3e8b2352998069662ee551f912b30c1ea69a96e645311b2ecd553288d3654. You have to remove (or rename) that container to be able to reuse that name..
提示我已经创建过名字为geth的镜像,如果要再创建一个 ethereum/client-go:test镜像的话需要换一个名字。
查看一下我所有的docker
[root@i-nxhzfrjz docker-geth-dev-master]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
92f3e8b23529 ethereum/client-go:test "/usr/bin/rungeth" 2 hours ago Created geth
7d3858a37e90 ethereum/client-go "geth console" 3 hours ago Exited (0) 3 hours ago compassionate_leavitt
f8c6a9193d8f ethereum/client-go "geth" 4 hours ago Exited (0) 3 hours ago naughty_bell
[root@i-nxhzfrjz docker-geth-dev-master]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
此处需注意自己所使用的端口。
至此一个dev环境搭建完成,其中的三个账户已经被初始化了一定的余额。
我们可以查看配置文件即创始块的信息
[root@i-nxhzfrjz docker-geth-dev-master]# vi genesis.json
{
"nonce": "0x00006d6f7264656e",
"difficulty": "0x20000",
"mixhash": "0x00000000000000000000000000000000000000647572616c65787365646c6578",
"coinbase": "0xde1e758511a7c67e7db93d1c23c1060a21db4615",
"timestamp": "0x00",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData": "0x",
"gasLimit": "0x2FEFD8",
"alloc": {
"de1e758511a7c67e7db93d1c23c1060a21db4615": {
"balance": "1000"
},
"27dc8de9e9a1cb673543bd5fce89e83af09e228f": {
"balance": "1100"
},
"d64a66c28a6ae5150af5e7c34696502793b91ae7": {
"balance": "900"
}
}
}
此文件为配置创世块文件。对三个账户进行了初始化金额,分别为1000,1100和900。其中初始化为1000里的为矿工奖励接收账户,随着挖矿会不断增加,你看到时候可能已经不是这个余额了。
下面再简单看一下Dockerfile文件的内容,其实很简单,就是将写好的配置文件cp到docker容器的指定位置。特别需要留意的是端口号,可根据自己的需要进行修改。其他内容请自行阅读分析。
[root@i-nxhzfrjz docker-geth-dev-master]# vi Dockerfile
FROM ethereum/client-go
# # 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
ENTRYPOINT ["/usr/bin/rungeth"]
# use non-standard ports so don't accidently connect to real servers
# XXX Docker inheritance doesn't override, it extends the port list...
EXPOSE 8110
EXPOSE 30310
EXPOSE 6110
进去[root@i-nxhzfrjz docker-geth-dev-master]# vi rungeth.docker 发现就是进geth交互平台的命令
#!/bin/bash
if [ -f /usr/bin/geth ]; then
/usr/bin/geth --datadir /root/.ethereum --password /root/geth.password --unlock "0" --port 30310 --rpc --rpcaddr "0.0.0.0" -rpcapi "eth,web3" --rpcport 8110 --networkid 4567890 --dev --lightkdf --nodiscover --maxpeers 0 --verbosity 6 --pprof --pprofport 6110 --mine --minerthreads 1 $@ 2> /tmp/geth.log
else
./geth --datadir /root/.ethereum --password /root/geth.password --unlock "0" --port 30310 --rpc --rpcaddr "0.0.0.0" -rpcapi "eth,web3" --rpcport 8110 --networkid 4567890 --dev --lightkdf --nodiscover --maxpeers 0 --verbosity 6 --pprof --pprofport 6110 $@ 2> /tmp/geth.log
fi
方法二
此方法为本人自行探索得出,经验证可以使用。
此方法非常简单,只需在执行正常的启动容器命令后面添加“–dev”参数即可。不过此方法不会像上面方法那样创建一批初始化账户,不过可以自行挖矿,进行交易,轻易获得不同金额的账户。
docker run -td -m 512M --memory-swap -1 -p 8545:8545 -p 30303:30303 -v /mnt/docker/dev:/root/.ethereum --name gethDev ethereum/client-go --rpcapi "db,eth,net,web3,personal,admin,miner" --rpc --rpcaddr "0.0.0.0" --cache=512 --dev
以上为本人启动时调整之后的启动命令。