Docker一键安装脚本信息
脚本内容:
bash
#!/bin/bash
set -e
LOGFILE="setup_docker.log"
# 将输出重定向到日志文件,同时在终端显示
exec > >(tee -i $LOGFILE)
exec 2>&1
# 检查命令是否可用
check_command() {
command -v "$1" >/dev/null 2>&1 || {
echo >&2 "$1 是必需的命令但未安装。正在终止。"
exit 1
}
}
# 检查网络连接
check_network() {
echo "检查网络连接..."
curl -s --head https://www.baidu.com | head -n 1 | grep "200\|301\|302" >/dev/null || {
echo "无法连接到互联网,请检查网络连接。"
exit 1
}
}
# 打印当前内核版本信息
print_kernel_version() {
echo "当前内核版本:"
uname -r
}
# 备份 yum 源配置
backup_yum_repos() {
echo "备份现有的 yum 源配置..."
sudo cp -r /etc/yum.repos.d /etc/yum.repos.d.backup || {
echo "备份 yum 源配置失败。正在终止。"
exit 1
}
}
# 更新 yum 源为阿里云镜像
update_yum_repos() {
echo "更新 yum 源为阿里云镜像..."
sudo curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo || {
echo "更新 yum 源失败。正在终止。"
exit 1
}
}
# 清理和重新生成 yum 缓存
refresh_yum_cache() {
echo "清理 yum 缓存..."
sudo yum clean all || {
echo "清理 yum 缓存失败。正在终止。"
exit 1
}
echo "重新生成 yum 缓存..."
sudo yum makecache || {
echo "生成 yum 缓存失败。正在终止。"
exit 1
}
}
# 更新系统
update_system() {
echo "更新系统软件包..."
sudo yum update -y || {
echo "更新系统软件包失败。正在终止。"
exit 1
}
}
# 安装必要的软件包
install_packages() {
echo "安装必要的软件包..."
sudo yum install -y yum-utils device-mapper-persistent-data lvm2 || {
echo "安装必要的软件包失败。正在终止。"
exit 1
}
}
# 添加 Docker CE 仓库
add_docker_repo() {
echo "添加 Docker CE 仓库..."
sudo yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo || {
echo "添加 Docker CE 仓库失败。正在终止。"
exit 1
}
echo "替换 Docker CE 仓库的下载地址为阿里云镜像..."
sudo sed -i 's/download.docker.com/mirrors.aliyun.com\/docker-ce/g' /etc/yum.repos.d/docker-ce.repo || {
echo "替换 Docker CE 仓库下载地址失败。正在终止。"
exit 1
}
}
# 安装 Docker CE
install_docker() {
echo "安装 Docker CE..."
sudo yum install -y docker-ce-25.0.5 docker-ce-cli-25.0.5 containerd.io || {
echo "安装 Docker CE 失败。正在终止。"
exit 1
}
}
# 下载并设置 Docker Compose
setup_docker_compose() {
echo "下载 Docker Compose..."
sudo curl -L https://gitee.com/fustack/docker-compose/releases/download/v2.24.1/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose || {
echo "下载 Docker Compose 失败。正在终止。"
exit 1
}
echo "赋予 Docker Compose 执行权限..."
sudo chmod +x /usr/local/bin/docker-compose || {
echo "赋予 Docker Compose 执行权限失败。正在终止。"
exit 1
}
}
# 启动和设置 Docker 服务
manage_docker_service() {
echo "启动 Docker 服务..."
sudo systemctl start docker || {
echo "启动 Docker 服务失败。正在终止。"
exit 1
}
echo "设置 Docker 服务开机启动..."
sudo systemctl enable docker || {
echo "设置 Docker 服务开机启动失败。正在终止。"
exit 1
}
echo "重启 Docker 服务..."
sudo systemctl restart docker || {
echo "重启 Docker 服务失败。正在终止。"
exit 1
}
}
# 主函数
main() {
# 进度提示
local steps=("检查命令" "检查网络连接" "打印内核版本" "备份 yum 源配置" "更新 yum 源" "清理和生成 yum 缓存" "更新系统" "安装必要软件包" "添加 Docker 仓库" "安装 Docker" "设置 Docker Compose" "管理 Docker 服务")
# 步骤计数器
local step_counter=1
echo "开始执行脚本,共 ${#steps[@]} 个步骤..."
# 检查必要的命令是否可用
echo "步骤 ${step_counter}/${#steps[@]}: ${steps[$((step_counter-1))]}"
check_command curl
check_command yum
check_command sudo
check_command systemctl
step_counter=$((step_counter + 1))
# 检查网络连接
echo "步骤 ${step_counter}/${#steps[@]}: ${steps[$((step_counter-1))]}"
check_network
step_counter=$((step_counter + 1))
echo "步骤 ${step_counter}/${#steps[@]}: ${steps[$((step_counter-1))]}"
print_kernel_version
step_counter=$((step_counter + 1))
echo "步骤 ${step_counter}/${#steps[@]}: ${steps[$((step_counter-1))]}"
backup_yum_repos
step_counter=$((step_counter + 1))
echo "步骤 ${step_counter}/${#steps[@]}: ${steps[$((step_counter-1))]}"
update_yum_repos
step_counter=$((step_counter + 1))
echo "步骤 ${step_counter}/${#steps[@]}: ${steps[$((step_counter-1))]}"
refresh_yum_cache
step_counter=$((step_counter + 1))
echo "步骤 ${step_counter}/${#steps[@]}: ${steps[$((step_counter-1))]}"
update_system
step_counter=$((step_counter + 1))
echo "步骤 ${step_counter}/${#steps[@]}: ${steps[$((step_counter-1))]}"
install_packages
step_counter=$((step_counter + 1))
echo "步骤 ${step_counter}/${#steps[@]}: ${steps[$((step_counter-1))]}"
add_docker_repo
step_counter=$((step_counter + 1))
echo "步骤 ${step_counter}/${#steps[@]}: ${steps[$((step_counter-1))]}"
install_docker
step_counter=$((step_counter + 1))
echo "步骤 ${step_counter}/${#steps[@]}: ${steps[$((step_counter-1))]}"
setup_docker_compose
step_counter=$((step_counter + 1))
echo "步骤 ${step_counter}/${#steps[@]}: ${steps[$((step_counter-1))]}"
manage_docker_service
step_counter=$((step_counter + 1))
echo "脚本执行成功!"
}
main
验证:
bash
docker pull hello-world