如何构建嵌入已有BNA的Hyperledger Composer Playground Docker镜像?
Awesome question! I’ve worked with Hyperledger Composer Playground and embedded networks before, so let’s walk through two solid, actionable solutions to get your demo up and running without relying on a real Fabric network:
Solution 1: Precreate Embedded Connection Cards & Copy to the Image
Your initial idea is spot-on—copying preconfigured embedded cards into the container’s card directory works perfectly, as long as those cards use the embedded connection type. Here’s how to execute it:
Step 1: Generate Embedded Cards Locally
First, install the Hyperledger Composer CLI to create the required cards:
npm install -g composer-cli
Then follow these steps:
- Package your Business Network into a BNA file (if you haven’t already):
composer archive create -t dir -n ./path-to-your-bna-source - Create a simple embedded connection profile file (save as
embedded-connection.json):{ "name": "embedded", "type": "embedded" } - Deploy the BNA to an embedded network and generate admin cards:
# Import the default PeerAdmin embedded card (included with Composer CLI) composer card import -f ~/.composer/cards/PeerAdmin@embedded.card || echo "PeerAdmin card already exists" # Install the BNA to the embedded network composer network install --card PeerAdmin@embedded --archiveFile your-network-name.bna # Start the network and create an admin identity composer network start --networkName your-network-name --networkVersion 1.0.0 --card PeerAdmin@embedded --networkAdmin admin --networkAdminEnrollSecret adminpw # Create and import the admin card for your network composer card create -p embedded-connection.json -u admin -n your-network-name -s adminpw composer card import -f admin@your-network-name.card
Step 2: Build the Custom Docker Image
Create a Dockerfile that copies these precreated cards into the playground image:
FROM hyperledger/composer-playground:latest # Copy your pregenerated cards into the container's composer card directory COPY ./admin@your-network-name.card /home/composer/.composer/cards/ COPY ./PeerAdmin@embedded.card /home/composer/.composer/cards/ # Ensure the composer user owns the cards directory (avoids permission issues) RUN chown -R composer:composer /home/composer/.composer/cards
Build and run the image as usual—when you launch Playground, your embedded network will be ready to use immediately.
Solution 2: Auto-Deploy BNA via Startup Script (No Precreated Cards)
If you want to avoid pregenerating cards locally, you can automate the BNA deployment directly in the container at startup using a custom script. This keeps your workflow entirely within Docker:
Step 1: Create a Startup Script
Save this as start-playground.sh (replace placeholders with your BNA details):
#!/bin/bash set -e # Create an embedded connection profile cat > /tmp/embedded-connection.json << EOF { "name": "embedded", "type": "embedded" } EOF # Ensure the default PeerAdmin embedded card is available composer card import -f /home/composer/.composer/cards/PeerAdmin@embedded.card || true # Install and start the BNA composer network install --card PeerAdmin@embedded --archiveFile /app/your-network-name.bna composer network start --networkName your-network-name --networkVersion 1.0.0 --card PeerAdmin@embedded --networkAdmin admin --networkAdminEnrollSecret adminpw # Create and import the admin card for your network composer card create -p /tmp/embedded-connection.json -u admin -n your-network-name -s adminpw composer card import -f admin@your-network-name.card # Launch the playground service exec composer-playground
Step 2: Write the Dockerfile
FROM hyperledger/composer-playground:latest # Copy your BNA file into the container COPY ./your-network-name.bna /app/ # Copy and make the startup script executable COPY ./start-playground.sh /app/ RUN chmod +x /app/start-playground.sh # Switch to the composer user (required for proper permissions) USER composer # Override the default entrypoint to run our script ENTRYPOINT ["/app/start-playground.sh"]
When you run this container, the script will automatically deploy your BNA to an embedded network and set up the necessary cards before launching Playground.
Key Things to Keep in Mind
- Version Matching: Make sure your BNA’s Hyperledger Composer version matches the version of the
hyperledger/composer-playgroundimage (use a specific tag like0.20.9instead oflatestfor stability). - Embedded Network Limits: This is a single-node, in-memory network—great for demos, but never use it for production.
- Permissions: Always ensure the
/home/composer/.composer/cardsdirectory is owned by thecomposeruser in the container to avoid access errors.
内容的提问来源于stack exchange,提问作者jpietzsch

