websoft9/install/install_docker.sh

134 lines
3.7 KiB
Bash
Raw Normal View History

2023-09-19 18:12:37 +08:00
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
2023-09-20 19:37:12 +08:00
# Install and Upgade Docker for mosts of Linux
# This script is intended from https://get.docker.com and add below:
#
2023-09-28 17:31:16 +08:00
# - install or update Docker
2023-09-20 19:37:12 +08:00
# - support Redhat, CentOS-Stream, OracleLinux, AmazonLinux
#
# 1. download the script
#
2023-09-28 17:31:16 +08:00
# $ curl -fsSL https://websoft9.github.io/websoft9/install/install_docker.sh -o install_docker.sh
2023-09-20 19:37:12 +08:00
#
# 2. verify the script's content
#
2023-09-28 17:31:16 +08:00
# $ cat install_docker.sh
2023-09-20 19:37:12 +08:00
#
# 3. run the script with --dry-run to verify the steps it executes
#
2023-09-28 17:31:16 +08:00
# $ sh install_docker.sh --dry-run
2023-09-20 19:37:12 +08:00
#
# 4. run the script either as root, or using sudo to perform the installation.
#
2023-09-28 17:31:16 +08:00
# $ sudo sh install_docker.sh
2023-09-19 17:44:20 +08:00
2023-10-18 13:03:06 +08:00
# it must export, otherwise Rocky Linux cannot used at yum command
export docker_packages="docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin"
2023-09-26 18:21:45 +08:00
echo_prefix_docker=$'\n[Docker] - '
2023-09-20 19:37:12 +08:00
2023-09-26 18:21:45 +08:00
docker_exist() {
# 检查 `docker` 命令是否存在
if ! command -v docker &> /dev/null; then
2023-09-28 17:31:16 +08:00
echo "docker command not exist"
2023-09-26 18:21:45 +08:00
return 1
fi
2023-09-28 17:31:16 +08:00
# 检查 Docker 服务是否正在运行
systemctl is-active docker.service &> /dev/null
2023-09-26 18:21:45 +08:00
if [ $? -ne 0 ]; then
2023-09-28 17:31:16 +08:00
echo "Docker service is not running, trying to start it..."
systemctl start docker.service
if [ $? -ne 0 ]; then
echo "Failed to start Docker service."
return 1
fi
2023-09-26 18:21:45 +08:00
fi
return 0
}
2023-09-20 19:37:12 +08:00
Install_Docker(){
2023-11-02 17:06:38 +08:00
2023-09-26 18:21:45 +08:00
echo "$echo_prefix_docker Installing Docker for your system"
2023-09-20 19:37:12 +08:00
# For redhat family
if [[ -f /etc/redhat-release ]]; then
# For CentOS, Fedora, or RHEL(only s390x)
if [[ $(cat /etc/redhat-release) =~ "RHEL" ]] && [[ $(uname -m) == "s390x" ]] || [[ $(cat /etc/redhat-release) =~ "CentOS" ]] || [[ $(cat /etc/redhat-release) =~ "Fedora" ]]; then
curl -fsSL https://get.docker.com -o get-docker.sh && sh get-docker.sh
else
# For other distributions
2023-10-19 16:46:24 +08:00
sudo yum install yum-utils -y > /dev/null
2023-09-20 19:37:12 +08:00
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
sudo yum install $docker_packages -y
fi
fi
# For Ubuntu, Debian, or Raspbian
2023-09-26 18:21:45 +08:00
if type apt >/dev/null 2>&1; then
2023-09-20 19:37:12 +08:00
# Wait for apt to be unlocked
curl -fsSL https://get.docker.com -o get-docker.sh && sh get-docker.sh
fi
}
Upgrade_Docker(){
2023-09-28 17:31:16 +08:00
if docker_exist; then
2023-09-26 18:21:45 +08:00
echo "$echo_prefix_docker Upgrading Docker for your system..."
2023-09-20 19:37:12 +08:00
dnf --version >/dev/null 2>&1
dnf_status=$?
yum --version >/dev/null 2>&1
yum_status=$?
apt --version >/dev/null 2>&1
apt_status=$?
if [ $dnf_status -eq 0 ]; then
sudo dnf update -y $docker_packages
elif [ $yum_status -eq 0 ]; then
sudo yum update -y $docker_packages
elif [ $apt_status -eq 0 ]; then
2023-10-19 16:46:24 +08:00
sudo apt update -y
2023-09-20 19:37:12 +08:00
sudo apt -y install --only-upgrade $docker_packages
else
echo "Docker installed, but cannot upgrade"
2023-09-19 17:44:20 +08:00
fi
else
2023-10-19 16:46:24 +08:00
max_retries=3
retry_count=0
while ((retry_count < max_retries)); do
Install_Docker
2023-11-02 17:06:38 +08:00
if ! docker_exist; then
2023-10-19 16:46:24 +08:00
echo "Installation timeout or failed, retrying..."
((retry_count++))
2023-10-19 17:11:14 +08:00
sleep 3
2023-10-19 16:46:24 +08:00
else
echo "Docker installed successfully."
exit 0
fi
done
echo "Docker Installation failed after $max_retries retries."
exit 1
2023-09-19 17:44:20 +08:00
fi
2023-09-20 19:37:12 +08:00
}
2023-09-19 17:44:20 +08:00
2023-10-06 16:45:57 +08:00
Start_Docker(){
2023-09-20 19:37:12 +08:00
# should have Docker server and Docker cli
2023-09-28 17:31:16 +08:00
if docker_exist; then
2023-10-06 16:45:57 +08:00
echo "$echo_prefix_docker Starting Docker"
2023-09-20 19:37:12 +08:00
sudo systemctl enable docker
2023-09-27 14:50:42 +08:00
sudo systemctl restart docker
2023-09-20 19:37:12 +08:00
else
2023-10-06 19:54:23 +08:00
echo "Docker not installed or start failed, exit..."
exit 1
2023-09-19 17:44:20 +08:00
fi
2023-09-20 19:37:12 +08:00
}
2023-09-19 17:44:20 +08:00
2023-10-06 20:34:28 +08:00
echo -e "\n\n-------- Docker --------"
2023-09-20 19:37:12 +08:00
Upgrade_Docker
2023-10-06 16:45:57 +08:00
Start_Docker