下载基础etherum镜像
docker pull etereum/client-go
创建docker网络(可选)
docker推荐创建自有网路,再将需要互联的容器配置到相同的网络中
创建一个docker网络
- 子网
- IP 段 172.18.0.0
- 掩码 255.255.0.0
- IP范围 172.18.0.1~172.18.255.254
- IP 广播 172.18.255.255
docker network create -d bridge --subnet=172.18.0.0/16 ethnet
可查看网络
docker network ls
cb789b79a3d5 bridge bridge local
23551173f607 ethereum-docker_default bridge local
9f2ce26b0890 ethnet bridge local
运行一个docker ethereum容器
# 指定挂载路径 本地磁盘目录:容器目录
# --network 指定网络
# --ip 指定固定ip
docker run -it --rm --network ethnet --ip 172.18.0.10 --name my-ethereum-test -v /e/docker/ethereum/workspace:/workspace --entrypoint /bin/sh ethereum/client-go
查看是否加入的docker网络
docker network inspect ethnet
"Containers": {
"6f45d3e95a0f2eefdccd150c28722fb8fc0aed33d2dafc08b7f0c267cfdf480a": {
"Name": "miner",
"EndpointID": "9d02040810255f3935959b08a187696ec8ba9c947bf36ab6351321fe78ec8aa7",
"MacAddress": "02:42:ac:12:00:28",
"IPv4Address": "172.18.0.40/16",
"IPv6Address": ""
},
"a4e3513f841902d7f21f164ef70084bdb47e4baac01432cb321ff7c684998493": {
"Name": "fervent_ganguly",
"EndpointID": "81e15ebb1b369d37e8d535dc48604c24ad4e735cf0c9f41f6ac6fb9a5733310e",
"MacAddress": "02:42:ac:12:00:0a",
"IPv4Address": "172.18.0.10/16",
"IPv6Address": ""
}
}
workspace 目录
创建容器内workspace目录结构
dapp\
dapp\miner\
dapp\data\
data\genesis.json
然后创建几个初始化账户
cd /workspace/dapp/miner
geth -datadir ./data account new
输入两次密码,获得地址,将地址记录下来, 例如
/workspace # cd miner/
/workspace/miner # geth -datadir ./data account new
INFO [01-29|01:49:22.906] Maximum peer count ETH=50 LES=0 total=50
INFO [01-29|01:49:22.906] Smartcard socket not found, disabling err="stat /run/pcscd/pcscd.comm: no such file or directory"
Your new account is locked with a password. Please give a password. Do not forget this password.
Password:
Repeat password:
Your new key was generated
Public address of the key: 0xDeA9384818E2A846F1Bd423Af41184436CdE194B
Path of the secret key file: data/keystore/UTC--2022-01-29T01-49-28.323954900Z--dea9384818e2a846f1bd423af41184436cde194b
- You can share your public address with anyone. Others need it to interact with you.
- You must NEVER share the secret key with anyone! The key controls access to your funds!
- You must BACKUP your key file! Without the key, it's impossible to access account funds!
- You must REMEMBER your password! Without the password, it's impossible to decrypt the key!
以下为(多次执行上面的geth -datadir ./data account new
)创建的地址
0xDeA9384818E2A846F1Bd423Af41184436CdE194B
0xcf9E1cFF0cBdBF1D3d685054c60Cd3E80dc1feCc
创建初始区块
编辑 genesis.json 文件
{
"config": {
"chainId": 866,
"homesteadBlock": 0,
"eip150Block": 0,
"eip150Hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"eip155Block": 0,
"eip158Block": 0,
"byzantiumBlock": 0,
"constantinopleBlock": 0,
"petersburgBlock": 0,
"istanbulBlock": 0,
"ethash": {}
},
"nonce": "0x0",
"timestamp": "0x61f4a1d9",
"extraData": "0x0000000000000000000000000000000000000000000000000000000000000000",
"gasLimit": "0x47b760",
"difficulty": "0x00002",
"mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x0000000000000000000000000000000000000000",
"alloc": {
"0xDeA9384818E2A846F1Bd423Af41184436CdE194B": {"balance": "100000000000000000000"},
"0xcf9E1cFF0cBdBF1D3d685054c60Cd3E80dc1feCc": {"balance": "5000000000000000000"},
"number": "0x0",
"gasUsed": "0x0",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000"
}
}
- 同一私链的节点必须使用同一份配置文件
- chainId:私链网络的标识,任意数字
- alloc:配置初始化的账户以及资产,以wei为单位
挖矿
上面只是配置好了一个以太坊私有网络,并没有真正创建网络,以太坊是一个分布式网络,有了矿工,才有网络。
创建 矿工主节点
- 一个容器,并且持久化
- 自动读取genesis.json 并初始化以太坊网络
- 能链接其他节点
- 能接收各种rpc调用,并能部署合约
- 可以配置挖矿账户,可以一键挖矿
编辑 entrypoint 启动执行脚本
/workspace/dapp/init.sh
geth -datadir ~/data/ init /workspace/dapp/genesis.json
if [[ $# -lt 1 ]]; then
exec "/bin/sh"
else
exec /bin/sh -c "$@"
fi
赋予可执行权限 chmod 755 init.sh
自动运行挖矿脚本
/workspace/dapp/mine.sh
#!/bin/sh
# 将workspace下的内容复制到主目录下
cp -r /workspace/dapp/miner/data/keystore/* ~/data/keystore/
# 启动以太坊节点。并设置挖矿收益账户
geth -datadir ~/data/ --networkid 866 --http --http.addr "172.18.0.10" --http.api admin,eth,miner,web3,personal,net,txpool --mine --miner.threads=1 --miner.etherbase "0xDeA9384818E2A846F1Bd423Af41184436CdE194B"
赋予可执行权限 chmod 755 mine.sh
- 第一行 cp 将生成的账户私钥文件copy到容器的home目录下,因为 workspace 为宿主目录挂载的,并非linux文件系统,直接将datadir指定到改目录会导致geth报错
- 第二行以命令启动以太坊节点,设置网络为 866 与genesis.json中的chainId保持一致即可。http.addr指定节点的ip地址, http.api指定对外的接口,--miner 指定挖矿收益接收账户
- 注意 现在有很多文档还在使用rpc, 在新本的镜像中rpc已经不在使用,使用http替代
启动容器
docker run -itd --name=my-ether-miner --network ethnet --ip 172.18.0.10 -p 8545:8545 --hostname node -v /e/docker/ethereum/workspace2:/workspace --entrypoint /workspace/dapp/init.sh ethereum/client-go /workspace/dapp/mine.sh
参考
基于docker的以太坊集群的私有链开发环境