1. 程式人生 > >docker容器安裝ethereum

docker容器安裝ethereum

方法一:利用現有開源

有一個現成的ethereum/client-go:test工具映象。

1 下載工具

2 構建映象

執行以下命令,構建映象,執行的過程中需要特別注意命令最後是有一個“.”的,否則會出現錯誤:

[[email protected] pgeth]# cd /root/pgeth/docker-geth-dev-master
[[email protected] 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
 ---> 55
b07edb552c
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 啟動映象

執行以下命令啟動映象:

[[email protected] 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

[[email protected] 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
[[email protected] 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容器的指定位置。特別需要留意的是埠號,可根據自己的需要進行修改。其他內容請自行閱讀分析。

[[email protected] 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

進去[[email protected] 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 [email protected] 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 [email protected] 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

以上為本人啟動時調整之後的啟動命令。