mycelium-cni/docker-demo.sh
Scott Yeager f71a82f785 Init
2025-06-19 22:17:52 -07:00

67 lines
2.2 KiB
Bash

#!/bin/bash
# This script is meant to be run in a fresh Ubuntu VM. It demonstrates how to
# assign Mycelium addresses to Docker containers, from a single Mycelium
# instance running on the host. When the script completes, there will be a
# container running with a Mycelium address assigned to it.
# Install prerequisites
apt update && apt install -y wget iproute2
# Install Docker
wget -qO- get.docker.com | bash
# Install Mycelium
MYCELIUM_VERSION="v0.5.6"
wget https://github.com/threefoldtech/mycelium/releases/download/${MYCELIUM_VERSION}/mycelium-x86_64-unknown-linux-musl.tar.gz
tar xf mycelium-x86_64-unknown-linux-musl.tar.gz
cp mycelium /usr/local/bin/
# Create Mycelium service
cat > /etc/systemd/system/mycelium.service <<EOF
[Unit]
Description=Mycelium Network
After=network.target
[Service]
ExecStart=/usr/local/bin/mycelium --peers tcp://188.40.132.242:9651 tcp://136.243.47.186:9651 tcp://185.69.166.7:9651 tcp://65.21.231.58:9651 tcp://209.159.146.190:9651
Restart=always
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now mycelium
# Symlink the docker net namespaces so we can use `ip netns` with them
ln -Ts /var/run/docker/netns /var/run/netns
# Start container
docker run -d --name mycelium-container ubuntu sleep infinity
# Get container PID and network namespace
NETNS_ID=$(docker inspect -f '{{.NetworkSettings.SandboxKey}}' mycelium-container | awk -F'/' '{print $NF}')
# Create veth pair
ip link add veth-host type veth peer name veth-container
ip link set veth-host up
# Move veth-container to container's netns
ip link set veth-container netns $NETNS_ID
ip netns exec $NETNS_ID ip link set veth-container up
# Configure IPv6 addresses
MYCELIUM_IP6=$(ip -6 a show dev mycelium | awk '/inet6.*global/ {print $2}' | cut -d'/' -f1 | cut -d: -f1-4)
ip netns exec "$NETNS_ID" ip a add "${MYCELIUM_IP6}::1/64" dev veth-container
# Make sure forwarding is enabled for ipv6
sysctl -w net.ipv6.conf.all.forwarding=1
# Add routes
VETH_HOST_LL=$(ip -6 a show dev veth-host | awk '/inet6.*link/ {print $2}' | cut -d'/' -f1)
ip netns exec "$NETNS_ID" ip r add 400::/7 via "$VETH_HOST_LL" dev veth-container
ip r add "${MYCELIUM_IP6}::1/128" dev veth-host
echo "Setup complete. Container should now have Mycelium connectivity."