Add Service Labels and Cleanup Using Standard Tools

Add a service label to all containers launch by compose.
This allows us to filter on the label for removing running
containers.

Use filtering for querying running containers and
images to make sure we only target the containers
and images we want to remove.

Signed-off-by: Brett Logan <lindluni@github.com>
This commit is contained in:
Brett Logan 2021-02-11 16:53:01 -05:00 committed by denyeart
parent ae81e8b296
commit 37513c3c2b
7 changed files with 29 additions and 14 deletions

View file

@ -10,9 +10,10 @@ networks:
name: fabric_test name: fabric_test
services: services:
ca_org3: ca_org3:
image: hyperledger/fabric-ca:latest image: hyperledger/fabric-ca:latest
labels:
service: hyperledger-fabric
environment: environment:
- FABRIC_CA_HOME=/etc/hyperledger/fabric-ca-server - FABRIC_CA_HOME=/etc/hyperledger/fabric-ca-server
- FABRIC_CA_SERVER_CA_NAME=ca-org3 - FABRIC_CA_SERVER_CA_NAME=ca-org3

View file

@ -13,6 +13,8 @@ services:
couchdb4: couchdb4:
container_name: couchdb4 container_name: couchdb4
image: couchdb:3.1.1 image: couchdb:3.1.1
labels:
service: hyperledger-fabric
# Populate the COUCHDB_USER and COUCHDB_PASSWORD to set an admin user and password # Populate the COUCHDB_USER and COUCHDB_PASSWORD to set an admin user and password
# for CouchDB. This will prevent CouchDB from operating in an "Admin Party" mode. # for CouchDB. This will prevent CouchDB from operating in an "Admin Party" mode.
environment: environment:

View file

@ -17,6 +17,8 @@ services:
peer0.org3.example.com: peer0.org3.example.com:
container_name: peer0.org3.example.com container_name: peer0.org3.example.com
image: hyperledger/fabric-peer:latest image: hyperledger/fabric-peer:latest
labels:
service: hyperledger-fabric
environment: environment:
#Generic peer variables #Generic peer variables
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock

View file

@ -13,6 +13,8 @@ services:
ca_org1: ca_org1:
image: hyperledger/fabric-ca:latest image: hyperledger/fabric-ca:latest
labels:
service: hyperledger-fabric
environment: environment:
- FABRIC_CA_HOME=/etc/hyperledger/fabric-ca-server - FABRIC_CA_HOME=/etc/hyperledger/fabric-ca-server
- FABRIC_CA_SERVER_CA_NAME=ca-org1 - FABRIC_CA_SERVER_CA_NAME=ca-org1
@ -29,6 +31,8 @@ services:
ca_org2: ca_org2:
image: hyperledger/fabric-ca:latest image: hyperledger/fabric-ca:latest
labels:
service: hyperledger-fabric
environment: environment:
- FABRIC_CA_HOME=/etc/hyperledger/fabric-ca-server - FABRIC_CA_HOME=/etc/hyperledger/fabric-ca-server
- FABRIC_CA_SERVER_CA_NAME=ca-org2 - FABRIC_CA_SERVER_CA_NAME=ca-org2
@ -45,6 +49,8 @@ services:
ca_orderer: ca_orderer:
image: hyperledger/fabric-ca:latest image: hyperledger/fabric-ca:latest
labels:
service: hyperledger-fabric
environment: environment:
- FABRIC_CA_HOME=/etc/hyperledger/fabric-ca-server - FABRIC_CA_HOME=/etc/hyperledger/fabric-ca-server
- FABRIC_CA_SERVER_CA_NAME=ca-orderer - FABRIC_CA_SERVER_CA_NAME=ca-orderer

View file

