Private Data Hosting with MultiChain in Docker
This document provides a step-by-step guide to setting up a private data hosting solution using MultiChain within a Docker container.
Create the Dockerfile
Since MultiChain does not have an official image on Docker Hub, begin by creating a Dockerfile
with the following content:
FROM ubuntu:22.04
# Install dependencies
RUN apt-get update && apt-get install -y \
curl \
unzip \
&& rm -rf /var/lib/apt/lists/*
# Install text editor for convenience
RUN apt update && apt install nano -y
# Download and install MultiChain
RUN curl -O https://www.multichain.com/download/multichain-2.3.3.tar.gz && \
tar -xzf multichain-2.3.3.tar.gz && \
mv multichain-2.3.3 multichain && \
mv multichain/multichaind multichain/multichain-cli multichain/multichain-util /usr/local/bin/ && \
rm -rf multichain-2.3.3.tar.gz
# Expose ports
EXPOSE 6820 6821 6822
# Command to keep the container running
CMD ["bash"]
Build the MultiChain Image
Run the following command to build the Docker image:
sudo docker build -t multichain_image .
Create Data Persistence Directory
Create a directory on the host machine to persist MultiChain data:
mkdir -p ~/multichain-data
Run the MultiChain Container
Start the MultiChain container and mount the persistent data directory:
sudo docker run -it --name multichain-node \
-v ~/multichain-data:/root/.multichain \
-p 6820:6820 -p 6821:6821 -p 6822:6822 \
multichain_image bash
Create a MultiChain Instance
Inside the container, create the first blockchain instance called mychain
:
multichain-util create mychain
Modify Configuration for a Private Network
Edit the MultiChain configuration file to restrict access:
nano /root/.multichain/mychain/params.dat
Update the following parameters:
anyone-can-connect = false
anyone-can-send = false
anyone-can-receive = false
Start the MultiChain Daemon
multichaind mychain -daemon
Create and Subscribe to a Stream
To store data on the blockchain, create and subscribe to a stream named myStream
:
multichain-cli mychain create stream myStream true
multichain-cli mychain subscribe myStream
Store Data in MultiChain
Publish a key-value pair to store data in the stream. The value must be in hexadecimal format. In this example, we store an IPFS hash in JSON format:
multichain-cli mychain publish myStream "key1" "{\"json\":{\"ipfs_hash\":\"QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn\"}}"
Retrieve Data from the Stream
To retrieve the stored data, use the following command:
multichain-cli mychain liststreamkeyitems myStream "key1"
Stop the MultiChain Daemon and Exit
To gracefully stop the daemon and exit the container:
multichain-cli mychain stop
exit
Following this guide, you have successfully set up a private MultiChain environment inside a Docker container, configured for secure data hosting with persistent storage and stream-based data management.