Skip to content

Instantly share code, notes, and snippets.

@legout
Last active March 3, 2025 14:07
Show Gist options
  • Save legout/b4af97a57d879e367f8bc3a42062dfcf to your computer and use it in GitHub Desktop.
Save legout/b4af97a57d879e367f8bc3a42062dfcf to your computer and use it in GitHub Desktop.
Docker Swarm reset
#!/bin/bash
# Safe Docker Swarm Reset Script
# This script safely disables Docker Swarm while preserving SSH connectivity
# and avoiding risky network operations
# Minimal error handling - continue on errors to prevent hanging
set +e
echo "======= Safe Docker Swarm Reset ======="
echo "This script will preserve SSH connectivity throughout the process"
# Step 1: Check if we have Docker
if ! command -v docker &> /dev/null; then
echo "Docker is not installed. Nothing to reset."
exit 0
fi
# Step 2: Verify SSH is not running in Docker
SSH_IN_DOCKER=$(ps aux | grep sshd | grep -v grep | grep docker)
if [ -n "$SSH_IN_DOCKER" ]; then
echo "WARNING: SSH appears to be running in Docker. Aborting for safety."
echo "Please use a different method to reset Docker."
exit 1
fi
# Step 3: Check swarm status without failing if Docker is down
echo "Checking swarm status..."
SWARM_STATUS=$(docker info 2>/dev/null | grep "Swarm: active" || echo "")
if [ -n "$SWARM_STATUS" ]; then
echo "This node is part of a swarm. Attempting to leave safely..."
docker swarm leave --force || echo "Could not leave swarm cleanly. Continuing anyway."
else
echo "This node is not part of a swarm or Docker is not responding."
fi
# Step 4: Save current SSH port from sshd config
SSH_PORT=$(grep "^Port " /etc/ssh/sshd_config | awk '{print $2}' || echo "22")
echo "Detected SSH on port $SSH_PORT - will ensure this remains accessible"
# Step 5: Save current iptables rules for SSH
echo "Backing up SSH firewall rules..."
SSH_RULES=$(sudo iptables-save | grep $SSH_PORT)
# Step 6: Safely stop Docker
echo "Stopping Docker service..."
sudo systemctl stop docker || echo "Warning: Could not stop Docker cleanly"
sleep 2
# Step 7: Remove only swarm-specific files, not network configs
echo "Removing swarm configuration files..."
sudo rm -rf /var/lib/docker/swarm/ 2>/dev/null || echo "Could not remove swarm directory"
# Step 8: Ensure SSH access is maintained in firewall
echo "Ensuring SSH access is maintained..."
if [ -n "$SSH_RULES" ]; then
echo "Restoring SSH firewall rules if needed..."
sudo iptables -A INPUT -p tcp --dport $SSH_PORT -j ACCEPT || echo "Could not add SSH rule"
fi
# Step 9: Start Docker again
echo "Starting Docker service..."
sudo systemctl start docker || echo "Warning: Could not start Docker"
# Step 10: Verify Docker is running
echo "Waiting for Docker to initialize..."
sleep 5
if docker info &>/dev/null; then
echo "Docker is running correctly."
docker info | grep -A 5 "Swarm" || echo "Could not retrieve Swarm info"
else
echo "WARNING: Docker appears to be having issues, but your SSH access should remain intact."
fi
echo "======= Reset Complete ======="
echo "Docker swarm has been reset safely. SSH connectivity should be preserved."
echo ""
echo "After setting up the WireGuard VPN:"
echo "1. On the manager node: docker swarm init --advertise-addr <VPN_IP>"
echo "2. On worker nodes: Use the join token provided by the manager"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment