flnxi-Wu/scripts/server/setup-docker-mirror.sh
WuYongJie f646b71813
Some checks failed
CI Check / check (push) Has been cancelled
chore: 初始化本地仓库并同步最近改动
2025-11-18 17:50:16 +08:00

156 lines
4.2 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# ============================================
# Docker 镜像加速器配置脚本
# 用于配置国内镜像源,加速 Docker 镜像拉取
# ============================================
set -e
echo "=========================================="
echo "配置 Docker 镜像加速器(国内镜像源)"
echo "=========================================="
# 检查是否为 root 用户
if [ "$EUID" -ne 0 ]; then
echo "❌ 错误: 请使用 root 用户或 sudo 运行此脚本"
exit 1
fi
# Docker 配置文件路径
DOCKER_DAEMON_JSON="/etc/docker/daemon.json"
DOCKER_DAEMON_JSON_BACKUP="/etc/docker/daemon.json.backup.$(date +%Y%m%d_%H%M%S)"
# 国内镜像源列表(按优先级排序)
MIRRORS=(
"https://docker.mirrors.ustc.edu.cn"
"https://hub-mirror.c.163.com"
"https://mirror.baidubce.com"
)
echo ""
echo "📋 配置信息:"
echo " 配置文件: ${DOCKER_DAEMON_JSON}"
echo " 镜像源数量: ${#MIRRORS[@]}"
echo ""
# 备份现有配置
if [ -f "$DOCKER_DAEMON_JSON" ]; then
echo "📦 备份现有配置..."
cp "$DOCKER_DAEMON_JSON" "$DOCKER_DAEMON_JSON_BACKUP"
echo " ✓ 备份完成: ${DOCKER_DAEMON_JSON_BACKUP}"
else
echo "📦 创建新配置文件..."
mkdir -p /etc/docker
fi
# 创建新的配置
echo ""
echo "🔧 配置镜像加速器..."
# 检查是否已存在 registry-mirrors 配置
if [ -f "$DOCKER_DAEMON_JSON" ]; then
# 如果文件存在,检查是否已有 registry-mirrors
if grep -q "registry-mirrors" "$DOCKER_DAEMON_JSON"; then
echo " ⚠️ 检测到已有 registry-mirrors 配置"
echo " 📝 将更新为推荐的国内镜像源"
# 使用 jq 更新配置(如果安装了 jq)
if command -v jq &> /dev/null; then
jq '.registry-mirrors = $mirrors' --argjson mirrors "$(printf '%s\n' "${MIRRORS[@]}" | jq -R . | jq -s .)" "$DOCKER_DAEMON_JSON" > "${DOCKER_DAEMON_JSON}.tmp"
mv "${DOCKER_DAEMON_JSON}.tmp" "$DOCKER_DAEMON_JSON"
else
echo " ⚠️ 未安装 jq,将手动创建配置"
# 创建新配置(保留其他配置项)
python3 << EOF
import json
import sys
try:
with open('${DOCKER_DAEMON_JSON}', 'r') as f:
config = json.load(f)
except:
config = {}
config['registry-mirrors'] = ${MIRRORS[@]@Q}
with open('${DOCKER_DAEMON_JSON}', 'w') as f:
json.dump(config, f, indent=2)
EOF
fi
else
# 追加 registry-mirrors 到现有配置
python3 << EOF
import json
try:
with open('${DOCKER_DAEMON_JSON}', 'r') as f:
config = json.load(f)
except:
config = {}
config['registry-mirrors'] = ${MIRRORS[@]@Q}
with open('${DOCKER_DAEMON_JSON}', 'w') as f:
json.dump(config, f, indent=2)
EOF
fi
else
# 创建新配置文件
cat > "$DOCKER_DAEMON_JSON" << EOF
{
"registry-mirrors": [
"https://docker.mirrors.ustc.edu.cn",
"https://hub-mirror.c.163.com",
"https://mirror.baidubce.com"
]
}
EOF
fi
echo " ✓ 配置完成"
# 显示配置内容
echo ""
echo "📄 当前配置内容:"
cat "$DOCKER_DAEMON_JSON" | python3 -m json.tool 2>/dev/null || cat "$DOCKER_DAEMON_JSON"
echo ""
# 重启 Docker 服务
echo "🔄 重启 Docker 服务..."
if systemctl is-active --quiet docker; then
systemctl daemon-reload
systemctl restart docker
echo " ✓ Docker 服务已重启"
else
echo " ⚠️ Docker 服务未运行,请手动启动: systemctl start docker"
fi
# 验证配置
echo ""
echo "🔍 验证配置..."
sleep 2
if docker info | grep -q "Registry Mirrors"; then
echo " ✓ 镜像加速器配置成功"
echo ""
echo "📋 已配置的镜像源:"
docker info | grep -A 10 "Registry Mirrors" | sed 's/^/ /'
else
echo " ⚠️ 未检测到镜像加速器配置,请检查 Docker 服务状态"
fi
echo ""
echo "=========================================="
echo "✅ 配置完成!"
echo "=========================================="
echo ""
echo "💡 提示:"
echo " - 配置已保存到: ${DOCKER_DAEMON_JSON}"
echo " - 备份文件: ${DOCKER_DAEMON_JSON_BACKUP}"
echo " - 现在可以使用 docker pull 拉取镜像,将自动使用国内镜像源"
echo ""
echo "🧪 测试镜像拉取:"
echo " docker pull minio/minio:RELEASE.2025-04-22T22-12-26Z"
echo ""