diff --git a/docker-network.sh b/docker-network.sh deleted file mode 100644 index 6adef63..0000000 --- a/docker-network.sh +++ /dev/null @@ -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 < /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" diff --git a/docker-network.sh. b/docker-network.sh. new file mode 100644 index 0000000..0db4a78 --- /dev/null +++ b/docker-network.sh. @@ -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 </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."