1

I want to create s sh script but it stops when "docker exec -it cli bash" is executed and do not go to the next line. How to run the other commands on root?

root@ee3abae377df:/opt/gopath/src/github.com/hyperledger/fabric/peer# Stops here and i am not able do execute the next command

docker exec -it cli bash
export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
export CORE_PEER_ADDRESS=peer0.org1.example.com:7051
export CORE_PEER_LOCALMSPID="Org1MSP"
export 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
export CHANNEL_NAME=mychannel
jeremysprofile
  • 10,028
  • 4
  • 33
  • 53
HectorCode
  • 205
  • 2
  • 11
  • Running a bunch of `export` commands in an interactive session makes no sense as such; they will be lost as soon as you terminate that interactive session. – tripleee Sep 11 '19 at 04:02

3 Answers3

2

docker exec -it creates an interactive docker container. It starts a new shell in your current terminal. This blocks the rest of the commands from running until you kill or exit the container. The rest of the commands you have will in fact be run, once you exit the container.

I assume this is not desired. You should look into creating an entrypoint in a custom Dockerfile for your docker container where you execute the remainder of the commands in your script.

If you haven't created a Dockerfile before, the getting started guide from Docker is a pretty good intro to everything docker-related.

jeremysprofile
  • 10,028
  • 4
  • 33
  • 53
1

Executing commands using CLI on any peer

  • As per the description you have given, cli is pointing to peer0.org1.example.com:7051. (Please check your docker-compose file and there would be one service with container name as "cli" and image as hyperledger/fabric-tools)
  • When you are executing "docker exec -it cli bash", you are entering into container of peer0.org1. It provides an interactive shell to you.

Let us consider we want to install chaincode using cli on Pee1 Org1, create one test.sh file and write the following cammand inside test.sh file

docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_ADDRESS=peer1.org1.example.com:7051" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp" cli peer chaincode install -n cc_name -v cc_version -p cc_path

Here we are passing environment varibles CORE_PEER_ADDRESS and CORE_PEER_MSPCONFIGPATH. Then just execute test.sh file, the chaincode will be installed on peer1 provided container is already running and environment varibles are correct. (please provide correct chaincode path, name, and version)

PAVAN
  • 771
  • 4
  • 14
0

You should leverage the option given by @jeremysprofile. But still, if you want to achieve it through shell script then you might want to write a expect script along with shell script or might merge both the scripts in one. It should look something like this :

#!/usr/bin/expect -f

expect "*root*" {

send -- "<your command>\r"
send -- "exit \r"
}

Expect is a program that "talks" to other interactive programs according to a script. Following the script, Expect knows what can be expected from a program and what the correct response should be.

Here is the Linux man Page.

Pacifist
  • 3,025
  • 2
  • 12
  • 20