有没有一种方法可以使用 docker compose 复制容器?

Is there a method to replicate container using docker compose?

我没有使用 docker swarm 或 Kubernetes 等容器编排技术。 我的目标是用不同的 IP:Port 复制 ca0 的服务来监听请求。这是维护 ca0 提供的服务的高可用性 (HA) 所必需的。 ca0的组成如下图:

version: '2'

networks:
  byfn:

services:
  ca0:
    image: hyperledger/fabric-ca:$IMAGE_TAG
    environment:
      - FABRIC_CA_HOME=/etc/hyperledger/fabric-ca-server
      - FABRIC_CA_SERVER_CA_NAME=ca-org1
      - FABRIC_CA_SERVER_TLS_ENABLED=true
      - FABRIC_CA_SERVER_TLS_CERTFILE=/etc/hyperledger/fabric-ca-server-config/ca.org1.example.com-cert.pem
      - FABRIC_CA_SERVER_TLS_KEYFILE=/etc/hyperledger/fabric-ca-server-config/key.pem
      - FABRIC_CA_SERVER_PORT=7054
    ports:
      - "7054:7054"
    command: sh -c 'fabric-ca-server start --ca.certfile /etc/hyperledger/fabric-ca-server-config/ca.org1.example.com-cert.pem --ca.keyfile /etc/hyperledger/fabric-ca-server-config/key.pem -b admin:adminpw -d'
    volumes:
      - ./crypto-config/peerOrganizations/org1.example.com/ca/:/etc/hyperledger/fabric-ca-server-config
    container_name: ca0_peerOrg1
    networks:
      - byfn

同样,要复制相同的服务,服务将其命名为ca1,容器名称ca1_peerOrg,我只是更改端口并将其附加到如上定义的先前ca0服务。 ca1服务定义如下:

# Replicated CA
ca1:
    image: hyperledger/fabric-ca:$IMAGE_TAG
    environment:
      - FABRIC_CA_HOME=/etc/hyperledger/fabric-ca-server
      - FABRIC_CA_SERVER_CA_NAME=ca-org1
      - FABRIC_CA_SERVER_TLS_ENABLED=true
      - FABRIC_CA_SERVER_TLS_CERTFILE=/etc/hyperledger/fabric-ca-server-config/ca.org1.example.com-cert.pem
      - FABRIC_CA_SERVER_TLS_KEYFILE=/etc/hyperledger/fabric-ca-server-config/key.pem
      - FABRIC_CA_SERVER_PORT=8054
    ports:
      - "8054:8054"
    command: sh -c 'fabric-ca-server start --ca.certfile /etc/hyperledger/fabric-ca-server-config/ca.org1.example.com-cert.pem --ca.keyfile /etc/hyperledger/fabric-ca-server-config/key.pem -b admin:adminpw -d'
    volumes:
      - ./crypto-config/peerOrganizations/org1.example.com/ca/:/etc/hyperledger/fabric-ca-server-config
    container_name: ca1_peerOrg1
    networks:
      - byfn

我 运行 docker 撰写文件并得到一个错误:

        gopal@gopal:~/Dappdev/first/fabric-samples/first-network$ docker-compose -f docker-compose-ca.yaml up  
ERROR: The Compose file './docker-compose-ca.yaml' is invalid because:
Invalid top-level property "ca1". Valid top-level sections for this Compose file are: services, version, networks, volumes, and extensions starting with "x-".

You might be seeing this error because you're using the wrong Compose file version. Either specify a supported version (e.g "2.2" or "3.3") and place your service definitions under the `services` key, or omit the `version` key and place your service definitions at the root of the file to use version 1.
For more on the Compose file format versions, see https://docs.docker.com/compose/compose-file/

我创建复制容器的方法不正确。

How to replicate a container with the different selected port?

您可以使用 YAML 锚点和合并语法:

services:
  ca0: &name-me  # <- this is an anchor
    image: hyperledger/fabric-ca:$IMAGE_TAG
    environment:
      - FABRIC_CA_HOME=/etc/hyperledger/fabric-ca-server
      - FABRIC_CA_SERVER_CA_NAME=ca-org1
      - FABRIC_CA_SERVER_TLS_ENABLED=true
      - FABRIC_CA_SERVER_TLS_CERTFILE=/etc/hyperledger/fabric-ca-server-config/ca.org1.example.com-cert.pem
      - FABRIC_CA_SERVER_TLS_KEYFILE=/etc/hyperledger/fabric-ca-server-config/key.pem
      - FABRIC_CA_SERVER_PORT=7054
    ports:
      - "7054:7054"
    command: sh -c 'fabric-ca-server start --ca.certfile /etc/hyperledger/fabric-ca-server-config/ca.org1.example.com-cert.pem --ca.keyfile /etc/hyperledger/fabric-ca-server-config/key.pem -b admin:adminpw -d'
    volumes:
      - ./crypto-config/peerOrganizations/org1.example.com/ca/:/etc/hyperledger/fabric-ca-server-config
    container_name: ca0_peerOrg1
    networks:
      - byfn
  ca1:
    <<: *name-me  # <- this is a merge (<<) with an alias (*name-me)
    # keys below merge notation override those that declared under anchor
    # so this:
    ports:
    - "8054:7054"
    # replaces default 'ports' (the whole key)

别名 *name-me 将所有内容都置于锚点 &name-me 下,但使用合并 <<: 符号,您可以覆盖锚点的某些属性(本例中为端口)。我认为您不能 使用 swarm 或 kubernetes,因为这不是管理副本的好方法。可以的话就去swarm或者K8s吧。