.local-bin/scripts/docker_check.sh
2026-02-09 13:52:33 -05:00

166 lines
5.2 KiB
Bash
Executable file

#!/usr/bin/env bash
# Detect platform
PLATFORM="$(uname)"
# Function to detect Linux package manager
detect_linux_package_manager() {
if command -v apt-get &>/dev/null; then
echo "apt"
elif command -v yum &>/dev/null; then
echo "yum"
elif command -v dnf &>/dev/null; then
echo "dnf"
elif command -v pacman &>/dev/null; then
echo "pacman"
else
echo "unknown"
fi
}
# Function to install Docker on Linux
install_docker_linux() {
local pkg_manager
pkg_manager=$(detect_linux_package_manager)
echo "Docker is not installed."
read -rp "Do you want to install Docker using $pkg_manager? (y/n): " choice
if [ "$choice" == "y" ]; then
case $pkg_manager in
"apt")
sudo apt-get update
sudo apt-get install -y docker.io docker-compose
sudo systemctl enable --now docker
sudo usermod -aG docker "$USER"
echo "Docker has been installed. Please log out and back in, then restart this script."
;;
"yum"|"dnf")
sudo "$pkg_manager" install -y docker docker-compose
sudo systemctl enable --now docker
sudo usermod -aG docker "$USER"
echo "Docker has been installed. Please log out and back in, then restart this script."
;;
"pacman")
sudo pacman -S docker docker-compose
sudo systemctl enable --now docker
sudo usermod -aG docker "$USER"
echo "Docker has been installed. Please log out and back in, then restart this script."
;;
*)
echo "Unsupported package manager. Please install Docker manually."
exit 1
;;
esac
exit 0
else
echo "Docker is required to run this script. Exiting."
exit 1
fi
}
# Function to start Docker if not running
start_docker() {
if [ "$PLATFORM" == "Darwin" ]; then
echo "Docker is not running. Starting Docker..."
open --background -a Docker
sleep 3 # Wait for Docker to start
elif [ "$PLATFORM" == "Linux" ]; then
echo "Docker is not running. Starting Docker..."
sudo systemctl start docker
sleep 3
else
echo "Unsupported platform: $PLATFORM"
exit 1
fi
}
# Check if Docker is installed
install_docker() {
if [ "$PLATFORM" == "Darwin" ]; then
echo "Docker is not installed."
read -rp "Do you want to install Docker using Homebrew? (y/n): " choice
if [ "$choice" == "y" ]; then
brew install --cask docker
echo "Docker has been installed. Please restart this script."
exit 0
else
echo "Docker is required to run this script. Exiting."
exit 1
fi
elif [ "$PLATFORM" == "Linux" ]; then
install_docker_linux
else
echo "Unsupported platform: $PLATFORM"
exit 1
fi
}
# Check if docker-compose is installed
install_docker_compose() {
if [ "$PLATFORM" == "Darwin" ]; then
echo "docker-compose is not installed."
read -rp "Do you want to install docker-compose using Homebrew? (y/n): " choice
if [ "$choice" == "y" ]; then
brew install docker-compose
echo "Docker Compose has been installed. Please restart this script."
exit 0
else
echo "Docker Compose is required to run this script. Exiting."
exit 1
fi
elif [ "$PLATFORM" == "Linux" ]; then
local pkg_manager
pkg_manager=$(detect_linux_package_manager)
echo "docker-compose is not installed."
read -rp "Do you want to install docker-compose using $pkg_manager? (y/n): " choice
if [ "$choice" == "y" ]; then
case $pkg_manager in
"apt")
sudo apt-get update
sudo apt-get install -y docker-compose
;;
"yum"|"dnf")
sudo "$pkg_manager" install -y docker-compose
;;
"pacman")
sudo pacman -S docker-compose
;;
*)
echo "Unsupported package manager. Please install docker-compose manually."
exit 1
;;
esac
echo "Docker Compose has been installed. Please restart this script."
exit 0
else
echo "Docker Compose is required to run this script. Exiting."
exit 1
fi
else
echo "Unsupported platform: $PLATFORM"
exit 1
fi
}
if ! command -v docker &>/dev/null; then
install_docker
fi
# Check if Docker is running
if [ "$(docker info >/dev/null 2>&1 && echo "running")" != "running" ]; then
start_docker
fi
# Check if docker-compose is called
if [ "$1" == "docker-compose" ]; then
# If docker-compose is called, check if Docker is running again
if [ "$(docker info >/dev/null 2>&1 && echo "running")" != "running" ]; then
if ! command -v docker-compose &>/dev/null; then
start_docker
fi
fi
fi
# Run the provided command
"$@"