3.docker-compose檔案的編寫
- 建立channel-artifacts檔案夾,将通道檔案,創世塊檔案,錨檔案移動到裡面去,之後通過配置挂載到容器裡面
~/testfabric$ mkdir channel-artifacts
~/testfabric$ mv channel.tx genesis.block goAnchor.tx cppAnchor.tx channel-artifacts/
-
建立chaincode檔案夾用于存放鍊碼檔案(會被挂載到用戶端容器裡)mkdir chaincode
- 拷貝fabric-samples/first-network/docker-compose-cli.yaml中的檔案到本目錄中
cp ~/hyperledger-fabric/fabric-samples/first-network/docker-compose-cli.yaml .
- 拷貝fabric-samples/first-network/base中檔案到本目錄中
cp -r ~/hyperledger-fabric/fabric-samples/first-network/base/docker-compose-base.yaml .
cp -r ~/hyperledger-fabric/fabric-samples/first-network/base/peer-base.yaml .
-
檢視檔案目錄結構如下:tree -L 2

1.用戶端容器配置
- 修改docker-compose-cli.yaml檔案
vi docker-compose-cli.yaml
# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#
version: '2'
volumes:
orderer.github.com:
peer0.orggo.github.com:
peer1.orggo.github.com:
peer0.orgcpp.github.com:
peer1.orgcpp.github.com:
networks:
byfn:
services:
orderer.github.com:
extends:
file: docker-compose-base.yaml
service: orderer.github.com
container_name: orderer.github.com
networks:
- byfn
peer0.orggo.github.com:
container_name: peer0.orggo.github.com
extends:
file: docker-compose-base.yaml
service: peer0.orggo.github.com
networks:
- byfn
peer1.orggo.github.com:
container_name: peer1.orggo.github.com
extends:
file: docker-compose-base.yaml
service: peer1.orggo.github.com
networks:
- byfn
peer0.orgcpp.github.com:
container_name: peer0.orgcpp.github.com
extends:
file: docker-compose-base.yaml
service: peer0.orgcpp.github.com
networks:
- byfn
peer1.orgcpp.github.com:
container_name: peer1.orgcpp.github.com
extends:
file: docker-compose-base.yaml
service: peer1.orgcpp.github.com
networks:
- byfn
cli:
container_name: cli
image: hyperledger/fabric-tools
tty: true
stdin_open: true
environment:
- GOPATH=/opt/gopath
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock #套接字檔案
- CORE_LOGGING_LEVEL=INFO #日志級别,critical,error,warning,notice,info,debug從高到低
- CORE_PEER_ID=cli #目前節點的ID即名字
- CORE_PEER_ADDRESS=peer0.orggo.github.com:7051 #連接配接peer節點的位址
- CORE_PEER_LOCALMSPID=OrgGoMSP #連接配接peer節點所屬的組織
- CORE_PEER_TLS_ENABLED=true #是否使用TLS加密,true時下面的3個檔案才生效
# 通信時使用的TLS證書, .crt證書, .key密鑰
- CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orggo.github.com/peers/peer0.orggo.github.com/tls/server.crt
# 通信時使用的TLS密鑰
- CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orggo.github.com/peers/peer0.orggo.github.com/tls/server.key
# TLS根證書檔案
- CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orggo.github.com/peers/peer0.orggo.github.com/tls/ca.crt
# 用戶端角色,連接配接網絡通路節點,需要賬号
# 普通賬号: 交易(寫資料),查詢(讀資料)
# 管理者賬号: 交易(寫資料),查詢(讀資料),建立通道,讓某個節點加入到通道,安裝鍊碼,鍊碼初始化
- CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/orggo.github.com/users/[email protected]/msp
working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
command: /bin/bash
volumes:
- /var/run/:/host/var/run/
- ./chaincode/:/opt/gopath/src/github.com/chaincode
- ./crypto-config:/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/
- ./channel-artifacts:/opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifacts
depends_on:
- orderer.github.com
- peer0.orggo.github.com
- peer1.orggo.github.com
- peer0.orgcpp.github.com
- peer1.orgcpp.github.com
networks:
- byfn
2.orderer和peer節點的配置
docker-compose-base.yaml中orderer節點和peer節點都引用了docker-compose-base.yaml檔案
- 修改docker-compose-base.yaml檔案
vi docker-compose-base.yaml
# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#
version: '2'
services:
orderer.github.com:
container_name: orderer.github.com
image: hyperledger/fabric-orderer
environment:
- ORDERER_GENERAL_LOGLEVEL=INFO
- ORDERER_GENERAL_LISTENADDRESS=0.0.0.0 #監聽本機位址,如果是0會自動讀取網卡,識别實際的IP
- ORDERER_GENERAL_GENESISMETHOD=file #生成創始區塊的資料來源
- ORDERER_GENERAL_GENESISFILE=/var/hyperledger/orderer/orderer.genesis.block #來自具體哪個檔案
- ORDERER_GENERAL_LOCALMSPID=OrdererMSP #目前節點屬于哪個組織ID
- ORDERER_GENERAL_LOCALMSPDIR=/var/hyperledger/orderer/msp
# enabled TLS
- ORDERER_GENERAL_TLS_ENABLED=true
- ORDERER_GENERAL_TLS_PRIVATEKEY=/var/hyperledger/orderer/tls/server.key
- ORDERER_GENERAL_TLS_CERTIFICATE=/var/hyperledger/orderer/tls/server.crt
- ORDERER_GENERAL_TLS_ROOTCAS=[/var/hyperledger/orderer/tls/ca.crt]
working_dir: /opt/gopath/src/github.com/hyperledger/fabric
command: orderer
volumes:
- ./channel-artifacts/genesis.block:/var/hyperledger/orderer/orderer.genesis.block
- ./crypto-config/ordererOrganizations/github.com/orderers/orderer.github.com/msp:/var/hyperledger/orderer/msp
- ./crypto-config/ordererOrganizations/github.com/orderers/orderer.github.com/tls/:/var/hyperledger/orderer/tls
- orderer.github.com:/var/hyperledger/production/orderer
ports:
- 7050:7050
peer0.orggo.github.com:
container_name: peer0.orggo.github.com
extends:
file: peer-base.yaml
service: peer-base
environment:
- CORE_PEER_ID=peer0.orggo.github.com #指定目前peer節點的名字
- CORE_PEER_ADDRESS=peer0.orggo.github.com:7051 #目前peer節點的通路位址
- CORE_PEER_GOSSIP_BOOTSTRAP=peer0.orggo.github.com:7051
# 設定目前節點是否被外部感覺, 如果想, 就指定自己的位址, 如果不想不用指定了
- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.orggo.github.com:7051
- CORE_PEER_LOCALMSPID=OrgGoMSP #目前peer所屬的組織的ID
volumes:
- /var/run/:/host/var/run/
- ./crypto-config/peerOrganizations/orggo.github.com/peers/peer0.orggo.github.com/msp:/etc/hyperledger/fabric/msp
- ./crypto-config/peerOrganizations/orggo.github.com/peers/peer0.orggo.github.com/tls:/etc/hyperledger/fabric/tls
- peer0.orggo.github.com:/var/hyperledger/production
ports:
- 7051:7051
- 7053:7053
peer1.orggo.github.com:
container_name: peer1.orggo.github.com
extends:
file: peer-base.yaml
service: peer-base
environment:
- CORE_PEER_ID=peer1.orggo.github.com
- CORE_PEER_ADDRESS=peer1.orggo.github.com:7051
- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer1.orggo.github.com:7051
- CORE_PEER_GOSSIP_BOOTSTRAP=peer1.orggo.github.com:7051
- CORE_PEER_LOCALMSPID=OrgGoMSP
volumes:
- /var/run/:/host/var/run/
- ./crypto-config/peerOrganizations/orggo.github.com/peers/peer1.orggo.github.com/msp:/etc/hyperledger/fabric/msp
- ./crypto-config/peerOrganizations/orggo.github.com/peers/peer1.orggo.github.com/tls:/etc/hyperledger/fabric/tls
- peer1.orggo.github.com:/var/hyperledger/production
ports:
- 8051:7051
- 8053:7053
peer0.orgcpp.github.com:
container_name: peer0.orgcpp.github.com
extends:
file: peer-base.yaml
service: peer-base
environment:
- CORE_PEER_ID=peer0.orgcpp.github.com
- CORE_PEER_ADDRESS=peer0.orgcpp.github.com:7051
- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.orgcpp.github.com:7051
- CORE_PEER_GOSSIP_BOOTSTRAP=peer0.orgcpp.github.com:7051
- CORE_PEER_LOCALMSPID=OrgCppMSP
volumes:
- /var/run/:/host/var/run/
- ./crypto-config/peerOrganizations/orgcpp.github.com/peers/peer0.orgcpp.github.com/msp:/etc/hyperledger/fabric/msp
- ./crypto-config/peerOrganizations/orgcpp.github.com/peers/peer0.orgcpp.github.com/tls:/etc/hyperledger/fabric/tls
- peer0.orgcpp.github.com:/var/hyperledger/production
ports:
- 9051:7051
- 9053:7053
peer1.orgcpp.github.com:
container_name: peer1.orgcpp.github.com
extends:
file: peer-base.yaml
service: peer-base
environment:
- CORE_PEER_ID=peer1.orgcpp.github.com
- CORE_PEER_ADDRESS=peer1.orgcpp.github.com:7051
- CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer1.orgcpp.github.com:7051
- CORE_PEER_GOSSIP_BOOTSTRAP=peer1.orgcpp.github.com:7051
- CORE_PEER_LOCALMSPID=OrgCppMSP
volumes:
- /var/run/:/host/var/run/
- ./crypto-config/peerOrganizations/orgcpp.github.com/peers/peer1.orgcpp.github.com/msp:/etc/hyperledger/fabric/msp
- ./crypto-config/peerOrganizations/orgcpp.github.com/peers/peer1.orgcpp.github.com/tls:/etc/hyperledger/fabric/tls
- peer1.orgcpp.github.com:/var/hyperledger/production
ports:
- 10051:7051
- 10053:7053
docker-compose-base.yaml中所有的peer節點都引用了peer-base.yaml檔案
- 修改peer-base.yaml檔案
vi peer-base.yaml
# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#
version: '2'
services:
peer-base:
image: hyperledger/fabric-peer
environment:
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
# the following setting starts chaincode containers on the same
# bridge network as the peers
# https://docs.docker.com/compose/networking/
# 要指定docker加入的網絡名.舉例:如果是Demo,得到的是demo_byfn
- CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=testfabric_byfn
- CORE_LOGGING_LEVEL=INFO
- CORE_PEER_TLS_ENABLED=true
- CORE_PEER_GOSSIP_USELEADERELECTION=true #是否自動選舉leader節點
- CORE_PEER_GOSSIP_ORGLEADER=false #指定目前節點是不是leader節點
- CORE_PEER_PROFILE_ENABLED=true #peer節點啟動後PROFILE程序是否啟動
- CORE_PEER_TLS_CERT_FILE=/etc/hyperledger/fabric/tls/server.crt
- CORE_PEER_TLS_KEY_FILE=/etc/hyperledger/fabric/tls/server.key
- CORE_PEER_TLS_ROOTCERT_FILE=/etc/hyperledger/fabric/tls/ca.crt
working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
command: peer node start
3.啟動容器
docker-compose --help
-f, --file FILE 指定配置檔案
-d : 以守護程序的方式運作
up 啟動容器
down 關閉容器
docker-compose -f docker-compose-cli.yaml up -d
成功啟動會出現:
Creating peer1.orggo.github.com ... done
Creating orderer.github.com ... done
Creating peer1.orgcpp.github.com ... done
Creating peer0.orgcpp.github.com ... done
Creating peer0.orggo.github.com ... done
Creating cli ... done
這時執行
docker-compose -f docker-compose-cli.yaml ps
全部為up狀态說明啟動成功!
Name Command State Ports
----------------------------------------------------------------------------------------------------
cli /bin/bash Up
orderer.github.com orderer Up 0.0.0.0:7050->7050/tcp
peer0.orgcpp.github.com peer node start Up 0.0.0.0:9051->7051/tcp, 0.0.0.0:9053->7053/tcp
peer0.orggo.github.com peer node start Up 0.0.0.0:7051->7051/tcp, 0.0.0.0:7053->7053/tcp
peer1.orgcpp.github.com peer node start Up 0.0.0.0:10051->7051/tcp, 0.0.0.0:10053->7053/tcp
peer1.orggo.github.com peer node start Up 0.0.0.0:8051->7051/tcp, 0.0.0.0:8053->7053/tcp