-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
49 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,51 @@ | ||
#!/bin/bash | ||
|
||
json_content='{ | ||
"registry-mirrors" : [ | ||
"https://mirror.gcr.io", | ||
"https://daocloud.io", | ||
"https://c.163.com", | ||
"https://huecker.io", | ||
"https://registry.docker-cn.com" | ||
] | ||
}' | ||
|
||
echo "$json_content" | sudo tee /etc/docker/daemon.json > /dev/null | ||
|
||
sudo systemctl restart docker | ||
# New JSON content to add | ||
new_mirrors=( | ||
"https://dockerhub.timeweb.cloud". | ||
"https://mirror.gcr.io" | ||
"https://daocloud.io" | ||
"https://c.163.com" | ||
"https://huecker.io" | ||
"https://registry.docker-cn.com" | ||
) | ||
|
||
if [ -f /etc/docker/daemon.json ]; then | ||
echo "File /etc/docker/daemon.json exists. Updating the content." | ||
current_mirrors=$(grep -oP '(?<="registry-mirrors": \[)[^]]*' /etc/docker/daemon.json | tr -d ' \n' | tr ',' '\n' | tr -d '"') | ||
else | ||
echo "File /etc/docker/daemon.json does not exist. Creating a new file." | ||
current_mirrors="" | ||
fi | ||
|
||
combined_mirrors=$(echo -e "$current_mirrors\n${new_mirrors[*]}" | tr ' ' '\n' | sort | uniq) | ||
|
||
new_content="{\n \"registry-mirrors\": [\n" | ||
for mirror in $combined_mirrors; do | ||
new_content+=" \"$mirror\",\n" | ||
done | ||
|
||
new_content=$(echo -e "$new_content" | sed '$ s/,$//') | ||
new_content=$(echo -e "$new_content" | sed '$ s/,$//') | ||
new_content="${new_content}\n ]\n}" | ||
|
||
echo -e "$new_content" | sudo tee /etc/docker/daemon.json > /dev/null | ||
|
||
# ANSI color codes | ||
GREEN='\033[0;32m' | ||
YELLOW='\033[0;33m' | ||
NOCOLOR='\033[0m' | ||
|
||
while true; do | ||
read -p "Restart Docker now? (y/N): " | ||
if [[ $REPLY =~ ^[Yy]$ ]]; then | ||
sudo systemctl restart docker | ||
echo | ||
echo -e "${GREEN}Docker restarted.${NOCOLOR}" | ||
break | ||
elif [[ $REPLY =~ ^[Nn]$ ]] || [[ -z $REPLY ]]; then | ||
echo | ||
echo -e "${YELLOW}Docker was not restarted. Please restart it manually to apply changes.${NOCOLOR}" | ||
break | ||
fi | ||
done |