如何在本地网络上的多个Docker容器之间进行通信 [英] How can I make a communication between several docker containers on my local network

查看:78
本文介绍了如何在本地网络上的多个Docker容器之间进行通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用多个Docker容器的Hyperledger区块链上工作:

I'm currently working on hyperledger blockchain that use several docker containers :


  • dev-peer0.org1.example.com-marbles -v5.9

  • peer0.org1.example.com

  • couchdb

  • orderer.example.com

  • cli

  • ca.example.com

  • dev-peer0.org1.example.com-marbles-v5.9
  • peer0.org1.example.com
  • couchdb
  • orderer.example.com
  • cli
  • ca.example.com

在默认配置下,它们都在同一台计算机上运行。

我要实现的目标是将它们划分在两台不同的计算机上:

计算机1:


  • dev -peer0.org1.example.com-marbles-v5.9

  • peer0.org1.example.com

  • couchdb

计算机2:


  • orderer.example.com

  • cli

  • ca.example.com

我可以使用主机文件来完成此工作而无需编辑任何conf文件吗?

PC1:

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.31.128.146 orderer.example.com
10.31.128.146 cli
10.31.128.146 ca.example.com

PC2:

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.31.128.198 dev-peer0.org1.example.com-marbles-v5.9
10.31.128.198 peer0.org1.example.com
10.31.128.198 couchdb

我已经尝试过,但没有工作,但我想知道是否应该进一步推动这种方式?

I have already tried and I didn't work but I wonder if I should push more into that way ?

#
# Copyright IBM Corp All Rights Reserved
#
# SPDX-License-Identifier: Apache-2.0
#
version: '2'

networks:
  basic:

services:
  ca.example.com:
    image: hyperledger/fabric-ca:x86_64-1.0.0
    environment:
      - FABRIC_CA_HOME=/etc/hyperledger/fabric-ca-server
      - FABRIC_CA_SERVER_CA_NAME=ca.example.com
    ports:
      - "7054:7054"
    command: sh -c 'fabric-ca-server start --ca.certfile /etc/hyperledger/fabric-ca-server-config/org1.example.com-cert.pem --ca.keyfile /etc/hyperledger/fabric-ca-server-config/a22daf356b2aab5792ea53e35f66fccef1d7f1aa2b3a2b92dbfbf96a448ea26a_sk -b admin:adminpw -d'
    volumes:
      - ./crypto-config/peerOrganizations/org1.example.com/ca/:/etc/hyperledger/fabric-ca-server-config
    container_name: ca.example.com
    networks:
      - basic

  orderer.example.com:
    container_name: orderer.example.com
    image: hyperledger/fabric-orderer:x86_64-1.0.0
    environment:
      - ORDERER_GENERAL_LOGLEVEL=debug
      - ORDERER_GENERAL_LISTENADDRESS=0.0.0.0
      - ORDERER_GENERAL_GENESISMETHOD=file
      - ORDERER_GENERAL_GENESISFILE=/etc/hyperledger/configtx/genesis.block
      - ORDERER_GENERAL_LOCALMSPID=OrdererMSP
      - ORDERER_GENERAL_LOCALMSPDIR=/etc/hyperledger/msp/orderer/msp
    working_dir: /opt/gopath/src/github.com/hyperledger/fabric/orderer
    command: orderer
    ports:
      - 7050:7050
    volumes:
        - ./config/:/etc/hyperledger/configtx
        - ./crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/:/etc/hyperledger/msp/orderer
        - ./crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/:/etc/hyperledger/msp/peerOrg1
    networks:
      - basic

  peer0.org1.example.com:
    container_name: peer0.org1.example.com
    image: hyperledger/fabric-peer:x86_64-1.0.0
    environment:
      - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
      - CORE_PEER_ID=peer0.org1.example.com
      - CORE_LOGGING_PEER=debug
      - CORE_CHAINCODE_LOGGING_LEVEL=DEBUG
      - CORE_PEER_LOCALMSPID=Org1MSP
      - CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp/peer/
      - CORE_PEER_ADDRESS=peer0.org1.example.com:7051
      # # 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=${COMPOSE_PROJECT_NAME}_basic
      - CORE_LEDGER_STATE_STATEDATABASE=CouchDB
      - CORE_LEDGER_STATE_COUCHDBCONFIG_COUCHDBADDRESS=couchdb:5984
    working_dir: /opt/gopath/src/github.com/hyperledger/fabric
    command: peer node start
    # command: peer node start --peer-chaincodedev=true
    ports:
      - 7051:7051
      - 7053:7053
    volumes:
        - /var/run/:/host/var/run/
        - ./crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/msp:/etc/hyperledger/msp/peer
        - ./crypto-config/peerOrganizations/org1.example.com/users:/etc/hyperledger/msp/users
        - ./config:/etc/hyperledger/configtx
    depends_on:
      - orderer.example.com
    networks:
      - basic

  couchdb:
    container_name: couchdb
    image: hyperledger/fabric-couchdb:x86_64-1.0.0
    ports:
      - 5984:5984
    environment:
      DB_URL: http://localhost:5984/member_db
    networks:
      - basic

  cli:
    container_name: cli
    image: hyperledger/fabric-tools:x86_64-1.0.0
    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_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
      - CORE_CHAINCODE_KEEPALIVE=10
    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/
        - ./crypto-config:/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/
    networks:
        - basic
    #depends_on:
    #  - orderer.example.com
    #  - peer0.org1.example.com
    #  - couchdb


推荐答案

我建议为覆盖网络启用Swarm模式。理想情况下,您甚至可以在Swarm模式下运行容器,但这并不仅仅是覆盖网络。

I would recommend enabling Swarm Mode for an overlay network. Ideally you can even run the containers inside Swarm Mode, but that's not necessary for just the overlay networking.

一个主机,运行以下命令创建一个管理器:



One one host, run the following to create a manager:

docker swarm init

然后在第二台主机上运行输出 docker swarm join 命令。

Then run the output docker swarm join command on your second host.

选项A:仅用于覆盖网络,您需要创建一个可连接的网络以在群集模式之外使用它:

Option A: for only overlay networking, you need to create a network as attachable to use it outside of swarm mode:

docker network create -d overlay --attachable basic

然后在您的撰写文件中,调整网络定义为外部:

And then in your compose file, adjust the network definition to be external:

version: '2'
networks:
  basic:
    external:
      name: basic

这样,您的容器就可以直接通过覆盖网络进行通信。

With that, your containers will be able to communicate directly over the overlay network.

选项B:使用Swarm模式运行contai另外,您可以跳过网络创建并将网络设置为外部网络。只需将您的版本更新为compose.yml文件中的 version:‘3’,我还要删除 container_name行。然后运行:

Option B: To use Swarm Mode to run the containers, you can skip the network creation and setting the network to external. Just update your version to version: '3' inside your compose.yml file, I'd also remove the "container_name" lines. Then run:

docker stack deploy -c compose.yml hyperledger

创建称为超级账本的堆栈。

to create a stack called hyperledger.

这篇关于如何在本地网络上的多个Docker容器之间进行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