Skip to content

Instantly share code, notes, and snippets.

@OnCloud125252
Last active November 4, 2024 21:20
Show Gist options
  • Save OnCloud125252/e227c02f8407c516d6e0f1dae429fbc8 to your computer and use it in GitHub Desktop.
Save OnCloud125252/e227c02f8407c516d6e0f1dae429fbc8 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Colors
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
RED='\033[0;31m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Symbols
CHECKMARK='\xE2\x9C\x94'
ROCKET='\xF0\x9F\x9A\x80'
CROSSMARK='\xE2\x9C\x98'
HOURGLASS='\xE2\x8C\x9B'
# Debug mode flag
DEBUG=false
# Check for debug option
if [ "$1" = "debug" ]; then
DEBUG=true
fi
# Function to execute command with or without output
execute_cmd() {
if [ "$DEBUG" = true ]; then
eval "$1"
else
eval "$1" > /dev/null 2>&1
fi
return $?
}
set -e
echo -e "\n${BLUE}${ROCKET} Starting Docker installation process...${NC}"
echo -e "\n${YELLOW}${HOURGLASS} Removing old Docker versions if they exist...${NC}"
for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do
if dpkg -l | grep -q "^ii $pkg "; then
execute_cmd "sudo apt remove -y $pkg" && echo -e "${GREEN}${CHECKMARK} Removed $pkg${NC}"
else
echo -e "${BLUE}${CHECKMARK} $pkg not found - skipping${NC}"
fi
done
# Add Docker's official GPG key
echo -e "\n${YELLOW}${HOURGLASS} Adding Docker's GPG key...${NC}"
execute_cmd "sudo apt update" && echo -e "${GREEN}${CHECKMARK} Package list updated${NC}"
execute_cmd "sudo apt install -y ca-certificates curl" && echo -e "${GREEN}${CHECKMARK} Certificates and curl installed${NC}"
execute_cmd "sudo install -m 0755 -d /etc/apt/keyrings" && echo -e "${GREEN}${CHECKMARK} Keyrings directory created${NC}"
execute_cmd "sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc" && echo -e "${GREEN}${CHECKMARK} GPG key downloaded${NC}"
execute_cmd "sudo chmod a+r /etc/apt/keyrings/docker.asc" && echo -e "${GREEN}${CHECKMARK} GPG key permissions set${NC}"
# Add the repository to Apt sources
echo -e "\n${YELLOW}${HOURGLASS} Adding Docker repository to Apt sources...${NC}"
if [ "$DEBUG" = true ]; then
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" | \
sudo tee /etc/apt/sources.list.d/docker.list
else
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" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
fi
echo -e "${GREEN}${CHECKMARK} Repository added${NC}"
echo -e "\n${YELLOW}${HOURGLASS} Installing Docker and Docker Compose...${NC}"
execute_cmd "sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin" && \
echo -e "${GREEN}${CHECKMARK} Docker and Docker Compose installed${NC}"
echo -e "\n${YELLOW}${HOURGLASS} Updating package list...${NC}"
execute_cmd "sudo apt update" && echo -e "${GREEN}${CHECKMARK} Package list updated${NC}"
echo -e "\n${YELLOW}${HOURGLASS} Starting Docker daemon...${NC}"
execute_cmd "sudo systemctl start docker" && echo -e "${GREEN}${CHECKMARK} Docker daemon started${NC}"
echo -e "\n${YELLOW}${HOURGLASS} Enabling Docker to start on boot...${NC}"
execute_cmd "sudo systemctl enable docker" && echo -e "${GREEN}${CHECKMARK} Docker enabled on boot${NC}"
# Final status check
if sudo systemctl is-active --quiet docker; then
echo -e "\n${GREEN}${ROCKET} Docker installation and setup completed successfully!${NC}\n"
else
echo -e "\n${RED}${CROSSMARK} Docker installation completed but service is not running. Please check the logs.${NC}\n"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment