深度学习
除了手动进行本地编译外,还可以采用容器(Docker)镜像的方式快速获取和运行Fabric网络,省去本地编译等待的时间。
9.3.1 安装Docker服务
Docker支持Linux常见的发行版(如Redhat/Centos/Ubuntu)和MacOS等,推荐使用1.12或者更新的版本。
Linux操作系统中可以通过如下命令来快速安装Docker:
$ curl -fsSL https://get.docker.com/ | sh
安装成功后,修改Docker服务配置。Ubuntu 16.04中默认采用了systemd管理启动服务,Docker配置文件在/etc/systemd/system/docker.service.d/override.conf下:
DOCKER_OPTS="$DOCKER_OPTS -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock
--api-cors-header='*'"
修改后,需要通过如下命令重启Docker服务:
$ sudo systemctl daemon-reload
$ sudo systemctl restart docker.service
对于使用upstart管理启动服务的操作系统(如旧版本的Ubuntu、Debian),则可以采用如下命令重启Docker服务:
$ sudo service docker restart
MacOS下可以通过访问http://docs.docker.com/docker-for-mac/install 下载Docker for Mac安装包进行安装。
9.3.2 安装docker-compose
docker-compose是一个Python程序,可以很方便地管理由多个Docker实例组成的分布式服务。
首先,安装python-pip软件包:
$ sudo aptitude install python-pip
安装docker-compose(推荐为1.8.0及以上版本):
$ sudo pip install docker-compose>=1.8.0
9.3.3 获取Docker镜像
Docker镜像可以从源码编译生成,或通过从DockerHub仓库下载获取。
目前,Fabric项目相关的镜像有十几个,其主要功能参见表9-1。
表9-1 与Fabric项目相关的镜像及其主要功能
这些镜像之间的相互依赖关系如图9-1所示。
图9-1 镜像之间的依赖关系
1.从源码生成镜像
可以通过如下命令在本地快速生成包括hyperledger/fabric-baseos、hyperledger /fabric-peer、hyperledger/fabric-orderer、hyperledger/fabric-ccenv、 hyperledger/fabric-javaenv等在内的多个Docker镜像:
$ cd $GOPATH/src/github.com/hyperledger/fabric
$ make docker
注意从源码直接生成的镜像,除了lastest标签外,还会额外带有所编译版本快照信息的标签,例如x86_64-1.0.0-snapshot123456。
2.从Dockerhub获取镜像
除了从源码编译外,还可以直接从Dockerhub来拉取相关的镜像,命令格式为docker pull<IMAGE_NAME:TAG>。
例如,从社区仓库直接获取fabric-peer、fabric-orderer、fabric-ca、fabric-tools等镜像的1.0.0版本可以使用如下命令:
$ ARCH=x86_64
$ BASEIMAGE_RELEASE=0.3.1
$ BASE_VERSION=1.0.0
$ PROJECT_VERSION=1.0.0
$ IMG_TAG=1.0.0
# 拉取镜像
$ docker pull hyperledger/fabric-peer:$ARCH-$IMG_TAG \
&& docker pull hyperledger/fabric-orderer:$ARCH-$IMG_TAG \
&& docker pull hyperledger/fabric-ca:$ARCH-$IMG_TAG \
&& docker pull hyperledger/fabric-tool:$ARCH-SING-TAG\
&& docker pull hyperledger/fabric-ccenv:$ARCH-$PROJECT_VERSION \
&& docker pull hyperledger/fabric-baseimage:$ARCH-$BASEIMAGE_RELEASE \
&& docker pull hyperledger/fabric-baseos:$ARCH-$BASEIMAGE_RELEASE
# 添加fabric-peer、fabric-orderer、fabric-ca和fabric-tools为最新版本标签
$ docker tag hyperledger/fabric-peer:$ARCH-$IMG_TAG hyperledger/fabric-peer \
&& docker tag hyperledger/fabric-orderer:$ARCH-$IMG_TAG hyperledger/ fabric-orderer \
&& docker tag hyperledger/fabric-ca:$ARCH-$IMG_TAG hyperledger/fabric-ca \
&& docker tag hyperledger/fabric-tools:$ARCH-$IMG_TAG hyperledger/fabric-tools
此外,还可以从第三方仓库获取镜像,拉取后可以添加默认的镜像标签别名。
例如,笔者仓库中构建了基于golang:1.8基础镜像的相关fabric镜像,可以通过如下命令获取:
$ ARCH=x86_64
$ BASEIMAGE_RELEASE=0.3.1
$ BASE_VERSION=1.0.0
$ PROJECT_VERSION=1.0.0
$ IMG_TAG=1.0.0
# 拉取镜像
$ docker pull yeasy/hyperledger-fabric-base:$IMG_TAG \
&& docker pull yeasy/hyperledger-fabric-peer:$IMG_TAG \
&& docker pull yeasy/hyperledger-fabric-orderer:$IMG_TAG \
&& docker pull yeasy/hyperledger-fabric-ca:$IMG_TAG
# 添加最新版本的标签
$ docker tag yeasy/hyperledger-fabric-peer:$IMG_TAG hyperledger/fabric-peer \
&& docker tag yeasy/hyperledger-fabric-orderer:$IMG_TAG hyperledger/fabric-orderer \
&& docker tag yeasy/hyperledger-fabric-ca:$IMG_TAG hyperledger/fabric-ca \
&& docker tag yeasy/hyperledger-fabric-peer:$IMG_TAG hyperledger/fabric-tools \
&& docker tag yeasy/hyperledger-fabric-base:$IMG_TAG hyperledger/fabric-ccenv:
$ARCH-$PROJECT_VERSION \
&& docker tag yeasy/hyperledger-fabric-base:$IMG_TAG hyperledger/fabric-baseos:
$ARCH-$BASE_VERSION \
&& docker tag yeasy/hyperledger-fabric-base:$IMG_TAG hyperledger/fabric-baseimage:
$ARCH-$BASEIMAGE_RELEASE
注意,其中BASEIMAGE_RELEASE是基础镜像fabric-baseimage的版本号;BASE_VERSION是Fabric项目的主版本号;PROJECT_VERSION是具体版本号。这些版本号需要与所使用的Fabric代码和配置保持一致。
9.3.4 镜像Dockerfile
读者也可以通过编写Dockefile的方式来生成相关镜像。
Dockerfile中的指令与本地编译过程十分类似,这里给出笔者编写的fabric-baseimage镜像、fabric-peer镜像、fabric-orderer镜像等关键镜像的Dockerfile,供读者参考使用。
1.fabric-baseimage镜像
fabric-baseimage镜像的参考Dockerfile如下,基于golang:1.8镜像生成,可以作为 Go链码容器的基础镜像。该镜像中包含了Fabric相关的代码,并安装了一些有用的工具,包括chaintools、gotools、 configtxgen、configtxlator和cryptogen等。
该Dockerfile也可以从http://github.com/yeasy/docker-hyperledger-fabric-base 下载获取:
# Dockerfile for Hyperledger fabric base image.
# If you need a peer node to run, please see the yeasy/hyperledger-peer image.
# Workdir is set to $GOPATH/src/github.com/hyperledger/fabric
# Data is stored under /var/hyperledger/db and /var/hyperledger/production
# Currently, the binary will look for config files at corresponding path.
FROM golang:1.8
LABEL maintainer "Baohua Yang <yangbaohua@gmail.com>"
ENV DEBIAN_FRONTEND noninteractive
# Reused in all children images
ENV FABRIC_CFG_PATH /etc/hyperledger/fabric
# Only useful for the building
ENV FABRIC_ROOT $GOPATH/src/github.com/hyperledger/fabric
ENV ARCH x86_64
# version for the base images, e.g., fabric-ccenv, fabric-baseos
ENV BASEIMAGE_RELEASE 0.3.1
# BASE_VERSION is required in core.yaml to build and run cc container
ENV BASE_VERSION 1.0.0
# version for the peer/orderer binaries, the community version tracks the hash
value like 1.0.0-snapshot-51b7e85
ENV PROJECT_VERSION 1.0.0-preview
# generic builder environment: builder: $(DOCKER_NS)/fabric-ccenv:$(ARCH)-
$(PROJECT_VERSION)
ENV DOCKER_NS hyperledger
# for golang or car's baseos: $(BASE_DOCKER_NS)/fabric-baseos:$(ARCH)-
$(BASEIMAGE_RELEASE)
ENV BASE_DOCKER_NS hyperledger
ENV LD_FLAGS="-X github.com/hyperledger/fabric/common/metadata.Version=${PROJECT_
VERSION} \
-X github.com/hyperledger/fabric/common/metadata.BaseVersion=${BASEIMAGE_
RELEASE} \
-X github.com/hyperledger/fabric/common/metadata.BaseDockerLabel=org.
hyperledger.fabric \
-X github.com/hyperledger/fabric/common/metadata.DockerNamespace=
hyperledger \
-X github.com/hyperledger/fabric/common/metadata.BaseDockerNamespace=hyper
ledger"
RUN mkdir -p /var/hyperledger/db \
/var/hyperledger/production \
# only useful when use as a ccenv image
/chaincode/input \
/chaincode/output \
$FABRIC_CFG_PATH
RUN apt-get update \
&& apt-get install -y libsnappy-dev zlib1g-dev libbz2-dev libltdl-dev \
&& rm -rf /var/cache/apt
# install chaintool
RUN curl -L https://github.com/hyperledger/fabric-chaintool/releases/download/
v0.10.3/chaintool > /usr/local/bin/chaintool \
&& chmod a+x /usr/local/bin/chaintool
# install gotools
RUN go get github.com/golang/protobuf/protoc-gen-go \
&& go get github.com/kardianos/govendor \
&& go get github.com/golang/lint/golint \
&& go get golang.org/x/tools/cmd/goimports \
&& go get github.com/onsi/ginkgo/ginkgo \
&& go get github.com/axw/gocov/... \
&& go get github.com/client9/misspell/cmd/misspell \
&& go get github.com/AlekSi/gocov-xml
# clone hyperledger fabric code and add configuration files
RUN mkdir -p $GOPATH/src/github.com/hyperledger \
&& cd $GOPATH/src/github.com/hyperledger \
&& git clone --single-branch -b master --depth 1 http://gerrit.hyperledger.
org/r/fabric \
&& cp $FABRIC_ROOT/devenv/limits.conf /etc/security/limits.conf \
&& cp -r $FABRIC_ROOT/sampleconfig/* $FABRIC_CFG_PATH
# install configtxgen, cryptogen and configtxlator
RUN cd $FABRIC_ROOT/ \
&& CGO_CFLAGS=" " go install -tags "nopkcs11" -ldflags "-X github.com/
hyperledger/fabric/common/configtx/tool/configtxgen/metadata.Version=
${PROJECT_VERSION}" github.com/hyperledger/fabric/common/configtx/tool/
configtxgen \
&& CGO_CFLAGS=" " go install -tags "" -ldflags "-X github.com/hyperledger/
fabric/common/tools/cryptogen/metadata.Version=${PROJECT_VERSION}" github.
com/hyperledger/fabric/common/tools/cryptogen \
&& CGO_CFLAGS=" " go install -tags "" -ldflags "-X github.com/hyperledger/
fabric/common/tools/configtxlator/metadata.Version=${PROJECT_VERSION}"
github.com/hyperledger/fabric/common/tools/configtxlator
# Install block-listener
RUN cd $FABRIC_ROOT/examples/events/block-listener \
&& go build \
&& mv block-listener $GOPATH/bin
# The data and config dir, can map external one with -v
VOLUME /var/hyperledger
#VOLUME /etc/hyperledger/fabric
# this is only a workaround for current hard-coded problem when using as fabric-
baseimage.
RUN ln -s $GOPATH /opt/gopath
# temporarily fix the `go list` complain problem, which is required in chaincode
packaging, see core/chaincode/platforms/golang/platform.go#GetDepoymentPayload
ENV GOROOT=/usr/local/go
WORKDIR $FABRIC_ROOT
LABEL org.hyperledger.fabric.version=${PROJECT_VERSION} \
org.hyperledger.fabric.base.version=${BASEIMAGE_RELEASE}
利用该Dockerfile,读者可以通过如下方式生成hyperledger/fabric-baseimage:latest镜像:
$ docker build -t hyperledger/fabric-baseimage:latest .
2.fabric-peer镜像
fabric-peer镜像基于fabric-baseimage生成,编译安装了peer命令。参考Dockerfile可以从http://github.com/yeasy/docker-hyperledger-fabric-peer 下载获取:
FROM hyperledger/fabric-baseimage:latest
LABEL maintainer "Baohua Yang <yangbaohua@gmail.com>"
EXPOSE 7051
# ENV CORE_PEER_MSPCONFIGPATH $FABRIC_CFG_PATH/msp
# install fabric peer and copy sampleconfigs
RUN cd $FABRIC_ROOT/peer \
&& CGO_CFLAGS=" " go install -ldflags "$LD_FLAGS -linkmode external -extldflags
'-static -lpthread'" \
&& go clean
# This will start with joining the default chain "testchainid"
# Use `peer node start --peer-defaultchain=false` will join no channel.
CMD ["peer","node","start"]
3.fabric-orderer镜像
fabric-orderer镜像基于fabric-baseimage生成,编译安装了orderer命令。参考Dockerfile可以从http://github.com/yeasy/docker-hyperledger-fabric-orderer 下载获取:
FROM hyperledger/fabric-baseimage:latest
LABEL maintainer "Baohua Yang <yangbaohua@gmail.com>"
EXPOSE 7050
ENV ORDERER_GENERAL_GENESISPROFILE=SampleInsecureSolo
ENV ORDERER_GENERAL_LOCALMSPDIR $FABRIC_CFG_PATH/msp
ENV ORDERER_GENERAL_LISTENADDRESS 0.0.0.0
ENV CONFIGTX_ORDERER_ORDERERTYPE=solo
RUN mkdir -p $FABRIC_CFG_PATH $ORDERER_GENERAL_LOCALMSPDIR
# install hyperledger fabric orderer
RUN cd $FABRIC_ROOT/orderer \
&& CGO_CFLAGS=" " go install -ldflags "$LD_FLAGS -linkmode external -extldflags
'-static -lpthread'" \
&& go clean
CMD ["orderer"]
4.fabric-ca镜像
读者可以参考如下Dockerfile内容,生成fabric-ca镜像。参考Dockerfile可以从http://github.com/yeasy/docker-hyperledger-fabric-ca 下载获取:
# Dockerfile for Hyperledger fabric-ca image.
# If you need a peer node to run, please see the yeasy/hyperledger-peer image.
# Workdir is set to $GOPATH/src/github.com/hyperledger/fabric-ca
# More usage infomation, please see https://github.com/yeasy/docker-hyperledger-
fabric-ca.
FROM golang:1.8
LABEL maintainer "Baohua Yang <yeasy.github.com>"
# ca-server and ca-client will check the following env in order, to get the home
cfg path
ENV FABRIC_CA_HOME /etc/hyperledger/fabric-ca-server
ENV FABRIC_CA_SERVER_HOME /etc/hyperledger/fabric-ca-server
ENV FABRIC_CA_CLIENT_HOME $HOME/.fabric-ca-client
ENV CA_CFG_PATH /etc/hyperledger/fabric-ca
# This is go simplify this Dockerfile
ENV FABRIC_CA_ROOT $GOPATH/src/github.com/hyperledger/fabric-ca
# Usually the binary will be installed into $GOPATH/bin, but we add local build
path, too
ENV PATH=$FABRIC_CA_ROOT/bin:$PATH
# fabric-ca-server will open service to '0.0.0.0:7054/api/v1/'
EXPOSE 7054
RUN mkdir -p $GOPATH/src/github.com/hyperledger \
$FABRIC_CA_SERVER_HOME \
$FABRIC_CA_CLIENT_HOME \
$CA_CFG_PATH \
/var/hyperledger/fabric-ca-server
# Need libtool to provide the header file ltdl.h
RUN apt-get update \
&& apt-get install -y libtool \
&& rm -rf /var/cache/apt
# clone and build ca
RUN cd $GOPATH/src/github.com/hyperledger \
&& git clone --single-branch -b master --depth 1 https://github.com/hyperledger/
fabric-ca \
# This will install fabric-ca-server and fabric-ca-client into $GOPATH/bin/
&& go install -ldflags " -linkmode external -extldflags '-static -lpthread'"
github.com/hyperledger/fabric-ca/cmd/... \
# Copy example ca and key files
&& cp $FABRIC_CA_ROOT/images/fabric-ca/payload/*.pem $FABRIC_CA_HOME/
VOLUME $FABRIC_CA_SERVER_HOME
VOLUME $FABRIC_CA_CLIENT_HOME
WORKDIR $FABRIC_CA_ROOT
# if no config exists under $FABRIC_CA_HOME, will init fabric-ca-server-config.
yaml and fabric-ca-server.db
CMD ["bash", "-c", "fabric-ca-server start -b admin:adminpw"]
提示 fabric-ca的更多安装和使用功能,将在后续章节专门进行讲解。
来源:我是码农,转载请保留出处和链接!
本文链接:http://www.54manong.com/?id=929
微信号:qq444848023 QQ号:444848023
加入【我是码农】QQ群:864689844(加群验证:我是码农)
全站首页 | 数据结构 | 区块链| 大数据 | 机器学习 | 物联网和云计算 | 面试笔试
var cnzz_protocol = (("https:" == document.location.protocol) ? "https://" : "http://");document.write(unescape("%3Cspan id='cnzz_stat_icon_1276413723'%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "s23.cnzz.com/z_stat.php%3Fid%3D1276413723%26show%3Dpic1' type='text/javascript'%3E%3C/script%3E"));本站资源大部分来自互联网,版权归原作者所有!
评论专区