|
| 1 | +# Fabric Starter Kit |
| 2 | + |
| 3 | +This section describes how to set up a self-contained environment for |
| 4 | +application development with the Hyperledger fabric. The setup |
| 5 | +uses **Docker** to provide a controlled environment with all the necessary |
| 6 | +Hyperledger fabric components to support a Node.js application built with |
| 7 | +the fabric's Node.js SDK, and chaincode written in Go. |
| 8 | + |
| 9 | +There are three Docker images that, when run, will provide a basic |
| 10 | +network environment. There is an image to run a single `peer`, one to run |
| 11 | +the `membersrvc`, and one to run both your Node.js application and your |
| 12 | +chaincode. See [Application Developer's Overview](../nodeSDK/app-overview.md) on how the |
| 13 | +components running within the containers will communicate. |
| 14 | + |
| 15 | +The starter kit comes with a sample Node.js application ready to execute and |
| 16 | +sample chaincode. The starter kit will be running in chaincode developer mode. |
| 17 | +In this mode, the chaincode is built and started prior to the application |
| 18 | +making a call to deploy it. |
| 19 | + |
| 20 | +**Note:** The deployment of chaincode in network mode requires that the |
| 21 | +Hyperledger fabric Node.js SDK has access to the chaincode source code and all |
| 22 | +of its dependencies, in order to properly build a deploy request. It also |
| 23 | +requires that the `peer` have access to the Docker daemon to be able to build |
| 24 | +and deploy the new Docker image that will run the chaincode. *This is a more |
| 25 | +complicated configuration and not suitable to an introduction to the |
| 26 | +Hyperledger fabric.* We recommend first running in chaincode development mode. |
| 27 | + |
| 28 | +## Further exploration |
| 29 | + |
| 30 | +If you wish, there are a number of chaincode examples near by. |
| 31 | +``` |
| 32 | + cd ../../chaincode |
| 33 | +``` |
| 34 | +## Getting started |
| 35 | + |
| 36 | +**Note:** This sample was prepared using Docker for Mac 1.12.0 |
| 37 | + |
| 38 | +* Prerequisite software to install: |
| 39 | + |
| 40 | + * [Docker](https://www.docker.com/products/overview) |
| 41 | + * docker-compose (may be packaged with Docker) |
| 42 | + |
| 43 | +* Copy our [docker-compose.yml](https://raw.githubusercontent.com/hyperledger/fabric/master/examples/sdk/node/docker-compose.yml) file to a local directory: |
| 44 | + |
| 45 | +``` |
| 46 | + curl -o docker-compose.yml https://raw.githubusercontent.com/hyperledger/fabric/master/examples/sdk/node/docker-compose.yml |
| 47 | +``` |
| 48 | + The docker-compose environment uses three Docker images. Two are published to |
| 49 | + DockerHub. However, with the third, we provide you the source to build your own, |
| 50 | + so that you can customize it to inject your application code for development. The following [Dockerfile](https://raw.githubusercontent.com/hyperledger/fabric/master/examples/sdk/node/Dockerfile) |
| 51 | + is used to build the base **fabric-starter-kit** image and may be used as |
| 52 | + a starting point for your own customizations. |
| 53 | + |
| 54 | +``` |
| 55 | + curl -o Dockerfile https://raw.githubusercontent.com/hyperledger/fabric/master/examples/sdk/node/Dockerfile |
| 56 | + docker build -t hyperledger/fabric-starter-kit:latest . |
| 57 | +``` |
| 58 | + |
| 59 | +* Start the fabric network environment using docker-compose. From a terminal |
| 60 | +session that has the working directory of where the above *docker-compose.yml* |
| 61 | +is located, execute one of following `docker-compose` commands. |
| 62 | + |
| 63 | + * to run as detached containers: |
| 64 | + |
| 65 | +``` |
| 66 | + docker-compose up -d |
| 67 | +``` |
| 68 | + **note:** to see the logs for the `peer` container use the |
| 69 | + `docker logs peer` command |
| 70 | + |
| 71 | + * to run in the foreground and see the log output in the current terminal |
| 72 | + session: |
| 73 | + |
| 74 | +``` |
| 75 | + docker-compose up |
| 76 | +``` |
| 77 | + |
| 78 | + Both commands will start three Docker containers. To view the container |
| 79 | + status use the `docker ps` command. The first time this is run, the Docker |
| 80 | + images will be downloaded. This may take 10 minutes or more depending on the |
| 81 | + network connections of the system running the command. |
| 82 | + |
| 83 | +``` |
| 84 | + docker ps |
| 85 | +``` |
| 86 | + You should see something similar to the following: |
| 87 | + |
| 88 | +``` |
| 89 | + CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES |
| 90 | + bb01a2fa96ef hyperledger/fabric-starter-kit "sh -c 'sleep 20; /op" About a minute ago Up 59 seconds starter |
| 91 | + ec7572e65f12 hyperledger/fabric-peer "sh -c 'sleep 10; pee" About a minute ago Up About a minute peer |
| 92 | + 118ef6da1709 hyperledger/fabric-membersrvc "membersrvc" About a minute ago Up About a minute membersrvc |
| 93 | +``` |
| 94 | + |
| 95 | +* Start a terminal session in the **starter** container. This is where the |
| 96 | +Node.js application is located. |
| 97 | + |
| 98 | + **note:** Be sure to wait 20 seconds after starting the network using the |
| 99 | + `docker-compose up` command before executing the following command to allow |
| 100 | + the network to initialize: |
| 101 | + |
| 102 | +``` |
| 103 | + docker exec -it starter /bin/bash |
| 104 | +``` |
| 105 | + |
| 106 | +* From the terminal session in the **starter** container execute the standalone |
| 107 | +Node.js application. The Docker terminal session should be in the working |
| 108 | +directory of the sample application called **app.js** (*/opt/gopath/src/github.com/hyperledger/fabric/examples/sdk/node*). Execute |
| 109 | +the following Node.js command to run the application: |
| 110 | + |
| 111 | +``` |
| 112 | + node app |
| 113 | +``` |
| 114 | + In another terminal session on the host you can view the logs for the peer |
| 115 | + by executing the following command (not in the docker shell above, in a new |
| 116 | + terminal session of the real system): |
| 117 | + |
| 118 | +``` |
| 119 | + docker logs peer |
| 120 | +``` |
| 121 | + |
| 122 | +* If you wish to run your own Node.js application using the pre-built Docker |
| 123 | +images: |
| 124 | + * use the directories in the `volumes` tag under **starter** in the |
| 125 | + `docker-compose.yml` file as a place to store your programs from the host |
| 126 | + system into the docker container. The first path is the top level system |
| 127 | + (host system) and the second is created in the Docker container. If you wish |
| 128 | + to use a host location that is not under the `/Users` directory (`~` is |
| 129 | + under `/Users') then you must add that to the Docker file sharing |
| 130 | + under Docker preferences. |
| 131 | + |
| 132 | +```yaml |
| 133 | + volumes: |
| 134 | + - ~/mytest:/user/mytest |
| 135 | +``` |
| 136 | + * copy or create and edit your application in the `~/mytest` directory as |
| 137 | + stated in the `docker-compose.yml` `volumes` tag under **starter** container. |
| 138 | + * run npm to install Hyperledger fabric Node.js SDK in the `mytest` directory: |
| 139 | + |
| 140 | +``` |
| 141 | + npm install /opt/gopath/src/github.com/hyperledger/fabric/sdk/node |
| 142 | +``` |
| 143 | + * run the application from within the **starter** Docker container using the |
| 144 | + following commands: |
| 145 | +
|
| 146 | +``` |
| 147 | + docker exec -it starter /bin/bash |
| 148 | +``` |
| 149 | + once in the shell, and assuming your Node.js application is called `app.js`: |
| 150 | +
|
| 151 | +``` |
| 152 | + cd /user/mytest |
| 153 | + node app |
| 154 | +``` |
| 155 | +* To shutdown the environment, execute the following **docker-compose** command |
| 156 | +in the directory where the *docker-compose.yml* is located. Any changes you made |
| 157 | +to the sample application or deployment of a chaincode will be lost. Only |
| 158 | +changes made to the shared area defined in the 'volumes' tag of the **starter** |
| 159 | +container will persist. This will shutdown each of the containers and remove |
| 160 | +the containers from Docker: |
| 161 | +
|
| 162 | +``` |
| 163 | + docker-compose down |
| 164 | +``` |
| 165 | + or if you wish to keep your changes and just stop the containers, which will |
| 166 | + be restarted on the next `up` command: |
| 167 | +
|
| 168 | +``` |
| 169 | + docker-compose kill |
| 170 | +``` |
0 commit comments