你须要知道区块链的概念及实现一个无需信任的电子货币的运做原理。能够先看看长版,若是你以为太长,能够看看短版。git
一个 docker 环境, 还有…… 没了github
注:Mac下docker集成了docker-compose,其余系统须要安装docker-composeweb
$ git clone --branch v1.0 --depth=1 https://github.com/gengxiankun/dockerfiles.git &&\
docker-compose -f dockerfiles/ethereum/ethereum-stack-compose.yml up -d
复制代码
因为须要分别构建(ganache/geth/truffle)三个容器,请耐心等待。 执行成功后,查看以太坊容器:docker
$ docker-compose -f dockerfiles/ethereum/ethereum-stack-compose.yml ps
Name Command State Ports
-------------------------------------------------------------------------
ganache docker-entrypoint.sh Up 0.0.0.0:7454->7454/tcp, 9454/tcp
geth /usr/sbin/init Up
truffle /usr/sbin/init Up
复制代码
能够看到,truffle、ganache及geth容器均已运行,证实环境搭建成功了!网络
以上,咱们能够看出构建了三个容器来分别运行ganache、geth及truffle。经过dockerfiles/ethereum/ethereum-stack-compose.yml
来看一下它们是如何相互运做的:架构
$ cat dockerfiles/ethereum/ethereum-stack-compose.yml
version: '2'
services:
ganache:
container_name: ganache
build: ./Ganache/
expose:
- "7454"
ports:
- "7454:7454"
environment:
- NETWORKID=6
- PORT=7454
restart: always
truffle:
container_name: truffle
build: ./Truffle/
volumes:
- ~/data/ethereum/:/data/
working_dir: /data
links:
- ganache:ganache
restart: always
geth:
container_name: geth
build: ./Geth/
restart: always
links:
- ganache:ganache
复制代码
能够看到,Ganache开放出了network_id
为6,端口
是7454的以太坊网络接口 triffle及geth经过docker内部链路links
链接ganache服务,直接访问host为ganache
无序指定IP,好比truffle的网络配置及geth链接:app
#truffle.js
module.exports = {
networks: {
development: {
host: "ganache",
port: 7454,
network_id: "6" // Match any network id
}
}
};
#geth链接本地ganache网络
$ docker exec -it geth geth attach http://ganache:7454
Welcome to the Geth JavaScript console!
instance: EthereumJS TestRPC/v2.1.0/ethereum-js
coinbase: 0xd08734d6ca10a2acb464d26ed033df08dd93acc3
at block: 0 (Sun, 15 Apr 2018 03:54:12 UTC)
modules: eth:1.0 evm:1.0 net:1.0 personal:1.0 rpc:1.0 web3:1.0
>
复制代码
truffle容器的工做目录/data
与本地的~/data/ethereum
目录挂载,执行truffle初始化,能够看到本地同步了容器的代码:框架
$ docker exec -it truffle truffle init
$ cd ~/data/ethereum
$ tree
.
├── contracts
│ └── Migrations.sol
├── migrations
│ └── 1_initial_migration.js
├── test
├── truffle-config.js
└── truffle.js
3 directories, 4 files
复制代码
如此,即可方便的在本地使用IDE开发智能合约了!tcp