websoft9/install/install_docker.sh

157 lines
5 KiB
Bash
Raw Normal View History

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