直接上脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/bin/bash

CURRENT_DIR=$(
cd "$(dirname "$0")"
pwd
)

function log() {
# 设置打印格式
message="[Docker Log]: $1 "
echo -e "${message}" 2>&1 | tee -a ${CURRENT_DIR}/install.log
}

# 一段花里胡哨的前控制台显示
echo
cat << EOF
Created by :

██████╗ ███████╗██╗ ██╗
██╔═══██╗██╔════╝██║ ██║
██║ ██║███████╗███████║
██║▄▄ ██║╚════██║██╔══██║
╚██████╔╝███████║██║ ██║
╚══▀▀═╝ ╚══════╝╚═╝ ╚═╝

for

██████╗ ██████╗ ██████╗██╗ ██╗███████╗██████╗
██╔══██╗██╔═══██╗██╔════╝██║ ██╔╝██╔════╝██╔══██╗
██║ ██║██║ ██║██║ █████╔╝ █████╗ ██████╔╝
██║ ██║██║ ██║██║ ██╔═██╗ ██╔══╝ ██╔══██╗
██████╔╝╚██████╔╝╚██████╗██║ ██╗███████╗██║ ██║
╚═════╝ ╚═════╝ ╚═════╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝

EOF

log "======================= 开始安装 ======================="

function Install_Docker(){
if which docker >/dev/null 2>&1; then
docker_v=`docker version | awk 'NR == 2{print $2}'`
log "检测到 Docker 已安装,跳过安装步骤"
log "启动 Docker "
systemctl start docker 2>&1 | tee -a ${CURRENT_DIR}/install.log
else
log "... 在线安装 docker"

if [[ $(curl -s ipinfo.io/country) == "CN" ]]; then
sources=(
"https://mirrors.aliyun.com/docker-ce"
"https://mirrors.tencent.com/docker-ce"
"https://mirrors.163.com/docker-ce"
"https://mirrors.cernet.edu.cn/docker-ce"
)

docker_install_scripts=(
"https://get.docker.com"
"https://testingcf.jsdelivr.net/gh/docker/docker-install@master/install.sh"
"https://cdn.jsdelivr.net/gh/docker/docker-install@master/install.sh"
"https://fastly.jsdelivr.net/gh/docker/docker-install@master/install.sh"
"https://gcore.jsdelivr.net/gh/docker/docker-install@master/install.sh"
"https://raw.githubusercontent.com/docker/docker-install/master/install.sh"
)

get_average_delay() {
local source=$1
local total_delay=0
local iterations=3

for ((i = 0; i < iterations; i++)); do
delay=$(curl -o /dev/null -s -w "%{time_total}\n" "$source")
total_delay=$(awk "BEGIN {print $total_delay + $delay}")
done

average_delay=$(awk "BEGIN {print $total_delay / $iterations}")
echo "$average_delay"
}

min_delay=${#sources[@]}
selected_source=""

for source in "${sources[@]}"; do
average_delay=$(get_average_delay "$source")

if (( $(awk 'BEGIN { print '"$average_delay"' < '"$min_delay"' }') )); then
min_delay=$average_delay
selected_source=$source
fi
done

if [ -n "$selected_source" ]; then
echo "选择延迟最低的源 $selected_source,延迟为 $min_delay 秒"
export DOWNLOAD_URL="$selected_source"

for alt_source in "${docker_install_scripts[@]}"; do
log "尝试从备选链接 $alt_source 下载 Docker 安装脚本..."
if curl -fsSL --retry 2 --retry-delay 3 --connect-timeout 5 --max-time 10 "$alt_source" -o get-docker.sh; then
log "成功从 $alt_source 下载安装脚本"
break
else
log "从 $alt_source 下载安装脚本失败,尝试下一个备选链接"
fi
done

if [ ! -f "get-docker.sh" ]; then
echo "所有下载尝试都失败了。您可以尝试手动安装 Docker,运行以下命令:"
echo "bash <(curl -sSL https://linuxmirrors.cn/docker.sh)"
exit 1
fi

sh get-docker.sh 2>&1 | tee -a ${CURRENT_DIR}/install.log

log "... 启动 docker"
systemctl enable docker; systemctl daemon-reload; systemctl start docker 2>&1 | tee -a ${CURRENT_DIR}/install.log

docker_config_folder="/etc/docker"
if [[ ! -d "$docker_config_folder" ]];then
mkdir -p "$docker_config_folder"
fi

docker version >/dev/null 2>&1
if [[ $? -ne 0 ]]; then
log "docker 安装失败"
exit 1
else
log "docker 安装成功"
docker_v=`docker version | awk 'NR == 2{print $2}'`
fi
else
log "无法选择源进行安装"
exit 1
fi
else
log "非中国大陆地区,无需更改源"
export DOWNLOAD_URL="https://download.docker.com"
curl -fsSL "https://get.docker.com" -o get-docker.sh
sh get-docker.sh 2>&1 | tee -a ${CURRENT_DIR}/install.log

log "... 启动 docker"
systemctl enable docker; systemctl daemon-reload; systemctl start docker 2>&1 | tee -a ${CURRENT_DIR}/install.log

docker_config_folder="/etc/docker"
if [[ ! -d "$docker_config_folder" ]];then
mkdir -p "$docker_config_folder"
fi

docker version >/dev/null 2>&1
if [[ $? -ne 0 ]]; then
log "docker 安装失败"
exit 1
else
log "docker 安装成功"
docker_v=`docker version | awk 'NR == 2{print $2}'`
fi
fi
fi
}

function Install_Compose(){
docker-compose version >/dev/null 2>&1
if [[ $? -ne 0 ]]; then
log "... 在线安装 docker-compose"

arch=$(uname -m)
if [ "$arch" == 'armv7l' ]; then
arch='armv7'
fi
curl -L https://resource.fit2cloud.com/docker/compose/releases/download/v2.26.1/docker-compose-$(uname -s | tr A-Z a-z)-$arch -o /usr/local/bin/docker-compose 2>&1 | tee -a ${CURRENT_DIR}/install.log
if [[ ! -f /usr/local/bin/docker-compose ]];then
log "docker-compose 下载失败,请稍候重试"
exit 1
fi
chmod +x /usr/local/bin/docker-compose
ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

docker-compose version >/dev/null 2>&1
if [[ $? -ne 0 ]]; then
log "docker-compose 安装失败"
exit 1
else
log "docker-compose 安装成功"
compose_v=`docker-compose -v`
fi
else
compose_v=`docker-compose -v`
if [[ $compose_v =~ 'docker-compose' ]];then
read -p "检测到已安装 Docker Compose 版本较低(建议使用大于等于 v2.0.0 版本),是否升级 [y/n] : " UPGRADE_DOCKER_COMPOSE
if [[ "$UPGRADE_DOCKER_COMPOSE" == "Y" ]] || [[ "$UPGRADE_DOCKER_COMPOSE" == "y" ]]; then
rm -rf /usr/local/bin/docker-compose /usr/bin/docker-compose
Install_Compose
else
log "Docker Compose 版本为 $compose_v"
fi
else
log "检测到 Docker Compose 已安装,跳过安装步骤"
fi
fi
}


function Show_Result(){
log ""
log "=================感谢您的耐心等待,安装已经完成=================="
log ""
log "Docker 版本为 $docker_v"
log "Docker Compose 版本为 $compose_v"
log ""
log "==============================================================="
}

function main(){
# 安装docker
Install_Docker
# 安装docker-compose
Install_Compose
# 显示安装结果
Show_Result
}

# 执行main方法
main