By Ashutosh Meher, Developer Evangelist at R3
Have you tried to run a Corda node on Docker? How about running an entire Corda network on Docker containers? It would be so cool to be able to spin up separate docker containers for each Corda node.
Corda also has an Official Docker Image, so we don’t really have to build one. And to run multiple docker containers, it’s often a good idea to use Docker Compose. So, we will be using both of them in this article.
New to Corda? A great way to start with Corda is to take a look at one of our online bootcamp webinars. The recording for one of them is available here: https://www.youtube.com/watch?v=tVE1rKbFA3g&t=6349s
You may also consider joining us for one of our in-person or live virtual bootcamps. Keep an eye on the link below to know what events are coming up: https://corda.net/blockchain-bootcamp/
So without further delay let’s get started. First, we need to create some artifacts (node certificates and network-parameters) for the Corda network. We will use a local bootstrap network (for simplicity) so that we can easily generate these artifacts using the network-bootstrapper tool.
Download network-bootstrapper tool here: https://software.r3.com/artifactory/corda/net/corda/corda-tools-network-bootstrapper/4.4/corda-tools-network-bootstrapper-4.4-sources.jar
To bootstrap a test-network we need the node.conf files for each node. The network-bootstrapper tool would be able to read the node.conf files to generate the required artifacts for the test-network.
Below is a node.conf for PartyA.
The others are going to be the same with an obvious update to the myLegalName field.
Note that we enabled ssh by configuring the port 2222. We will need this to connect to the node as it won’t start up with a CRaSH shell as it usually does in the host machine.
Some Stuff for Docker
Make sure you change the host in the p2pAddress. Note that partya is the name of the docker container which I will define later using Docker Compose.
We will be using the default bridge network mode in docker. Container to container communication can be done using the container names as hostname.
The official Corda docker image exposes the ports 10200, 10201and 10202. Do not change them in the node.conf file.
Make sure you listen to 0.0.0.0 not 127.0.0.1 or localhost , since those will be localhost for the container.
Running the Bootstrapper
Now once we have the node.conf ready for all our nodes we can run the network-bootstrapper tool. To run the bootstrapper tool place the bootstrapper jar alongside the node.conf files and run the bootstrapper jar using the java -jar <bootstrapper-jar-file-name> command. It should generate a lot of files as shown below.
We are not going to need all the files that got generated, so let’s do some cleanup. All we need is the node.conf file, the node certificates and the network-paramaters.
Below is how the nodes folders look after the removal of all unnecessary files. Notice that we have moved the network-parameters file to a shared folder.
Note that all nodes shared the samenetwork-parameters, so don’t worry about which one to move to the shared folder. I also deleted bootstrapper tool and the node.conf files from the base folder to keep it clean.
We need one final thing, a CorDapp. No, I am not going to build one here, but I can use one from the Corda-samples repo.
But where do we place this CorDapp? Just create another cordapps folder inside shared and drop the CorDapps in. We will configure the Corda docker containers to look for CorDapps in this folder.
Now we have all we need to define the docker container. As mentioned earlier we are going to use Docker Compose. For each container, we need to define a service in the docker-compose.yaml file. Let’s see how it looks for Party A.
Let me try to explain each of the sections:
- image: It’s the official Corda docker image to be pulled from docker hub.
- container_name: Pretty self-explanatory.
- ports: Remember each Corda docker image exposes rpc port at10201. We are forwarding it to 10006 on the host machine. The ssh port 2222 is forwarded to 2001.
- volumes: Here we mount some volumes on the container. We need the node.conf, certificates, cordapps, and network-parameters. Additionally, I am also mapping persistence (h2 database file) and logs to the host machine.
Note that we also mount a folder node-infos from the shared directory. As the node spins-up inside the container it places its node-info file in this shared folder, so that its discoverable by other nodes in the network. So this serves as a network map.
That’s should be it. It should be the same for the other node as well. Here’s the final docker-compose.yaml file.
Running the container
It’s time we spin up the containers. Just use the command docker-compose up from the root directory.
And that should spin-up 4 Corda nodes in separate docker containers.
To shutdown use: docker-compose down
Connecting to the Node
We do not get the CRaSH shell as we normally get when running the node directly in the host machine. So, how do we connect to the node? There are two ways to that:
- Using a client. (You don’t have to build one — Try node-explorer)
- Using ssh: ssh -p [portNumber] [host] -l [user]
The port number should be the forwarded port number on the host machine and the host is localhost. So if you wish to ssh to partya use:
ssh -p 2001 localhost -l user1
And the type and password and you should be in the CRaSH shell of the node.
Note: You may have to use -o StrictHostKeyChecking=no option if ssh fail due to host key checking.
And, that’s it! Thanks for reading!
Want to learn more about building awesome blockchain applications on Corda? Be sure to visit https://corda.net, check out our community page to learn how to connect with other Corda developers, and sign up for one of our newsletters for the latest updates.
— Ashutosh Meher is a Developer Evangelist at R3, an enterprise blockchain software firm working with a global ecosystem of more than 350 participants across multiple industries from both the private and public sectors to develop on Corda, its open-source blockchain platform, and Corda Enterprise, a commercial version of Corda for enterprise usage.
Follow Ashutosh on Twitter here.