基于docker搭建以太坊私链
环境准备
- centos 7
- docker
- 以太坊环境:ethereum/client-go
VMware安装centos 7
按照网上教程安装即可,比较简单
centos 7 上搭建docker
1、使用root用户登录centos,更新yum安装包
yum -y update
2、安装docker所需的软件包
yum install -y yum-utils device-mapper-persistent-data lvm2
3、设置yum软件源,这里选择阿里云
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
4、查看仓库中docker的所有版本
yum list docker-ce --showduplicates | sort -r
5、安装docker
yum -y install docker-ce
6、启动docker
systemctl start docker
7、将docker设置为开机自启动
systemctl enable docker
8、查看docker是否安装成功
docker version
安装以太坊
1、在docker中下载以太坊镜像
docker pull ethereum/client-go
2、创建目录blockChain
mkdir blockChain
3、进入blockChain目录,创建ethereum目录,创建start-ethereum.sh文件,用于启动以太坊镜像
cd blockChain
mkdir ethereum
vim start-ethereum.sh
其中start-ethereum.sh文件的内容如下
docker stop ethereum-node
docker rm ethereum-node
docker run -d --name ethereum-node -v /root/blockChain/ethereum:/root \
-p 8545:8545 -p 30300:30300 \
ethereum/client-go
docker exec -it ethereum-node /bin/sh
4、测试运行./start-ethereum.sh,首先需要给文件赋予执行权限:chmod u+x start-ethereum.sh
5、exit退出后进入ethereum目录,创建genesis.json文件,并赋予文件执行权限
cd ethereum
vim genesis.json
其中 genesis.json文件的内容如下
genesis.json文件内容
{
"config": {
"chainId": 123,
"homesteadBlock": 0,
"eip150Block": 0,
"eip150Hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"eip155Block": 0,
"eip158Block": 0,
"byzantiumBlock": 0,
"constantinopleBlock": 0,
"petersburgBlock": 0,
"istanbulBlock": 0,
"ethash": {}
},
"nonce": "0x0",
"timestamp": "0x5ddf8f3e",
"extraData": "0x0000000000000000000000000000000000000000000000000000000000000000",
"gasLimit": "0x47b760",
"difficulty": "0x000002",
"mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x0000000000000000000000000000000000000000",
"alloc": { },
"number": "0x00",
"gasUsed": "0x00",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000"
}
6、运行./start-ethereum.sh,之后切换到/root目录下
7、初始化节点,写入创世区块,运行下面的命令
geth --datadir data0 init genesis.json
8、启动节点
geth --identity "TestNode" --datadir data0 --http --http.port "8545" --http.addr "0.0.0.0" -port "30301" --networkid "123" --http.api "eth,net,web3,personal" --nodiscover console --allow-insecure-unlock
使用命令 -rpc报错时,是因为现在不用rpc,改用http,将原来rpc改为http即可,如rpcport -> http.port
centos 7 安装geth并搭建以太坊
1、更新系统
yum -y update
2、更新下载必要的软件
yum install gcc-c++ -y
yum install git -y
3、安装golang并配置环境变量
wget https://studygolang.com/dl/golang/go1.14.1.linux-amd64.tar.gz
tar -C /usr/local -zxzf go1.14.1.linux-amd64.tar.gz
# 添加到环境变量
echo "export GOROOT=/usr/local/go" >> /etc/profile
echo "export PATH=/usr/local/go/bin:$PATH" >> /etc/profile
source /etc/profile
# 查看golang版本
go version
4、安装geth
wget https://gethstore.blob.core.windows.net/builds/geth-alltools-linux-amd64-1.9.10-58cf5686.tar.gz
tar zxvf geth-alltools-linux-amd64-1.9.10-58cf5686.tar.gz
mv geth-alltools-linux-amd64-1.9.10-58cf5686 ~/geth-home
export PATH=$HOME/geth-home:$PATH
echo `export PATH=$HOME/geth-home:$PATH` >> ~/.bashrc
# 添加到环境变量
export PATH=$HOME/geth-home:$PATH >> /etc/profile
echo `export PATH=$HOME/geth-home:$PATH` >> /etc/profile
# 查看geth版本信息
geth version
5、选择目录创建一个genesis.json文件
该文件的内容与上面文件内容相同
6、初始化节点,写入创世区块,并在与genesis.json文件相同的目录下运行下面的命令
geth --datadir data0 init genesis.json
7、启动节点
geth --identity "TestNode" --datadir data0 --rpc --rpcport "8545" --rpcaddr "0.0.0.0" -port "30301" --networkid "123" --rpcapi "eth,net,web3,personal" --nodiscover console --allow-insecure-unlock