linuxtool/Config/Docker/source.sh

44 lines
1.3 KiB
Bash
Raw Normal View History

2024-09-26 10:53:51 +08:00
#!/bin/bash
echo "========$(basename $0 .sh)========"
echo "1. 查看当前源"
echo "2. 换源"
declare user_choice
read -p "请输入:" user_choice
2024-09-26 10:53:51 +08:00
case $user_choice in
2024-09-26 10:53:51 +08:00
'1')
grep -oP '(https?://[^\"]+)' /etc/docker/daemon.json
;;
'2')
declare -A mirror_options
declare -a mirror_list
declare mirror_choice=0
mirror_options['Daocloud(默认)']='https://docker.m.daocloud.io'
mirror_options['官方']='docker.io'
2024-09-26 10:53:51 +08:00
for mirror in "${!mirror_options[@]}";
2024-09-26 10:53:51 +08:00
do
mirror_choice=$(( mirror_choice+1 ))
mirror_list[$mirror_choice]=$mirror
echo "${mirror_choice}.${mirror}"
2024-09-26 10:53:51 +08:00
done
read -p "请输入要选择的镜像,也可直接输入镜像网站:" selected_mirror
if [[ -z $selected_mirror ]];then
2024-09-26 10:53:51 +08:00
declare url='https://docker.m.daocloud.io'
elif [[ $selected_mirror =~ [\w\.]+ ]];then
declare url=$selected_mirror
elif [[ ${selected_mirror} =~ [1-${!mirror_options[*]}] ]];then
selected_mirror=${mirror_list[$selected_mirror]}
declare url=${mirror_options[$selected_mirror]}
2024-09-26 10:53:51 +08:00
else
echo "输入错误"
exit
fi
2024-09-26 19:46:58 +08:00
echo "正在写入配置文件"
2024-09-26 10:53:51 +08:00
echo "{\"registry-mirrors\": [\"${url}\"]}" > "/etc/docker/daemon.json"
echo "正在重启 Docker"
systemctl restart docker 2>> /dev/null || echo "Docker 重启失败"&&exit
2024-09-26 10:53:51 +08:00
echo "换源成功"
;;
esac