• -------------------------------------------------------------
  • ====================================

15-Yaml文件格式介绍&docker-compse.yaml详解

区块链 dewbay 4年前 (2020-01-06) 1691次浏览 已收录 0个评论 扫描二维码

上一篇文章讲到,搭建 Fabric 网络的过程中,启动 1CLI+4Peer+1Orderer 容器,有两种方式。一种是利用 docker-composer 来自动部署,还有一种,就是手动启动每一个容器。

这篇文章,主要是讲解利用 Docker 容器来启动 Fabric 网络。接下来的内容,主要是对

《HyperLedger 实战-手动搭建一个 Fabric 网络-基于 Docker 容器的方式》 中第三部分

配置 Fabric 环境的 docker-compose 文件 内容的进一步分析。

全文将按照如下顺序展开:

15-Yaml文件格式介绍&docker-compse.yaml详解
全文结构

一、Yaml 模板文件&docker 指令

Yaml模板文件

模板文件是使用 Compose 的核心,默认的模板文件名称为 docker-compose.yml,格式为 YAML 格式。

在旧版本(版本 1)中,其中每个顶级元素为服务名称,次级元素为服务容器的配置信息,例如

webapp:
  image: examples/web
  ports:
    - "80:80"
  volumes:
    - "/data"

版本 2 扩展了 Compose 的语法,同时尽量保持跟版本 1 的兼容,除了可以声明网络和存储信息外,最大的不同一是添加了版本信息,另一个是需要将所有的服务放到 services 根下面。例如,上面例子改写为版本 2,内容为

version: "2"
services:
  webapp:
    image: examples/web
    ports:
      - "80:80"
    volumes:
      - "/data"

【声明】 在 Fabric 网络,涉及的 docker-composer 文件,都是版本 2 的 compose 语法。

注意!每个服务都必须通过 image 指令指定镜像或 build 指令(需要 Dockerfile)等来自动构建生成镜像。

docker 指令

下面对我们在后文中将要用到的 docker 指令进行介绍:

  • container_name

指定容器名称。默认将会使用 项目名称 _ 服务名称 _ 序号 这样的格式。

[例如]  container_name: docker-web-container
  • image

指定为镜像名称或镜像 ID。如果镜像在本地不存在,Compose 将会尝试拉去这个镜像。

[例如]
image: ubuntu
image: orchardup/postgresql
image: a4bc65fd
  • command

覆盖容器启动后默认执行的命令。

[例如]  command: echo "hello world"
  • environment

设置环境变量。你可以使用数组或字典两种格式。

只给定名称的变量会自动获取运行 Compose 主机上对应变量的值,可以用来防止泄露不必要的数据。

[例如]
environment:
  RACK_ENV: development
  SESSION_SECRET:
[或者]
environment:
  - RACK_ENV=development
  - SESSION_SECRET
  • extends

基于其它模板文件进行扩展。

例如我们已经有了一个 webapp 服务,定义一个基础模板文件为 common.yml

# common.yml
webapp:
  build: ./webapp
  environment:
    - DEBUG=false
    - SEND_EMAILS=false

再编写一个新的 development.yml 文件,使用 common.yml 中的 webapp 服务进行扩展。

# development.yml
web:
  extends:
    file: common.yml
    service: webapp
  ports:
    - "8000:8000"
  links:
    - db
  environment:
    - DEBUG=true
db:
  image: postgres

后者会自动继承 common.yml 中的 webapp 服务及环境变量定义

  • working_dir

指定容器中工作目录。

[例如]:working_dir: /code
  • volumes

数据卷所挂载路径设置。可以设置宿主机路径 (HOST:CONTAINER) 或加上访问模式 (HOST:CONTAINER:ro)。该指令中路径支持相对路径。

[例如]
volumes:
 - /var/lib/mysql
 - cache/:/tmp/cache
 - ~/configs:/etc/configs/:ro
  • ports

暴露端口信息。

使用宿主:容器 (HOST:CONTAINER)格式 或者 仅仅指定容器的端口(宿主将会随机选择端口)都可以。

[例如]
ports:
 - "3000"
 - "8000:8000"
 - "49100:22"
 - "127.0.0.1:8001:8001"

在 fabric 网络搭建的过程中,关于 peer 节点的 docker-compose 文件配置内容如下:

peer0.org1.example.com:
    container_name: peer0.org1.example.com
    ...
    ports:
      - 7051:7051
      - 7053:7053

peer1.org1.example.com:
    container_name: peer1.org1.example.com
    ...
    ports:
      - 8051:7051
      - 8053:7053

peer0.org2.example.com:
    container_name: peer0.org2.example.com
    ...
    ports:
      - 9051:7051
      - 9053:7053

peer1.org2.example.com:
    container_name: peer1.org2.example.com
    ...
    ports:
      - 10051:7051
      - 10053:7053

