简体   繁体   中英

Run docker exec from remote machine in Hyperledger Fabric+

I am running Hyperledger fabric with n number of peers, each on different VM. VM0 has peer0, orderer and CA. VM1 has peer1, VM2 has peer2 and so on. After all the docker containers are up, channel is created, each peer joins the channel, chaincode is installed and instantiated through peer0, later installed on all other peers.

# Stop existing containers and bring up the network
docker-compose -f docker-compose.yml down
docker-compose -f docker-compose.yml up -d

# Create the channel
docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp/users/Admin@org1.example.com/msp" peer0.org1.example.com peer channel create -o orderer.example.com:7050 -c mychannel -f /etc/hyperledger/configtx/channel.tx
# Join peer0.org1.example.com to the channel.
docker exec -e "CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp/users/Admin@org1.example.com/msp" peer0.org1.example.com peer channel fetch config -o orderer.example.com:7050 -c mychannel

docker exec -e "CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp/users/Admin@org1.example.com/msp" peer0.org1.example.com peer channel join -b mychannel_config.block

# Install and instantiate chaincode through CLI container
docker exec -e "CORE_PEER_ADDRESS=peer0.org1.example.com:7051" cli peer chaincode install -n exp2 -p github.com/exp2/go -v 1.0
docker exec -e "CORE_PEER_ADDRESS=peer0.org1.example.com:7051" cli peer chaincode instantiate -o orderer.example.com:7050 -C mychannel -n exp2 -v 1.0 -c '{"Args":[""]}' -P "AND('Org1MSP.member')"
sleep 5
docker exec -e "CORE_PEER_ADDRESS=peer0.org1.example.com:7051" cli peer chaincode invoke -o orderer.example.com:7050 -C mychannel -n exp2 -c '{"function":"initLedger","Args":[""]}'

With current setup I have to run the similar script separately on each VM to make the peer join the channel and install the chaincode. Is there any way to just run one script from VM0 and complete the setup on other VMs. Can we bring up docker container remotely and execute docker exec command remotely on all other VMs?

With ansible it's pretty easy to do it: https://www.ansible.com/resources/get-started

You would setup a invetory file with the hosts for each org.

hosts:
  org1:
    ansible_host: ip
  org2:
    ansible_host: ip

And run the command using the ansible-playbook command passing the .yml file with the commands that should run in each host.

- hosts: org1
  tasks:
  - name: Create channel 1
    command: docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp/users/Admin@org1.example.com/msp" peer0.org1.example.com peer channel create -o orderer.example.com:7050 -c mychannel -f /etc/hyperledger/configtx/channel.tx

You can use this Scripts to join the network of two orgs. However, you may need K8S or docker Swarm or extra host for peers to connect each other

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM