Update docker-network.sh.

This commit is contained in:
2025-07-12 18:09:57 +00:00
parent f9f3997f7e
commit ea6b35c0df
2 changed files with 77 additions and 65 deletions

View File

@@ -1,65 +0,0 @@
#!/bin/bash
set -euo pipefail
# -------------------------------
# Prompt user for subnet base
# -------------------------------
read -rp "Enter the second octet for your Docker network (e.g., 100 for 10.100.0.0/16): " octet
# Validate that it's a number between 1 and 254
if ! [[ "$octet" =~ ^[0-9]{1,3}$ ]] || ((octet < 1 || octet > 254)); then
echo "Invalid input. Must be a number between 1 and 254."
exit 1
fi
# -------------------------------
# Define values
# -------------------------------
DOCKER_DAEMON_FILE="/etc/docker/daemon.json"
ADDRESS_POOL_BASE="10.${octet}.0.0/16"
ADDRESS_POOL_SIZE=24
# NETWORK_NAME="bridge_default"
NETWORK_SUBNET="10.${octet}.1.0/24"
# -------------------------------
# Replace daemon.json
# -------------------------------
echo "Writing Docker daemon.json with base $ADDRESS_POOL_BASE..."
sudo mkdir -p /etc/docker
cat <<EOF | sudo tee "$DOCKER_DAEMON_FILE" > /dev/null
{
"default-address-pools": [
{
"base": "10.${octet}.0.0/16",
"size": 24
}
]
}
EOF
echo "Docker config updated at $DOCKER_DAEMON_FILE"
# -------------------------------
# Delete and recreate network
# -------------------------------
# if docker network inspect "$NETWORK_NAME" >/dev/null 2>&1; then
# echo "Network '$NETWORK_NAME' exists. Deleting it..."
# docker network rm "$NETWORK_NAME"
# fi
# echo "Creating network '$NETWORK_NAME' with subnet $NETWORK_SUBNET..."
# docker network create \
# --driver=bridge \
# --subnet="$NETWORK_SUBNET" \
# "$NETWORK_NAME"
# echo "✅ Done. You may need to restart Docker to apply default address pool changes:"
echo " sudo systemctl restart docker"

77
docker-network.sh. Normal file
View File

@@ -0,0 +1,77 @@
#!/bin/bash
set -euo pipefail
DOCKER_CONFIG="/etc/docker/daemon.json"
JQ=$(command -v jq || true)
if [[ -z "$JQ" ]]; then
echo "❌ jq is required but not installed."
exit 1
fi
# Step 1: Check if default-address-pools exists and extract current second octet
DEFAULT_OCTET=""
if [[ -f "$DOCKER_CONFIG" ]] && grep -q '"default-address-pools"' "$DOCKER_CONFIG"; then
BASE=$(jq -r '.["default-address-pools"][0].base' "$DOCKER_CONFIG")
if [[ "$BASE" =~ ^10\.([0-9]+)\. ]]; then
DEFAULT_OCTET="${BASH_REMATCH[1]}"
echo "🔍 Found existing default-address-pools base: $BASE"
fi
fi
# Step 2: Ask for second octet (suggest default if available)
if [[ -n "$DEFAULT_OCTET" ]]; then
read -rp "Use existing second octet ($DEFAULT_OCTET)? [Y/n]: " confirm
confirm=${confirm:-Y}
if [[ "$confirm" =~ ^[Nn]$ ]]; then
read -rp "Enter new second octet (e.g. 102): " OCTET
else
OCTET="$DEFAULT_OCTET"
fi
else
read -rp "Enter second octet for Docker address pool (e.g. 102): " OCTET
fi
# Validate input
if ! [[ "$OCTET" =~ ^[0-9]+$ ]] || ((OCTET < 1 || OCTET > 254)); then
echo "❌ Invalid octet: $OCTET"
exit 1
fi
# Step 3: Write daemon.json (overwrite or create)
echo "💾 Writing Docker daemon config with base: 10.${OCTET}.0.0/16"
sudo mkdir -p /etc/docker
sudo tee "$DOCKER_CONFIG" > /dev/null <<EOF
{
"default-address-pools": [
{
"base": "10.${OCTET}.0.0/16",
"size": 24
}
]
}
EOF
# Step 4: Restart Docker
echo "🔄 Restarting Docker..."
sudo systemctl restart docker
# Step 5: Create standard networks
declare -A networks
networks[internet]="10.${OCTET}.100.0/24"
networks[pangolin_edge]="10.${OCTET}.200.0/24"
for name in "${!networks[@]}"; do
subnet="${networks[$name]}"
if docker network inspect "$name" >/dev/null 2>&1; then
echo "✅ Network '$name' already exists."
else
echo " Creating network '$name' with subnet $subnet"
docker network create \
--driver=bridge \
--subnet="$subnet" \
"$name"
fi
done
echo "✅ Done. Docker daemon configured and standard networks created."