detect os to install curl if missing, then install docker

This commit is contained in:
2025-01-30 20:21:43 +09:00
parent 39521b8276
commit 5605cac812
+51 -9
View File
@@ -2,21 +2,62 @@
# Function to install Docker # Function to install Docker
install_docker() { install_docker() {
# Detect OS
OS=""
if [ -f /etc/os-release ]; then
. /etc/os-release
OS=$ID
fi
# Function to install curl if missing
install_curl() {
if ! command -v curl &> /dev/null; then
echo "curl is not installed. Installing curl..."
case "$OS" in
ubuntu|debian)
sudo apt update && sudo apt install -y curl
;;
centos|rhel|rocky|alma)
sudo yum install -y curl
;;
fedora)
sudo dnf install -y curl
;;
arch)
sudo pacman -Sy --noconfirm curl
;;
*)
echo "Unsupported OS: $OS. Please install curl manually."
exit 1
;;
esac
fi
}
# Install curl if missing
install_curl
# Check if Docker is already installed
if command -v docker &> /dev/null; then
echo "Docker is already installed."
return
fi
# Prompt user to install Docker
echo -n "Docker is not installed. Do you want to install Docker? (y/n): " echo -n "Docker is not installed. Do you want to install Docker? (y/n): "
read answer read answer
if [ "$answer" != "${answer#[Yy]}" ]; then if [[ "$answer" =~ ^[Yy]$ ]]; then
echo "Installing Docker..." echo "Installing Docker..."
curl -fsSL https://get.docker.com -o get-docker.sh > /dev/null 2>&1 curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh > /dev/null 2>&1 sudo sh get-docker.sh
rm get-docker.sh > /dev/null 2>&1 rm get-docker.sh
# Add current user to docker group # Add the current user to the docker group
sudo usermod -aG docker $USER sudo usermod -aG docker $USER
#newgrp docker
echo "Docker has been installed successfully!" echo "Docker has been installed successfully!"
echo "You may need to log out and back in for group changes to take effect."
else else
echo "Docker installation skipped. Please install Docker manually to use this script." echo "Docker installation skipped. Please install Docker manually to use this script."
exit 1 exit 1
@@ -24,6 +65,7 @@ install_docker() {
} }
check_container_running() { check_container_running() {
docker ps --format '{{.Names}}' | grep -q "^$1$" docker ps --format '{{.Names}}' | grep -q "^$1$"
return $? return $?