Badr Bellaj
4 min readDec 2, 2023

--

How to Deploy Node.js Chaincode on an Offline Machine

There are instances where you may encounter the need to deploy a Node.js chaincode on a peer that lacks internet connectivity. In a development environment, the peer utilizes fabric-nodeenv as its runtime, which is delivered as a Docker image pointing to the npm package registry. Without an internet connection, the chaincode installation process will fail.

To overcome this challenge, we can establish a workaround by employing an npm proxy on a separate machine and redirecting fabric-nodeenv to it (you can alternatively think running chaincode as CCaaS). One effective tool for this purpose is Verdaccio.

Follow these steps:

  1. Begin by pulling the Verdaccio Docker image:
docker pull verdaccio/verdaccio:4
  1. Create a folder named “verdaccio” (e.g., /appli/verdaccio).
  2. Inside the folder, create a Docker Compose file.

In the designated folder, create a Docker Compose file (docker-compose.yml) with the following content:

version: '3.1'
services:
verdaccio:
image: verdaccio/verdaccio
container_name: "verdaccio"
networks:
- node-network
environment:
- VERDACCIO_PORT=4873
ports:
- "4873:4873"
volumes:
- "./storage:/verdaccio/storage"
- "./conf:/verdaccio/conf"
- "./plugins:/verdaccio/plugins"
networks:
node-network:
driver: bridge

Afterward, create the “storage” and “plugins” directories, which will be mounted as Docker volumes:

mkdir storage
mkdir plugins

Following this, create the file ./conf/config.yaml with the specified content.

Afterwards, create the file ./conf/config.yaml with the provided content:

storage: /verdaccio/storage
plugins: /verdaccio/plugins
web:
title: Verdaccio
auth:
htpasswd:
file: ./htpasswd
uplinks:
npmjs:
url: https://registry.npmjs.org/
packages:
'@*/*':
access: $all
publish: $authenticated
unpublish: $authenticated
proxy: npmjs
'**':
access: $all
publish: $authenticated
unpublish: $authenticated
proxy: npmjs
middlewares:
audit:
enabled: true
logs:
- {type: stdout, format: pretty, level: http}

Ensure that the folder structure resembles the following:

- your_parent_folder/
- verdaccio/
- conf/
- config.yaml
- storage/
- plugins/

To run Verdaccio, execute the following command:

docker-compose up -d

After the successful execution, visit the following URL in your browser: http://machine_ip:4873

You should see the Verdaccio default configuration page. Note that while Verdaccio’s default configuration uses the htpasswd authentication plugin, it supports various other plugins such as LDAP, Bitbucket, Gitlab, GitHub, and more.

Let’s proceed by creating a new user using the following commands:

npm adduser --registry http://localhost:4873

Since Verdaccio runs as a non-root user (uid=10001) inside the container, it’s important to ensure that the mount directory is assigned to the correct user if you use a bind mount to override the default. In the example provided, run the following command:

sudo chown -R 10001:65533 /path/for/verdaccio

This step is crucial to prevent permission errors during runtime.

Next, on a machine, clone the docker/fabric-nodeenv repository:

git clone https://github.com/hyperledger/fabric-chaincode-node.git

Navigate into the docker/fabric-nodeenv directory, modify the Dockerfile, and add the following line to adjust the npmjs registry source:

RUN npm config set registry http://your_verdaccio_ip:4873/

Make sure to replace your_verdaccio_ip with the actual IP address of your Verdaccio instance.

With these steps completed, you’ve set up a new user, addressed permission considerations, and configured the npm registry source for the fabric-nodeenv Docker image.

Great! To summarize, after making the necessary modifications to the Dockerfile, you can build the Docker image with the following command:

docker build -t hyperledger/fabric-nodeenv:2.5 .

Ensure you include the dot at the end of the command. Wait for the build process to complete, and then check the list of Docker images to confirm that the new image has been created:

docker images

Once the image is created, update the peer’s configuration file core.yaml to specify the name of the image along with its tag. Normally, you would keep the tag unchanged:

With these steps completed, you can share the image with your offline VM and deploy your chaincode.

To test the connection to the chaincode container, use the following commands:

docker exec -it dev-your-chaincode-container-name sh

Inside the container, execute the following command to check the npm registry source:

/ # npm config get registry

Ensure that the registry source matches the address of your Verdaccio instance (e.g., http://youIp:4873).

These steps should help you deploy and test your chaincode on an offline machine successfully.

Alternatively you can install npm dependencies then install the generated packaged tar as described in https://github.com/hyperledger/fabric/blob/111cff51600d26d4b4b05f52825da11e7629e971/docs/source/deploy_chaincode.md

--

--