Setting Up IPFS as a Private P2P Network Using Docker
Configuring a Secure and Decentralized IPFS Peer-to-Peer Network with Docker
Step 1: Generate a swarm.key
A swarm.key
is required for a private IPFS network. Below are two methods to generate it:
Method 1: Using openssl
echo "/key/swarm/psk/1.0.0/" > swarm.key
echo "/base16/" >> swarm.key
openssl rand -hex 32 >> swarm.key
Method 2: Using go-ipfs-swarm-key-gen
git clone https://github.com/Kubuxu/go-ipfs-swarm-key-gen
cd go-ipfs-swarm-key-gen/ipfs-swarm-key-gen/
go run main.go > /path/to/your/swarm.key
Step 2: Create Directories for Each Node
To enable data persistence, create separate directories for each node:
Node 1
mkdir -p /home/akramwired/ipfs_node1_staging /home/akramwired/ipfs_node1_data
Node 2
mkdir -p /home/akramwired/ipfs_node2_staging /home/akramwired/ipfs_node2_data
Replace the directory paths with your desired locations.
Step 3: Set Environment Variables
Define environment variables for each node:
Node 1
export ipfs_node1_staging=/home/akramwired/ipfs_node1_staging
export ipfs_node1_data=/home/akramwired/ipfs_node1_data
Node 2
export ipfs_node2_staging=/home/akramwired/ipfs_node2_staging
export ipfs_node2_data=/home/akramwired/ipfs_node2_data
Step 4: Run IPFS Nodes Using Docker
Run the following Docker command to start Node 1:
Node 1
sudo docker run -d --name ipfs_node1 \
-v $ipfs_node1_staging:/export \
-v $ipfs_node1_data:/data/ipfs \
-v /home/akramwired/swarm.key:/data/ipfs/swarm.key \
-p 4001:4001 \
-p 4001:4001/udp \
-p 127.0.0.1:8080:8080 \
-p 127.0.0.1:5001:5001 \
ipfs/kubo:latest
Run the following Docker command to start Node 2:
Node 2
sudo docker run -d --name ipfs_node2 \
-v $ipfs_node2_staging:/export \
-v $ipfs_node2_data:/data/ipfs \
-v /home/akramwired/swarm.key:/data/ipfs/swarm.key \
-p 4002:4001 \
-p 4002:4001/udp \
-p 127.0.0.1:8081:8080 \
-p 127.0.0.1:5002:5001 \
ipfs/kubo:latest
Ensure that the swarm.key
path is correct for your system.
Step 5: Add Bootstrap Peers
To connect the nodes, retrieve the Peer ID and addresses of Node 1:
sudo docker exec ipfs_node1 ipfs id
Example output:
{
"ID": "12D3KooWDp4tAykGeaBYAF5mvpErer5z1M523mU5Xw8BpKw5xn4W",
"Addresses": [
"/ip4/127.0.0.1/tcp/4001/p2p/12D3KooWDp4tAykGeaBYAF5mvpErer5z1M523mU5Xw8BpKw5xn4W",
"/ip4/172.17.0.2/tcp/4001/p2p/12D3KooWDp4tAykGeaBYAF5mvpErer5z1M523mU5Xw8BpKw5xn4W",
"/ip6/::1/tcp/4001/p2p/12D3KooWDp4tAykGeaBYAF5mvpErer5z1M523mU5Xw8BpKw5xn4W"
]
}
Add Node 1 as a bootstrap peer on Node 2:
sudo docker exec -it ipfs_node2 sh
Inside the Node 2 container:
ipfs bootstrap add /ip4/127.0.0.1/tcp/4001/p2p/12D3KooWDp4tAykGeaBYAF5mvpErer5z1M523mU5Xw8BpKw5xn4W
ipfs bootstrap add /ip4/172.17.0.2/tcp/4001/p2p/12D3KooWDp4tAykGeaBYAF5mvpErer5z1M523mU5Xw8BpKw5xn4W
ipfs bootstrap add /ip6/::1/tcp/4001/p2p/12D3KooWDp4tAykGeaBYAF5mvpErer5z1M523mU5Xw8BpKw5xn4W
Verify the connection:
ipfs swarm peers
Step 6: Add Files to Node 1
Log into Node 1 and create a sample file:
sudo docker exec -it ipfs_node1 sh
Inside the Node 1 container:
echo "Hello, IPFS! From Node 1" > example.txt
ipfs add example.txt
Example output:
added QmTzQ1dQCUCWo3W5Jcf5eJECmMWw5EwbxyuF2ZQEShA4S1 example.txt
Step 7: Retrieve the File from Node 2
Log into Node 2:
sudo docker exec -it ipfs_node2 sh
Inside the Node 2 container, retrieve the file using the Content Identifier (CID):
ipfs get QmTzQ1dQCUCWo3W5Jcf5eJECmMWw5EwbxyuF2ZQEShA4S1
To display the contents directly:
ipfs cat QmTzQ1dQCUCWo3W5Jcf5eJECmMWw5EwbxyuF2ZQEShA4S1
To persist the file on both nodes, pin it on Node 2:
ipfs pin add QmTzQ1dQCUCWo3W5Jcf5eJECmMWw5EwbxyuF2ZQEShA4S1
Following these steps, you can set up and run a private IPFS network using Docker for secure peer-to-peer file sharing.