更新 docker.sh

This commit is contained in:
lc 2025-02-13 02:51:36 +00:00
parent 1c01e3e18e
commit 5489de1e70

180
docker.sh
View File

@ -1,91 +1,91 @@
#!/bin/bash #!/bin/bash
# 默认使用国内镜像 # 默认使用国内镜像
USE_CHINA_MIRROR="yes" USE_CHINA_MIRROR="yes"
# 询问用户是否使用国内镜像 # 询问用户是否使用国内镜像
read -p "是否使用国内镜像站点安装 Docker 和 Docker Compose[yes/no] (默认: yes): " USE_CHINA_MIRROR read -p "是否使用国内镜像站点安装 Docker 和 Docker Compose? [yes/no] (默认: yes): " USE_CHINA_MIRROR
USE_CHINA_MIRROR=${USE_CHINA_MIRROR:-yes} USE_CHINA_MIRROR=${USE_CHINA_MIRROR:-yes}
# 设置镜像站点 # 设置镜像站点
if [ "$USE_CHINA_MIRROR" = "yes" ]; then if [ "$USE_CHINA_MIRROR" = "yes" ]; then
DOCKER_MIRROR="https://mirrors.aliyun.com/docker-ce" DOCKER_MIRROR="https://mirrors.aliyun.com/docker-ce"
DOCKER_COMPOSE_URL="https://get.daocloud.io/docker/compose/releases/download" DOCKER_COMPOSE_URL="https://get.daocloud.io/docker/compose/releases/download"
echo "使用国内镜像站点安装 Docker 和 Docker Compose。" echo "使用国内镜像站点安装 Docker 和 Docker Compose。"
else else
DOCKER_MIRROR="https://download.docker.com" DOCKER_MIRROR="https://download.docker.com"
DOCKER_COMPOSE_URL="https://github.com/docker/compose/releases/download" DOCKER_COMPOSE_URL="https://github.com/docker/compose/releases/download"
echo "使用官方站点安装 Docker 和 Docker Compose。" echo "使用官方站点安装 Docker 和 Docker Compose。"
fi fi
# 方法 1固定版本号根据需要手动更新 # 方法 1固定版本号根据需要手动更新
DOCKER_COMPOSE_VERSION="v2.23.0" DOCKER_COMPOSE_VERSION="v2.23.0"
# 方法 2从阿里云镜像站点获取版本号无需访问 GitHub # 方法 2从阿里云镜像站点获取版本号无需访问 GitHub
if [ "$USE_CHINA_MIRROR" = "yes" ]; then if [ "$USE_CHINA_MIRROR" = "yes" ]; then
DOCKER_COMPOSE_VERSION=$(curl -s https://mirrors.aliyun.com/docker-ce/linux/static/stable-x86_64/ | grep -oP 'docker-compose-\K.*(?=-linux-x86_64)' | tail -1) DOCKER_COMPOSE_VERSION=$(curl -s https://mirrors.aliyun.com/docker-ce/linux/static/stable-x86_64/ | grep -oP 'docker-compose-\K.*(?=-linux-x86_64)' | tail -1)
fi fi
# 检测操作系统类型 # 检测操作系统类型
if [ -f /etc/os-release ]; then if [ -f /etc/os-release ]; then
. /etc/os-release . /etc/os-release
OS=$ID OS=$ID
VERSION=$VERSION_ID VERSION=$VERSION_ID
else else
echo "无法检测操作系统类型" echo "无法检测操作系统类型"
exit 1 exit 1
fi fi
# 安装 Docker # 安装 Docker
install_docker() { install_docker() {
case $OS in case $OS in
"debian") "debian")
sudo apt-get update sudo apt-get update
sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common
curl -fsSL ${DOCKER_MIRROR}/linux/debian/gpg | sudo apt-key add - curl -fsSL ${DOCKER_MIRROR}/linux/debian/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] ${DOCKER_MIRROR}/linux/debian $(lsb_release -cs) stable" sudo add-apt-repository "deb [arch=amd64] ${DOCKER_MIRROR}/linux/debian $(lsb_release -cs) stable"
sudo apt-get update sudo apt-get update
sudo apt-get install -y docker-ce docker-ce-cli containerd.io sudo apt-get install -y docker-ce docker-ce-cli containerd.io
;; ;;
"ubuntu") "ubuntu")
sudo apt-get update sudo apt-get update
sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common
curl -fsSL ${DOCKER_MIRROR}/linux/ubuntu/gpg | sudo apt-key add - curl -fsSL ${DOCKER_MIRROR}/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] ${DOCKER_MIRROR}/linux/ubuntu $(lsb_release -cs) stable" sudo add-apt-repository "deb [arch=amd64] ${DOCKER_MIRROR}/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get update sudo apt-get update
sudo apt-get install -y docker-ce docker-ce-cli containerd.io sudo apt-get install -y docker-ce docker-ce-cli containerd.io
;; ;;
"centos"|"rocky"|"almalinux") "centos"|"rocky"|"almalinux")
sudo yum install -y yum-utils sudo yum install -y yum-utils
sudo yum-config-manager --add-repo ${DOCKER_MIRROR}/linux/centos/docker-ce.repo sudo yum-config-manager --add-repo ${DOCKER_MIRROR}/linux/centos/docker-ce.repo
sudo yum install -y docker-ce docker-ce-cli containerd.io sudo yum install -y docker-ce docker-ce-cli containerd.io
;; ;;
*) *)
echo "不支持的操作系统: $OS" echo "不支持的操作系统: $OS"
exit 1 exit 1
;; ;;
esac esac
# 启动并启用 Docker 服务 # 启动并启用 Docker 服务
sudo systemctl start docker sudo systemctl start docker
sudo systemctl enable docker sudo systemctl enable docker
} }
# 安装 Docker Compose # 安装 Docker Compose
install_docker_compose() { install_docker_compose() {
echo "正在安装 Docker Compose 版本: $DOCKER_COMPOSE_VERSION" echo "正在安装 Docker Compose 版本: $DOCKER_COMPOSE_VERSION"
sudo curl -L "${DOCKER_COMPOSE_URL}/${DOCKER_COMPOSE_VERSION}/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose sudo curl -L "${DOCKER_COMPOSE_URL}/${DOCKER_COMPOSE_VERSION}/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose sudo chmod +x /usr/local/bin/docker-compose
} }
# 主函数 # 主函数
main() { main() {
install_docker install_docker
install_docker_compose install_docker_compose
echo "Docker 和 Docker Compose 安装完成!" echo "Docker 和 Docker Compose 安装完成!"
docker --version docker --version
docker-compose --version docker-compose --version
} }
main main