Fabric_超详细的fabric网络搭建03-docker-compose文件的编写

3.docker-compose文件的编写

  • 创建channel-artifacts文件夹,将通道文件,创世块文件,锚文件移动到里面去,之后通过配置挂载到容器里面
~/testfabric$ mkdir channel-artifacts
~/testfabric$ mv channel.tx genesis.block goAnchor.tx cppAnchor.tx channel-artifacts/
  • mkdir chaincode 创建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 查看文件目录结构如下:

Fabric_超详细的fabric网络搭建03-docker-compose文件的编写_第1张图片

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 

你可能感兴趣的:(Fabric)