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
# 安装 Docker
sudo yum install -y docker-ce docker-ce-cli containerd.io
# 启动并设置开机自启
sudo systemctl start docker
sudo systemctl enable docker
echo "Docker 安装完成。"
# 安装 Docker Compose
echo "正在安装 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" ]; then
echo "无法获取 Docker Compose 最新版本号。请手动检查 https://github.com/docker/compose/releases"
return
fi
# 下载 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; then
echo "Docker Compose 安装完成,版本:$(docker-compose --version)"
else
echo "Docker Compose 安装失败。"
fi
}
# 启动 Docker 服务
start_docker() {
echo "正在启动 Docker 服务..."
sudo systemctl start docker
sudo systemctl enable docker
echo "Docker 服务已启动。"
}
# 重启 Docker 服务
restart_docker() {
echo "正在重启 Docker 服务..."
sudo systemctl restart docker
if [ $? -eq 0 ]; then
echo "Docker 服务已成功重启。"
else
echo "Docker 服务重启失败,请检查日志。"
fi
}
# 停止 Docker 服务
stop_docker() {
echo "正在停止 Docker 服务..."
sudo systemctl stop docker
if [ $? -eq 0 ]; then
echo "Docker 服务已停止。"
else
echo "Docker 服务停止失败,请检查日志。"
fi
}
# 卸载 Docker 和 Docker Compose
uninstall_docker() {
echo "正在卸载 Docker..."
# 停止 Docker 服务
sudo systemctl stop docker
# 卸载 Docker
sudo 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/docker
echo "Docker 已卸载。"
# 卸载 Docker Compose
echo "正在卸载 Docker Compose..."
sudo rm -f /usr/local/bin/docker-compose
sudo rm -f /usr/bin/docker-compose
echo "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; do
show_menu
read -p "请输入你的选择 [1-6]:" choice
case $choice in
1)
install_docker
;;
2)
start_docker
;;
3)
restart_docker
;;
4)
stop_docker
;;
5)
uninstall_docker
;;
6)
echo "退出脚本。"
exit 0
;;
*)
echo "无效的选择,请重新输入。"
;;
esac
echo ""
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 和 containerd
sudo apt-get install -y docker-ce docker-ce-cli containerd.io
# 启动并设置开机自启
sudo systemctl start docker
sudo systemctl enable docker
echo "Docker 安装完成。"
# 安装 Docker Compose
echo "正在安装 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" ]; then
echo "无法获取 Docker Compose 最新版本号。请手动检查 https://github.com/docker/compose/releases"
return
fi
# 下载 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; then
echo "Docker Compose 安装完成,版本:$(docker-compose --version)"
else
echo "Docker Compose 安装失败。"
fi
}
# 启动 Docker 服务
start_docker() {
echo "正在启动 Docker 服务..."
sudo systemctl start docker
sudo systemctl enable docker
echo "Docker 服务已启动。"
}
# 重启 Docker 服务
restart_docker() {
echo "正在重启 Docker 服务..."
sudo systemctl restart docker
if [ $? -eq 0 ]; then
echo "Docker 服务已成功重启。"
else
echo "Docker 服务重启失败,请检查日志。"
fi
}
# 停止 Docker 服务
stop_docker() {
echo "正在停止 Docker 服务..."
sudo systemctl stop docker
if [ $? -eq 0 ]; then
echo "Docker 服务已停止。"
else
echo "Docker 服务停止失败,请检查日志。"
fi
}
# 卸载 Docker 和 Docker Compose
uninstall_docker() {
echo "正在卸载 Docker..."
# 停止 Docker 服务
sudo systemctl stop docker
# 卸载 Docker
sudo apt-get purge -y docker-ce docker-ce-cli containerd.io
# 删除所有镜像、容器、卷和网络
sudo rm -rf /var/lib/docker
sudo rm -rf /var/lib/containerd
echo "Docker 已卸载。"
# 卸载 Docker Compose
echo "正在卸载 Docker Compose..."
sudo rm -f /usr/local/bin/docker-compose
sudo rm -f /usr/bin/docker-compose
echo "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; do
show_menu
read -p "请输入你的选择 [1-6]:" choice
case $choice in
1)
install_docker
;;
2)
start_docker
;;
3)
restart_docker
;;
4)
stop_docker
;;
5)
uninstall_docker
;;
6)
echo "退出脚本。"
exit 0
;;
*)
echo "无效的选择,请重新输入。"
;;
esac
echo ""
done