#!/bin/bash # 获取版本号从环境变量 CONTAINERD_VERSION=${CONTAINERD_VERSION:-1.6.8} NERDCTL_VERSION=${NERDCTL_VERSION:-1.7.6} CNI_VERSION=${CNI_VERSION:-1.1.1} BUILDKIT_VERSION=${BUILDKIT_VERSION:-0.14.1} # Docker Compose 安装路径 DOCKER_COMPOSE_PATH="/usr/local/bin/docker-compose" LOGFILE="$(pwd)/install_log.txt" install_docker() { echo "正在检查 Docker 是否已安装..." | tee -a $LOGFILE if ! [ -x "$(command -v docker)" ]; then echo "Docker 未安装。正在安装 Docker..." | tee -a $LOGFILE curl -fsSL https://get.docker.com -o get-docker.sh | tee -a $LOGFILE sh get-docker.sh | tee -a $LOGFILE echo "Docker 已安装。" | tee -a $LOGFILE else echo "Docker 已安装。" | tee -a $LOGFILE fi } install_docker_compose() { install_docker echo "正在安装 Docker Compose..." | tee -a $LOGFILE # 检查并安装 jq if ! [ -x "$(command -v jq)" ]; then echo "安装 jq..." | tee -a $LOGFILE sudo apt-get update && sudo apt-get install -y jq | tee -a $LOGFILE fi # 检查并安装 curl if ! [ -x "$(command -v curl)" ]; then echo "安装 curl..." | tee -a $LOGFILE sudo apt-get update && sudo apt-get install -y curl | tee -a $LOGFILE fi # 使用 GitHub API 获取最新版本 LATEST_RELEASE=$(curl -s https://api.github.com/repos/docker/compose/releases/latest | jq -r .tag_name) if [ -z "$LATEST_RELEASE" ]; then echo "无法获取 Docker Compose 的最新版本。" | tee -a $LOGFILE exit 1 fi # 更新 Docker Compose 安装 URL 以使用最新版本 DOCKER_COMPOSE_URL="https://github.com/docker/compose/releases/download/${LATEST_RELEASE}/docker-compose-$(uname -s)-$(uname -m)" sudo curl -L $DOCKER_COMPOSE_URL -o $DOCKER_COMPOSE_PATH | tee -a $LOGFILE sudo chmod +x $DOCKER_COMPOSE_PATH | tee -a $LOGFILE echo "Docker Compose ${LATEST_RELEASE} 已安装。" | tee -a $LOGFILE } uninstall_docker_compose() { echo "正在卸载 Docker Compose..." | tee -a $LOGFILE sudo rm -f $DOCKER_COMPOSE_PATH | tee -a $LOGFILE echo "Docker Compose 已卸载。" | tee -a $LOGFILE } install_containerd() { echo "正在安装 containerd..." | tee -a $LOGFILE sudo apt-get update | tee -a $LOGFILE sudo apt-get install -y containerd | tee -a $LOGFILE sudo mkdir -p /etc/containerd containerd config default | sudo tee /etc/containerd/config.toml | tee -a $LOGFILE sudo systemctl restart containerd | tee -a $LOGFILE } configure_containerd() { echo "正在配置 containerd 使用 nvidia-container-runtime..." | tee -a $LOGFILE sudo tee -a /etc/containerd/config.toml > /dev/null < /dev/null <