Files
shell-scripts/set-docker-mirror.sh
2026-08-05 01:43:16 -05:00

41 lines
1000 B
Bash

#!/bin/bash
set -euo pipefail
MIRRORS=(
"https://docker.1ms.run"
"https://docker.xuanyuan.me"
"https://docker.m.daocloud.io"
"https://dockerproxy.com"
"https://docker.nju.edu.cn"
)
DAEMON_JSON="/etc/docker/daemon.json"
if [ "$EUID" -ne 0 ]; then
echo "Please run as root (sudo)." >&2
exit 1
fi
# Backup existing config
if [ -f "$DAEMON_JSON" ]; then
cp "$DAEMON_JSON" "${DAEMON_JSON}.bak.$(date +%s)"
fi
# Build mirrors JSON array
MIRRORS_JSON=$(printf '%s\n' "${MIRRORS[@]}" | jq -R . | jq -s .)
# Merge with existing config if present
if [ -f "$DAEMON_JSON" ]; then
jq --argjson mirrors "$MIRRORS_JSON" '. + {"registry-mirrors": $mirrors}' "$DAEMON_JSON" > "${DAEMON_JSON}.new"
else
jq -n --argjson mirrors "$MIRRORS_JSON" '{"registry-mirrors": $mirrors}' > "${DAEMON_JSON}.new"
fi
mv "${DAEMON_JSON}.new" "$DAEMON_JSON"
echo "Wrote $DAEMON_JSON"
systemctl daemon-reload
systemctl restart docker
echo "Docker mirrors configured. Verify: docker info | grep -A5 Mirrors"