在上述的配置中,通过将宿主主机的本地端口 7051、8051、9051、10051 等四个端口,分别映射为四个 docker 容器的 7051 端口;将 7052、8052、90052、10052 等四个端口,分别映射为 docker 容器的 7052 端口。

  • tty

模拟一个假的远程控制台。

[例如] tty:true
  • build

指定 Dockerfile 所在文件夹的路径(可以是绝对路径,或者相对 docker-compose.yml 文件的路径)。 Compose 将会利用它自动构建这个镜像,然后使用这个镜像。

[例如]:build: /path/to/build/dir
  • depends_on

可以保证 build 的先后顺序。

[例如]:
depends_on:
      - orderer.example.com
      - peer0.org1.example.com

二、Orderer 节点配置文件 | docker-compose-base.yaml

Orderer 的配置是在 base/docker-compose-base.yaml 里面。

结合上面一部分的 docker 命令,我们对该文件进行注释

# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#

version: '2'  # 表示用的是版本 2 的 YAML 模板

services:     # 在版本 2 中,所有的服务都要放在 services 根下面

  orderer.example.com:   # Orderer 排序服务
    container_name: orderer.example.com   # 定义容器的名称
    image: hyperledger/fabric-orderer     # 指定容器的镜像
    environment:         # 设置环境变量
      - ORDERER_GENERAL_LOGLEVEL=debug
      - ORDERER_GENERAL_LISTENADDRESS=0.0.0.0
      - ORDERER_GENERAL_GENESISMETHOD=file
      - ORDERER_GENERAL_GENESISFILE=/var/hyperledger/orderer/orderer.genesis.block
      - ORDERER_GENERAL_LOCALMSPID=OrdererMSP
      - 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/example.com/orderers/orderer.example.com/msp:/var/hyperledger/orderer/msp
    - ../crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/tls/:/var/hyperledger/orderer/tls
    ports:             # 暴露端口信息 
      - 7050:7050

  peer0.org1.example.com: # Org1 的 Peer0 服务
    container_name: peer0.org1.example.com
    extends:
      file: peer-base.yaml
      service: peer-base
    environment:
      - CORE_PEER_ID=peer0.org1.example.com
      - CORE_PEER_ADDRESS=peer0.org1.example.com:7051
      - CORE_PEER_CHAINCODELISTENADDRESS=peer0.org1.example.com:7052
      - CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.org1.example.com:7051
      - CORE_PEER_LOCALMSPID=Org1MSP
    volumes:
        - /var/run/:/host/var/run/
        - ../crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/msp:/etc/hyperledger/fabric/msp
        - ../crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls:/etc/hyperledger/fabric/tls
    ports:
      - 7051:7051
      - 7052:7052
      - 7053:7053

  peer1.org1.example.com:    # Org1 的 Peer1 服务
    container_name: peer1.org1.example.com
    extends:
      file: peer-base.yaml
      service: peer-base
    environment:
      - CORE_PEER_ID=peer1.org1.example.com
      - CORE_PEER_ADDRESS=peer1.org1.example.com:7051
      - CORE_PEER_CHAINCODELISTENADDRESS=peer1.org1.example.com:7052
      - CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer1.org1.example.com:7051
      - CORE_PEER_GOSSIP_BOOTSTRAP=peer0.org1.example.com:7051
      - CORE_PEER_LOCALMSPID=Org1MSP
    volumes:
        - /var/run/:/host/var/run/
        - ../crypto-config/peerOrganizations/org1.example.com/peers/peer1.org1.example.com/msp:/etc/hyperledger/fabric/msp
        - ../crypto-config/peerOrganizations/org1.example.com/peers/peer1.org1.example.com/tls:/etc/hyperledger/fabric/tls

    ports:
      - 8051:7051
      - 8052:7052
      - 8053:7053

  peer0.org2.example.com:    # Org2 的 Peer0 服务
    container_name: peer0.org2.example.com
    extends:
      file: peer-base.yaml
      service: peer-base
    environment:
      - CORE_PEER_ID=peer0.org2.example.com
      - CORE_PEER_ADDRESS=peer0.org2.example.com:7051
      - CORE_PEER_CHAINCODELISTENADDRESS=peer0.org2.example.com:7052
      - CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.org2.example.com:7051
      - CORE_PEER_LOCALMSPID=Org2MSP
    volumes:
        - /var/run/:/host/var/run/
        - ../crypto-config/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/msp:/etc/hyperledger/fabric/msp
        - ../crypto-config/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls:/etc/hyperledger/fabric/tls
    ports:
      - 9051:7051
      - 9052:7052
      - 9053:7053

  peer1.org2.example.com:   # Org1 的 Peer0 服务
    container_name: peer1.org2.example.com
    extends:
      file: peer-base.yaml
      service: peer-base
    environment:
      - CORE_PEER_ID=peer1.org2.example.com
      - CORE_PEER_ADDRESS=peer1.org2.example.com:7051
      - CORE_PEER_CHAINCODELISTENADDRESS=peer1.org2.example.com:7052
      - CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer1.org2.example.com:7051
      - CORE_PEER_GOSSIP_BOOTSTRAP=peer0.org2.example.com:7051
      - CORE_PEER_LOCALMSPID=Org2MSP
    volumes:
        - /var/run/:/host/var/run/
        - ../crypto-config/peerOrganizations/org2.example.com/peers/peer1.org2.example.com/msp:/etc/hyperledger/fabric/msp
        - ../crypto-config/peerOrganizations/org2.example.com/peers/peer1.org2.example.com/tls:/etc/hyperledger/fabric/tls
    ports:
      - 10051:7051
      - 10052:7052
      - 10053:7053

