my/docker.sh
2025-02-13 02:50:58 +00:00

91 lines
3.2 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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