Centos docker安装脚本
#!/bin/bash# docker_manager.sh
# 用于管理 Docker 和 Docker Compose 的交互脚本# =======================
# 配置变量
# =======================# Docker 仓库地址
DOCKER_REPO="https://download.docker.com/linux/centos/docker-ce.repo"# =======================
# 函数定义
# =======================# 安装 Docker 和 Docker Compose
install_docker() {echo "正在安装 Docker..."# 移除旧版本sudo yum remove -y docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine# 安装必要的软件包sudo yum install -y yum-utils device-mapper-persistent-data lvm2# 添加 Docker 仓库sudo yum-config-manager --add-repo $DOCKER_REPO# 安装 Dockersudo yum install -y docker-ce docker-ce-cli containerd.io# 启动并设置开机自启sudo systemctl start dockersudo systemctl enable dockerecho "Docker 安装完成。"# 安装 Docker Composeecho "正在安装 Docker Compose..."# 获取最新版本号DOCKER_COMPOSE_LATEST=$(curl -s https://api.github.com/repos/docker/compose/releases/latest | grep tag_name | cut -d '"' -f 4)if [ -z "$DOCKER_COMPOSE_LATEST" ]; thenecho "无法获取 Docker Compose 最新版本号。请手动检查 https://github.com/docker/compose/releases"returnfi# 下载 Docker Compose 二进制文件sudo curl -L "https://github.com/docker/compose/releases/download/${DOCKER_COMPOSE_LATEST}/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose# 授予可执行权限sudo chmod +x /usr/local/bin/docker-compose# 创建软链接(可选)sudo ln -sf /usr/local/bin/docker-compose /usr/bin/docker-compose# 验证安装if command -v docker-compose &> /dev/null; thenecho "Docker Compose 安装完成,版本:$(docker-compose --version)"elseecho "Docker Compose 安装失败。"fi
}# 启动 Docker 服务
start_docker() {echo "正在启动 Docker 服务..."sudo systemctl start dockersudo systemctl enable dockerecho "Docker 服务已启动。"
}# 重启 Docker 服务
restart_docker() {echo "正在重启 Docker 服务..."sudo systemctl restart dockerif [ $? -eq 0 ]; thenecho "Docker 服务已成功重启。"elseecho "Docker 服务重启失败,请检查日志。"fi
}# 停止 Docker 服务
stop_docker() {echo "正在停止 Docker 服务..."sudo systemctl stop dockerif [ $? -eq 0 ]; thenecho "Docker 服务已停止。"elseecho "Docker 服务停止失败,请检查日志。"fi
}# 卸载 Docker 和 Docker Compose
uninstall_docker() {echo "正在卸载 Docker..."# 停止 Docker 服务sudo systemctl stop docker# 卸载 Dockersudo yum remove -y docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine# 删除所有镜像、容器、卷和网络sudo rm -rf /var/lib/dockerecho "Docker 已卸载。"# 卸载 Docker Composeecho "正在卸载 Docker Compose..."sudo rm -f /usr/local/bin/docker-composesudo rm -f /usr/bin/docker-composeecho "Docker Compose 已卸载。"
}# 显示菜单
show_menu() {echo "=============================="echo " Docker 管理脚本 "echo "=============================="echo "1. 安装 Docker(包括 Docker Compose)"echo "2. 启动 Docker 服务"echo "3. 重启 Docker 服务"echo "4. 停止 Docker 服务"echo "5. 卸载 Docker(包括 Docker Compose)"echo "6. 退出"echo "=============================="
}# =======================
# 主程序
# =======================while true; doshow_menuread -p "请输入你的选择 [1-6]:" choicecase $choice in1)install_docker;;2)start_docker;;3)restart_docker;;4)stop_docker;;5)uninstall_docker;;6)echo "退出脚本。"exit 0;;*)echo "无效的选择,请重新输入。";;esacecho ""
done
Ubuntu docker安装脚本
#!/bin/bash# docker_manager.sh
# 用于管理 Docker 和 Docker Compose 的交互脚本(适用于 Ubuntu)# =======================
# 配置变量
# =======================# Docker 仓库地址
DOCKER_REPO="https://download.docker.com/linux/ubuntu"# =======================
# 函数定义
# =======================# 安装 Docker 和 Docker Compose
install_docker() {echo "正在安装 Docker..."# 移除旧版本sudo apt-get remove -y docker docker-engine docker.io containerd runc# 更新包索引sudo apt-get update# 安装必要的软件包sudo apt-get install -y apt-transport-https ca-certificates curl gnupg lsb-release# 添加 Docker 的官方 GPG 密钥curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg# 设置稳定的 Docker 仓库echo \"deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] $DOCKER_REPO $(lsb_release -cs) stable" | \sudo tee /etc/apt/sources.list.d/docker.list > /dev/null# 更新包索引sudo apt-get update# 安装 Docker Engine、Docker CLI 和 containerdsudo apt-get install -y docker-ce docker-ce-cli containerd.io# 启动并设置开机自启sudo systemctl start dockersudo systemctl enable dockerecho "Docker 安装完成。"# 安装 Docker Composeecho "正在安装 Docker Compose..."# 获取最新版本号DOCKER_COMPOSE_LATEST=$(curl -s https://api.github.com/repos/docker/compose/releases/latest | grep tag_name | cut -d '"' -f 4)if [ -z "$DOCKER_COMPOSE_LATEST" ]; thenecho "无法获取 Docker Compose 最新版本号。请手动检查 https://github.com/docker/compose/releases"returnfi# 下载 Docker Compose 二进制文件sudo curl -L "https://github.com/docker/compose/releases/download/${DOCKER_COMPOSE_LATEST}/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose# 授予可执行权限sudo chmod +x /usr/local/bin/docker-compose# 创建软链接(可选)sudo ln -sf /usr/local/bin/docker-compose /usr/bin/docker-compose# 验证安装if command -v docker-compose &> /dev/null; thenecho "Docker Compose 安装完成,版本:$(docker-compose --version)"elseecho "Docker Compose 安装失败。"fi
}# 启动 Docker 服务
start_docker() {echo "正在启动 Docker 服务..."sudo systemctl start dockersudo systemctl enable dockerecho "Docker 服务已启动。"
}# 重启 Docker 服务
restart_docker() {echo "正在重启 Docker 服务..."sudo systemctl restart dockerif [ $? -eq 0 ]; thenecho "Docker 服务已成功重启。"elseecho "Docker 服务重启失败,请检查日志。"fi
}# 停止 Docker 服务
stop_docker() {echo "正在停止 Docker 服务..."sudo systemctl stop dockerif [ $? -eq 0 ]; thenecho "Docker 服务已停止。"elseecho "Docker 服务停止失败,请检查日志。"fi
}# 卸载 Docker 和 Docker Compose
uninstall_docker() {echo "正在卸载 Docker..."# 停止 Docker 服务sudo systemctl stop docker# 卸载 Dockersudo apt-get purge -y docker-ce docker-ce-cli containerd.io# 删除所有镜像、容器、卷和网络sudo rm -rf /var/lib/dockersudo rm -rf /var/lib/containerdecho "Docker 已卸载。"# 卸载 Docker Composeecho "正在卸载 Docker Compose..."sudo rm -f /usr/local/bin/docker-composesudo rm -f /usr/bin/docker-composeecho "Docker Compose 已卸载。"
}# 显示菜单
show_menu() {echo "=============================="echo " Docker 管理脚本 "echo "=============================="echo "1. 安装 Docker(包括 Docker Compose)"echo "2. 启动 Docker 服务"echo "3. 重启 Docker 服务"echo "4. 停止 Docker 服务"echo "5. 卸载 Docker(包括 Docker Compose)"echo "6. 退出"echo "=============================="
}# =======================
# 主程序
# =======================while true; doshow_menuread -p "请输入你的选择 [1-6]:" choicecase $choice in1)install_docker;;2)start_docker;;3)restart_docker;;4)stop_docker;;5)uninstall_docker;;6)echo "退出脚本。"exit 0;;*)echo "无效的选择,请重新输入。";;esacecho ""
done