@ -13,6 +13,8 @@ services:
couchdb0: couchdb0:
container_name: couchdb0 container_name: couchdb0
image: couchdb:3.1.1 image: couchdb:3.1.1
labels:
service: hyperledger-fabric
# Populate the COUCHDB_USER and COUCHDB_PASSWORD to set an admin user and password # Populate the COUCHDB_USER and COUCHDB_PASSWORD to set an admin user and password
# for CouchDB. This will prevent CouchDB from operating in an "Admin Party" mode. # for CouchDB. This will prevent CouchDB from operating in an "Admin Party" mode.
environment: environment:
@ -40,6 +42,8 @@ services:
couchdb1: couchdb1:
container_name: couchdb1 container_name: couchdb1
image: couchdb:3.1.1 image: couchdb:3.1.1
labels:
service: hyperledger-fabric
# Populate the COUCHDB_USER and COUCHDB_PASSWORD to set an admin user and password # Populate the COUCHDB_USER and COUCHDB_PASSWORD to set an admin user and password
# for CouchDB. This will prevent CouchDB from operating in an "Admin Party" mode. # for CouchDB. This will prevent CouchDB from operating in an "Admin Party" mode.
environment: environment:

View file

@ -19,6 +19,8 @@ services:
orderer.example.com: orderer.example.com:
container_name: orderer.example.com container_name: orderer.example.com
image: hyperledger/fabric-orderer:latest image: hyperledger/fabric-orderer:latest
labels:
service: hyperledger-fabric
environment: environment:
- FABRIC_LOGGING_SPEC=INFO - FABRIC_LOGGING_SPEC=INFO
- ORDERER_GENERAL_LISTENADDRESS=0.0.0.0 - ORDERER_GENERAL_LISTENADDRESS=0.0.0.0
@ -59,6 +61,8 @@ services:
peer0.org1.example.com: peer0.org1.example.com:
container_name: peer0.org1.example.com container_name: peer0.org1.example.com
image: hyperledger/fabric-peer:latest image: hyperledger/fabric-peer:latest
labels:
service: hyperledger-fabric
environment: environment:
#Generic peer variables #Generic peer variables
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
@ -94,6 +98,8 @@ services:
peer0.org2.example.com: peer0.org2.example.com:
container_name: peer0.org2.example.com container_name: peer0.org2.example.com
image: hyperledger/fabric-peer:latest image: hyperledger/fabric-peer:latest
labels:
service: hyperledger-fabric
environment: environment:
#Generic peer variables #Generic peer variables
- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
@ -129,6 +135,8 @@ services:
cli: cli:
container_name: cli container_name: cli
image: hyperledger/fabric-tools:latest image: hyperledger/fabric-tools:latest
labels:
service: hyperledger-fabric
tty: true tty: true
stdin_open: true stdin_open: true
environment: environment:

View file

@ -19,27 +19,19 @@ export VERBOSE=false
. scripts/utils.sh . scripts/utils.sh
# Obtain CONTAINER_IDS and remove them # Obtain CONTAINER_IDS and remove them
# TODO Might want to make this optional - could clear other containers
# This function is called when you bring a network down # This function is called when you bring a network down
function clearContainers() { function clearContainers() {
CONTAINER_IDS=$(docker ps -a | awk '($2 ~ /dev-peer.*/) {print $1}') infoln "Removing remaining containers"
if [ -z "$CONTAINER_IDS" -o "$CONTAINER_IDS" == " " ]; then docker rm -f $(docker ps -aq --filter label=service=hyperledger-fabric) 2>/dev/null || true
infoln "No containers available for deletion" docker rm -f $(docker ps -aq --filter name='dev-peer*') 2>/dev/null || true
else
docker rm -f $CONTAINER_IDS
fi
} }
# Delete any images that were generated as a part of this setup # Delete any images that were generated as a part of this setup
# specifically the following images are often left behind: # specifically the following images are often left behind:
# This function is called when you bring the network down # This function is called when you bring the network down
function removeUnwantedImages() { function removeUnwantedImages() {
DOCKER_IMAGE_IDS=$(docker images | awk '($1 ~ /dev-peer.*/) {print $3}') infoln "Removing generated chaincode docker images"
if [ -z "$DOCKER_IMAGE_IDS" -o "$DOCKER_IMAGE_IDS" == " " ]; then docker image rm -f $(docker images -aq --filter reference='dev-peer*') 2>/dev/null || true
infoln "No images available for deletion"
else
docker rmi -f $DOCKER_IMAGE_IDS
fi
} }
# Versions of fabric known not to work with the test network # Versions of fabric known not to work with the test network