在Docker中运行EOS(MAC版)

在Docker中运行EOS(MAC版)

在Docker中也可以简单快速的构建EOS.IO.笔者在Mac平台下参考官方文档躺了一次河。记录如下:

安装依赖

  • Docker 版本 17.05或者更高

    testdeMBP:Docker test$ docker -v

    Docker version 18.04.0-ce, build 3d479c0

  • docker-compose version >= 1.10.0

    testdeMBP:Docker test$ docker-compose -v

    docker-compose version 1.21.2, build unknown

Docker需求

  • Docker需要至少7GB内存。(Docker -> Preferences -> Advanced -> Memory -> 7GB or above)
  • 如果下面的build失败了,确保内存的配置满足要求然后再试一次。

编译eos镜像

git clone https://github.com/EOSIO/eos.git --recursive  --depth 1
cd eos/Docker
docker build . -t eosio/eos`

这个过程需要等待一会

上面的命令默认构建master分支的最近提交。如果需要编译运行特定分支/tag,可以使用一个编译参数。例如,你想基于标签v1.0.4生成docker镜像:

docker build -t eosio/eos:v1.0.4 --build-arg branch=v1.0.4 .

默认eosio.system中的符号被设置为SYS。你可以在编译的时候通过symbol参数来覆盖它。

 docker build -t eosio/eos --build-arg symbol=<symbol> .

只启动nodeos docker 容器

docker run --name nodeos -p 8888:8888 -p 9876:9876 -t eosio/eos nodeosd.sh -e arg1 arg2

遇到如下错误:

zexudeMBP:Docker zexu$ docker-machine env default
Error checking TLS connection: Host is not running

解决方法

zexudeMBP:Docker zexu$ docker-machine restart default
Restarting "default"...
Starting "default"...
(default) Check network to re-create if needed...
(default) Waiting for an IP...
Machine "default" was started.
Waiting for SSH to be available...
Detecting the provisioner...
Waiting for SSH to be available...
Detecting the provisioner...
Restarted machines may have new IP addresses. You may need to re-run the `docker-machine env` command. zexudeMBP:Docker zexu$ docker-machine env
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.100:2376"
export DOCKER_CERT_PATH="/Users/zexu/.docker/machine/machines/default"
export DOCKER_MACHINE_NAME="default"
# Run this command to configure your shell:
# eval $(docker-machine env)
zexudeMBP:Docker zexu$ eval $(docker-machine env)
zexudeMBP:Docker zexu$

获取链信息

首先获取docker中http service的IP地址:

zexudeMBP:Docker zexu$ docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
default * virtualbox Running tcp://192.168.99.100:2376 v18.03.1-ce
zexudeMBP:Docker zexu$ docker-machine ip default
192.168.99.100

调用HTTP接口:

curl http://192.168.99.100:8888/v1/chain/get_info

得到如下信息:

{"server_version":"c9b7a247","chain_id":"cf057bbfb72640471fd910bcb67639c22df9f92470936cddc1ade0e2f2e7dc4f","head_block_num":2866,"last_irreversible_block_num":2865,"last_irreversible_block_id":"00000b315494325c32f9e930957f776e3819d4eacdc9d28f8fc6df7149318891","head_block_id":"00000b32b4ea4d06db5c78006c34be993deb952de142e3a186df5ab8312d6116","head_block_time":"2018-06-20T08:06:49.000","head_block_producer":"eosio","virtual_block_cpu_limit":3506516,"virtual_block_net_limit":18419703,"block_cpu_limit":199900,"block_net_limit":1048576}

启动nodeos和keosd容器

docker volume create --name=nodeos-data-volume
docker volume create --name=keosd-data-volume
docker-compose up -d

执行最后一句时会等待很长一段时间,总共16步。。

Creating network "docker_default" with the default driver
Building builder
Step 1/16 : FROM ubuntu:18.04
6b98dfc16071: Pull complete
4001a1209541: Pull complete
6319fc68c576: Pull complete
b24603670dc3: Pull complete
97f170c87c6f: Pull complete
。。。。。

遇到如下错误:

CMake Error at src/bsoncxx/CMakeLists.txt:86 (find_package):
Could not find a configuration file for package "libbson-static-1.0" that
is compatible with requested version "1.10.0". The following configuration files were considered but not accepted: /usr/local/lib/cmake/libbson-static-1.0/libbson-static-1.0-config.cmake, version: 1.9.3

解决方法如下

    short fix is to change the file builder\Dockerfile line 63 to the following:
RUN git clone --depth 1 -b releases/v3.2 https://github.com/mongodb/mongo-cxx-driver \ the latest stable of mongo-cxx-driver require to update the mongo-driver to be v1.10 and above.

执行完成之后,会启动两个services,nodeosd和keosd。nodeos service将8888和9876端口暴露给主机。keosd不会暴露任何端口,因为它只会被cleos访问,而cleos是运行在keosd容器内部的,详情见下节。

 zexudeMBP:Docker zexu$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d78d7b5c831a eosio/eos "/opt/eosio/bin/keos…" 28 minutes ago Up 29 minutes docker_keosd_1
2478e62b8825 eosio/eos "/opt/eosio/bin/node…" 28 minutes ago Up 29 minutes 0.0.0.0:8888->8888/tcp, 0.0.0.0:9876->9876/tcp docker_nodeosd_1

执行cleos命令

你可以通过一个bash alias来运行cleos 命令。

alias cleos='docker-compose exec keosd /opt/eosio/bin/cleos -u http://nodeosd:8888 --wallet-url http://localhost:8900'
cleos get info
cleos get account inita

上传样品交换合约:

cleos set contract exchange contracts/exchange/

如果接下来你不需要keosd服务了,你可以使用下面的命令来停止服务:

docker-compose stop keosd

开发/编译个性化合约

因为eosio/eos镜像没有包含合约开发所需要的依赖包(这是by design的,为了镜像容量小的目的),所以需要使用eosio/eos-dev镜像。这个镜像包含了用eosiocpp编译合约所需要的二进制文件和依赖包。

可以使用Docker Hub上的已有镜像或者进入dev文件夹来手动编译镜像。

cd dev
docker build -t eosio/eos-dev .

修改默认配置

可以使用docker compose覆盖文件的方式来修改默认配置。例如:创建要替换的config2.ini文件和一个docker-compose.override.yml,yml文件的内容如下:

version: "2"

services:
nodeos:
volumes:
- nodeos-data-volume:/opt/eosio/bin/data-dir
- ./config2.ini:/opt/eosio/bin/data-dir/config.ini

然后重启你的docker容器:

docker-compose down
docker-compose up

清理data-dir

使用下面的命令清除用docker-compose命令创建的volume:

docker volume rm nodeos-data-volume
docker volume rm keosd-data-volume

Docker hub

Docker Hub镜像可以从Docker Hub上获取到。创建一个新的docker-compose.yaml文件,内容如下:

version: "3"    

services:
nodeosd:
image: eosio/eos:latest
command: /opt/eosio/bin/nodeosd.sh --data-dir /opt/eosio/bin/data-dir -e
hostname: nodeosd
ports:
- 8888:8888
- 9876:9876
expose:
- "8888"
volumes:
- nodeos-data-volume:/opt/eosio/bin/data-dir keosd:
image: eosio/eos:latest
command: /opt/eosio/bin/keosd --wallet-dir /opt/eosio/bin/data-dir --http-server-address=127.0.0.1:8900
hostname: keosd
links:
- nodeosd
volumes:
- keosd-data-volume:/opt/eosio/bin/data-dir volumes:
nodeos-data-volume:
keosd-data-volume:

注意:默认的版本是最新的,可以改成自己需要的

run docker pull eosio/eos:latest
run docker-compose up

EOSIO 1.0 测试网络

可以使用docker镜像可以很容易的在本地创建一个EOSIO 1.0测试网络。运行下面的命令即可:

注意:如果你想使用mogo db插件,你必须在data-dir/config.ini中打开它。

# pull images
docker pull eosio/eos:v1.0.4 # create volume
docker volume create --name=nodeos-data-volume
docker volume create --name=keosd-data-volume
# start containers
docker-compose -f docker-compose-eosio1.0.yaml up -d
# get chain info
curl http://127.0.0.1:8888/v1/chain/get_info
# get logs
docker-compose logs -f nodeosd
# stop containers
docker-compose -f docker-compose-eosio1.0.yaml down

区块数据默认存储在 --data-dir下面,钱包文件默认存储在 --wallet-dir下面,可以根据需要修改他们。

关于MongoDB 插件

现在mogodb插件在config.ini中是默认关闭的,需要你手动修改这个文件或者在docker-compose文件中将config.ini挂载到/opt/eosio/bin/data-dir/config.ini

上一篇:2293: Distribution Center 中南多校


下一篇:HDOJ.1010 Tempter of the Bone (DFS)