Skip to content

Instantly share code, notes, and snippets.

@PrashantBhatasana
Created November 20, 2025 04:48
Show Gist options
  • Select an option

  • Save PrashantBhatasana/c876c5e864fe1392b25f49c7bc0749f5 to your computer and use it in GitHub Desktop.

Select an option

Save PrashantBhatasana/c876c5e864fe1392b25f49c7bc0749f5 to your computer and use it in GitHub Desktop.
Easy script to install docker on ubuntu machine.
#!/bin/bash
# Ensure script is run as root
if [ "$EUID" -ne 0 ]; then
echo "❌ Please run this script as root (use sudo)."
exit 1
fi
export DEBIAN_FRONTEND=noninteractive
# === Argument Parsing ===
FORCE_INSTALL=false
FORCE_UNINSTALL=false
for arg in "$@"; do
case "$arg" in
--force)
FORCE_INSTALL=true
;;
--uninstall)
FORCE_UNINSTALL=true
;;
*)
echo "❌ Unknown argument: $arg"
echo "Usage: $0 [--force] [--uninstall]"
exit 1
;;
esac
done
# === Docker Uninstall Function ===
uninstall_docker() {
echo "πŸ—‘οΈ Removing Docker..."
apt-get remove -yqq docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
rm -rf /var/lib/docker /etc/docker
echo "βœ… Docker has been completely removed."
exit 0
}
# === Fast Uninstall Mode ===
if [ "$FORCE_UNINSTALL" = true ]; then
uninstall_docker
fi
# === Interactive Menu ===
if [ "$FORCE_INSTALL" = false ]; then
while true; do
echo "πŸ”§ Docker Installation Menu:"
echo "1️⃣ Install Docker"
echo "2️⃣ Uninstall Docker"
echo "3️⃣ Exit"
read -p "πŸ‘‰ Enter your choice: " choice
case $choice in
1) break ;;
2) uninstall_docker ;;
3) echo "🚫 Exiting..."; exit 0 ;;
*) echo "❌ Invalid input! Please select 1, 2, or 3." ;;
esac
done
else
echo "βœ… Force install mode enabled. Proceeding with Docker installation..."
fi
# === Docker Installation ===
echo "πŸ”„ Updating system packages..."
apt-get update -qq
echo "πŸ“¦ Installing required dependencies..."
apt-get install -yqq --no-install-recommends ca-certificates curl gnupg
echo "πŸ”‘ Setting up Docker GPG key..."
install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
chmod a+r /etc/apt/keyrings/docker.asc
echo "πŸ“¦ Adding Docker repository..."
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
echo "πŸ”„ Updating package list (Docker repo added)..."
apt-get update -qq
echo "🐳 Installing Docker (CE, CLI, Containerd, Buildx, Compose)..."
apt-get install -yqq --no-install-recommends docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
echo "πŸ”„ Enabling and starting Docker service..."
systemctl enable --now docker
echo "πŸ‘€ Adding current user to the Docker group..."
usermod -aG docker $SUDO_USER
echo "πŸ”” Please log out and log back in for Docker group changes to take effect."
echo "πŸš€ Verifying Docker installation..."
docker run --rm hello-world
echo "πŸŽ‰ Docker installation complete! Run 'docker -v' to verify."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment