websoft9/install/install_docker.sh

213 lines
8.1 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
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
2023-11-09 02:36:20 +00:00
if [ "$mirror" = "Official" ]; then
mirror=""
fi
2024-06-15 07:37:23 +00:00
curl -fsSL --max-time 5 https://get.docker.com -o /dev/null
if [ $? -eq 0 ]; then
# For redhat family
if [[ -f /etc/redhat-release ]] || command -v amazon-linux-extras >/dev/null 2>&1; then
# For CentOS, Fedora, or RHEL(only s390x)
if [[ $(cat /etc/redhat-release) =~ "Red Hat" ]] && [[ $(uname -m) == "s390x" ]] || [[ $(cat /etc/redhat-release) =~ "CentOS" ]] || [[ $(cat /etc/redhat-release) =~ "Fedora" ]]; then
curl -fsSL https://get.docker.com -o get-docker.sh
timeout $timeout sh get-docker.sh --channel stable --mirror $mirror
else
# For other distributions(Redhat and Rocky linux ...)
dnf --version >/dev/null 2>&1
dnf_status=$?
yum --version >/dev/null 2>&1
yum_status=$?
2023-11-09 02:36:20 +00:00
2024-06-15 07:37:23 +00:00
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
if command -v amazon-linux-extras >/dev/null 2>&1; then
wget -O /etc/yum.repos.d/CentOS7-Base.repo https://websoft9.github.io/stackhub/apps/roles/role_common/files/CentOS7-Base.repo
sudo sed -i "s/\$releasever/7/g" /etc/yum.repos.d/docker-ce.repo
timeout $timeout sudo yum install $docker_packages --disablerepo='amzn2-extras,amzn2-core' -y
else
timeout $timeout sudo yum install $docker_packages -y
fi
else
echo "None of the required package managers are installed."
fi
fi
fi
2024-08-07 07:11:33 +00:00
if [[ $(cat /etc/os-release) =~ "Amazon Linux" ]]; then
sudo dnf install docker -y
sudo systemctl enable docker
sudo systemctl start docker
sudo mkdir -p /usr/local/lib/docker/cli-plugins/
sudo curl -SL "https://github.com/docker/compose/releases/latest/download/docker-compose-linux-$(uname -m)" -o /usr/local/lib/docker/cli-plugins/docker-compose
sudo chmod +x /usr/local/lib/docker/cli-plugins/docker-compose
fi
2024-06-15 07:37:23 +00:00
# For Ubuntu, Debian, or Raspbian
if type apt >/dev/null 2>&1; then
# 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-08 05:52:53 +00:00
timeout $timeout sh get-docker.sh --channel stable --mirror $mirror
2024-06-15 07:37:23 +00:00
fi
else
echo "can not install by installation script, use special way to install docker"
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 install yum-utils -y
sudo dnf config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
sudo dnf install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
elif [ $yum_status -eq 0 ]; then
sudo yum install yum-utils -y
sudo yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
sudo yum install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
elif [ $apt_status -eq 0 ]; then
sudo apt-get update
sudo apt-get install -y ca-certificates curl gnupg lsb-release
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
2023-09-20 11:37:12 +00:00
else
2024-06-15 07:37:23 +00:00
echo "don't know package tool"
2023-09-20 11:37:12 +00:00
fi
2024-06-15 07:37:23 +00:00
sudo systemctl enable docker
sudo systemctl restart docker
2023-11-08 05:52:53 +00:00
fi
2024-06-15 07:37:23 +00:00
2023-09-20 11:37:12 +00:00
}
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-12-07 09:18:32 +00:00
if [ -z "$execute_mode" ] || [ "$execute_mode" = "install" ]; then
Start_Docker
fi