How to restart the fabric composer without losing the existing data?

The startFabric.sh under fabric-dev-servers (formerly fabric-tools) does more than just start the Fabric - it removes existing Fabric Containers and recreates new Containers from the Docker Images. The impact of this is that you lose all your data and your Business Network from the Fabric. All Business Network Cards except PeerAdmin@hlfv1 are now useless.

If you want to stop and start your Fabric after you have created it, retaining your Business Network and data follow these commands:

  • Change to the directory where the docker-compose.yml file is (e.g. /home/<user>/fabric-tools/fabric-scripts/hlfv1/composer)
  • Run docker-compose stop to top the Fabric Containers
  • Run docker-compose start to restart where you left off.

It is necessary to be in the correct folder before using the docker-compose command.

VERSION UPDATE

With Composer v0.20.x (for Fabric 1.2) the folders' names/version have changed:

/home/<user>/fabric-dev-servers/fabric-scripts/hlfv12/composer

With Composer v0.19.x (older, for Fabric v1.1) the folders names/versions have changed:

/home/<user>/fabric-dev-servers/fabric-scripts/hlfv11/composer


Edit you startFabric.sh inside fabric-dev-server/fabric-scripts/hlfv11 like below

#!/bin/bash

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
# http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Exit on first error, print all commands.
set -e

Usage() {
    echo ""
    echo "Usage: ./startFabric.sh [-d || --dev]"
    echo ""
    echo "Options:"
    echo -e "\t-d or --dev: (Optional) enable fabric development mode"
    echo ""
    echo "Example: ./startFabric.sh"
    echo ""
    exit 1
}

Parse_Arguments() {
    while [ $# -gt 0 ]; do
        case $1 in
            --help)
                HELPINFO=true
                ;;
            --dev | -d)
                FABRIC_DEV_MODE=true
                ;;
        esac
        shift
    done
}

Parse_Arguments $@

if [ "${HELPINFO}" == "true" ]; then
    Usage
fi

#Detect architecture
ARCH=`uname -m`

# Grab the current directory
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

if [ "${FABRIC_DEV_MODE}" == "true" ]; then
    DOCKER_FILE="${DIR}"/composer/docker-compose-dev.yml
else
    DOCKER_FILE="${DIR}"/composer/docker-compose.yml
fi

ARCH=$ARCH docker-compose -f "${DOCKER_FILE}" stop
ARCH=$ARCH docker-compose -f "${DOCKER_FILE}" up -d

# wait for Hyperledger Fabric to start
# incase of errors when running later commands, issue export FABRIC_START_TIMEOUT=<larger number>
echo "sleeping for ${FABRIC_START_TIMEOUT} seconds to wait for fabric to complete start up"
sleep ${FABRIC_START_TIMEOUT}

# Create the channel
#docker exec peer0.org1.example.com peer channel create -o orderer.example.com:7050 -c composerchannel -f /etc/hyperledger/configtx/composer-channel.tx

# Join peer0.org1.example.com to the channel.
#docker exec -e "CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp/users/[email protected]/msp" peer0.org1.example.com peer channel join -b composerchannel.block

if [ "${FABRIC_DEV_MODE}" == "true" ]; then
    echo "Fabric Network started in chaincode development mode"
fi

To expand on R Thatcher's solution, if you have access to a bash shell, then you can put this script in your fabric-dev-server folder. It will restart the network as in the steps above, without losing your data. Simply cd into the correct directory and runs docker-compose stop & docker-compose start. Also works fine to run this after a restart to bring the network back.

#!/bin/bash
cd ./fabric-scripts/hlfv1/composer
docker-compose stop
docker-compose start

Seems trivial, and doesn't add much to the discussion but it saves time.