三、Peer 节点的配置文件 | peer-base.yaml

Peer 的配置是在 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/
      - CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=e2ecli_default
      #- CORE_LOGGING_LEVEL=ERROR
      - CORE_LOGGING_LEVEL=DEBUG
      - CORE_PEER_TLS_ENABLED=true
      - CORE_PEER_GOSSIP_USELEADERELECTION=true
      - CORE_PEER_GOSSIP_ORGLEADER=false
      - CORE_PEER_PROFILE_ENABLED=true
      - 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   # 容器启动之后,运行 peer node start 命令开启服务

四、CLI 的配置文件 | docker-compose-cli.yaml

CLI 在整个 Fabric 网络中扮演客户端的角色,我们在开发测试的时候可以用 CLI 来代替 SDK,执行各种 SDK 能执行的操作。CLI 会和 Peer 相连,把指令发送给对应的 Peer 执行。

CLI 的配置在 docker-compose-cli.yaml 中,接下来会对该文件进行注释,文件内容如下:

# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#

version: '2'

services:  # Service 根下,是该模板文件定义的所有服务

  orderer.example.com: # Orderer 服务
    extends:  # 基于其它模板进行扩展。
      file:   base/docker-compose-base.yaml # 进行拓展时使用的文件
      service: orderer.example.com          # 进行拓展时使用的服务
    # 以上表示使用 base/docker-compose-base.yaml 中的 orderer.example.com 服务进行拓展
    container_name: orderer.example.com

  peer0.org1.example.com:
    container_name: peer0.org1.example.com
    extends:
      file:  base/docker-compose-base.yaml
      service: peer0.org1.example.com

  peer1.org1.example.com:
    container_name: peer1.org1.example.com
    extends:
      file:  base/docker-compose-base.yaml
      service: peer1.org1.example.com

  peer0.org2.example.com:
    container_name: peer0.org2.example.com
    extends:
      file:  base/docker-compose-base.yaml
      service: peer0.org2.example.com

  peer1.org2.example.com:
    container_name: peer1.org2.example.com
    extends:
      file:  base/docker-compose-base.yaml
      service: peer1.org2.example.com

  cli:
    container_name: cli
    image: hyperledger/fabric-tools
    tty: true  # 模拟一个假的远程控制台。
    environment:
      - GOPATH=/opt/gopath
      - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
      - CORE_LOGGING_LEVEL=DEBUG
      - CORE_PEER_ID=cli
      - CORE_PEER_ADDRESS=peer0.org1.example.com:7051
      - CORE_PEER_LOCALMSPID=Org1MSP
      - CORE_PEER_TLS_ENABLED=true
      - CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.crt
      - CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.key
      - CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
      - CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
    working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
    command: /bin/bash -c './scripts/script.sh ${CHANNEL_NAME}; sleep $TIMEOUT'
    volumes:
        - /var/run/:/host/var/run/
        - ../chaincode/go/:/opt/gopath/src/github.com/hyperledger/fabric/examples/chaincode/go
        - ./crypto-config:/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/
        - ./scripts:/opt/gopath/src/github.com/hyperledger/fabric/peer/scripts/
        - ./channel-artifacts:/opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifacts
    depends_on:  # 保证服务开启的顺序
      - orderer.example.com
      - peer0.org1.example.com
      - peer1.org1.example.com
      - peer0.org2.example.com
      - peer1.org2.example.com

五、总结

这篇文章,主要是介绍了一下 docker 的 YAML 模板文件;

接着列举了几种常见的 docker 指令。

最后,通过结合前面的讲解,对实战中遇到的 docker-compose-base.yaml、

peer-base.yaml 和 docker-compose-cli.yaml 文件进行了详细的解释。

这篇文章算是为下一篇文章《手动搭建一个 Fabric 网络-纯手动部署方式》预热吧!


参考链接

 

 

转自知乎 苏小乐 :https://www.zhihu.com/people/shan-de-ding-zhu/activities


露水湾 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:15-Yaml文件格式介绍&docker-compse.yaml详解
喜欢 (0)
[]
分享 (0)
关于作者:
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址