跳到主要内容

Linux

2024年05月14日
柏拉文
越努力,越幸运

一、Yum


1.1 步骤

1.2 脚本

1. 创建一个名为 install_docker.sh 的脚本

#!/bin/bash

set -e # 遇到错误时停止脚本执行

# 函数:处理错误
handle_error() {
echo "Error: $1"
exit 1
}

# 函数:检查是否以 root 用户运行
check_root() {
if [ "$(id -u)" -ne 0 ]; then
echo "This script must be run as root. Exiting."
exit 1
fi
}

# 函数:删除旧的 Docker 配置文件
remove_old_docker_config() {
echo "Removing old Docker configuration files (if any)..."
if [ -f "$DOCKER_CONFIG_FILE" ]; then
sudo rm -f "$DOCKER_CONFIG_FILE" || handle_error "Failed to remove old Docker config file"
echo "Old Docker configuration file removed."
fi
}

# 函数:删除旧版本 Docker
remove_old_docker() {
echo "Removing old versions of Docker..."
# 删除已安装的 Docker 相关包
if sudo yum list installed | grep -q '^docker'; then
sudo yum remove -y docker docker-ce docker-ce-cli containerd.io docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-engine || handle_error "Failed to remove old Docker packages"
echo "Old Docker versions removed."
else
echo "No old Docker versions found."
fi
}

# 函数:安装 yum 必要系统工具
install_required_packages() {
echo "Installing required packages..."
sudo yum install -y --allowerasing yum-utils device-mapper-persistent-data lvm2 || handle_error "Failed to install necessary packages"
}

# 函数:添加 Docker 软件源信息
add_docker_repo() {
echo "Adding Docker repository..."
# 检查是否已添加 Docker 软件源
if ! sudo yum repolist all | grep -q 'docker-ce'; then
sudo yum-config-manager --add-repo "$DOCKER_REPO_URL" || handle_error "Failed to add Docker repository"
echo "Docker repository added."
else
echo "Docker repository already added."
fi
}

# 函数:更换 Docker 镜像源为国内镜像
configure_docker_mirror() {
echo "Configuring Docker to use a domestic mirror..."
# 创建配置目录(如果不存在)
sudo mkdir -p "$DOCKER_CONFIG_DIR" || handle_error "Failed to create Docker config directory"

# 写入配置文件
cat <<EOF | sudo tee "$DOCKER_CONFIG_FILE" > /dev/null
{
"registry-mirrors": ["$DOCKER_MIRROR_URL", "https://registry.docker-cn.com"]
}
EOF
echo "Docker configuration updated."
}

# 函数:更新软件源缓存并安装 Docker-CE
install_docker_ce() {
echo "Updating package index and installing Docker-CE..."
sudo yum makecache || handle_error "Failed to update yum cache"
sudo yum install -y --allowerasing docker-ce || handle_error "Failed to install Docker-CE"
}

# 函数:启动并启用 Docker 服务
start_docker_service() {
echo "Starting and enabling Docker service..."
sudo systemctl start docker || handle_error "Failed to start Docker service"
sudo systemctl enable docker || handle_error "Failed to enable Docker service"
}

# 函数:验证 Docker 服务状态
verify_docker_service() {
if systemctl is-active --quiet docker; then
echo "Docker service is running."
else
handle_error "Docker service is not running."
fi
}

# 函数:添加当前用户到 docker 组
add_user_to_docker_group() {
echo "Adding user to docker group..."
sudo usermod -aG docker "$USER" || handle_error "Failed to add user to docker group"
}

# 函数:验证 Docker 安装
verify_docker_installation() {
echo "Verifying Docker installation..."
sudo docker run hello-world || handle_error "Docker verification failed"
}

# 定义变量
DOCKER_REPO_URL="https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo"
DOCKER_MIRROR_URL="https://624m5g3q.mirror.aliyuncs.com"
DOCKER_CONFIG_DIR="/etc/docker"
DOCKER_CONFIG_FILE="$DOCKER_CONFIG_DIR/daemon.json"

# 主程序
check_root

# 删除旧的 Docker 配置文件
remove_old_docker_config

# 删除旧版本 Docker
remove_old_docker

# 安装 yum 必要系统工具
install_required_packages

# 添加 Docker 软件源信息
add_docker_repo

# 更换 Docker 镜像源为国内镜像
configure_docker_mirror

# 更新软件源缓存并安装 Docker-CE
install_docker_ce

# 启动并启用 Docker 服务
start_docker_service

# 验证 Docker 服务状态
verify_docker_service

# 添加当前用户到 docker 组
add_user_to_docker_group

# 验证 Docker 安装
verify_docker_installation

# 提示重新登录以使组更改生效
echo "Docker installation completed successfully!"
echo "Please log out and log back in to apply the docker group membership, or run 'newgrp docker' to apply it immediately."

2. 发送 install_docker.sh 脚本(可选)

scp install_docker.sh root@192.168.105.132:/root

3. 赋予脚本可执行权限

chmod +x install_docker.sh

4. 执行脚本,安装 Docker

./install_docker.sh

5. 检查 Docker 服务状态

systemctl start docker // 立即启动 docker 服务,而不是等到系统重启时。
systemctl stop docker // 停止正在运行的 docker 服务。
systemctl disable docker // 禁止 docker 服务在系统启动时自动启动。
systemctl status docker // 查看 docker 服务的当前状态。
systemctl list-unit-files --type=service | grep enabled // 查看所有自动启动的服务