forked from shidahuilang/QL-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker.sh
248 lines (231 loc) · 7.43 KB
/
docker.sh
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#!/usr/bin/env bash
# 字体颜色配置
Green="\033[32;1m"
Red="\033[31m"
Yellow="\033[33;1m"
Blue="\033[36;1m"
Font="\033[0m"
GreenBG="\033[42;37m"
RedBG="\033[41;37m"
OK="${Green}[OK]${Font}"
ERROR="${Red}[ERROR]${Font}"
function print_ok() {
echo
echo -e " ${OK} ${Blue} $1 ${Font}"
echo
}
function print_error() {
echo
echo -e "${ERROR} ${RedBG} $1 ${Font}"
echo
}
function ECHOY() {
echo
echo -e "${Yellow} $1 ${Font}"
echo
}
function ECHOB() {
echo -e "${Blue} $1 ${Font}"
}
function ECHOG() {
echo
echo -e "${Green} $1 ${Font}"
echo
}
if [[ ! "$USER" == "root" ]]; then
print_error "警告:请使用root用户操作!~~"
exit 1
fi
function system_check() {
if [[ "$(. /etc/os-release && echo "$ID")" == "centos" ]]; then
yum install -y sudo wget curl
[[ ${CHONGXIN} == "YES" ]] && uninstall_centos_dk
install_centos_dk
elif [[ "$(. /etc/os-release && echo "$ID")" == "ubuntu" ]]; then
apt-get -y update
apt-get install -y sudo wget curl
[[ ${CHONGXIN} == "YES" ]] && uninstall_ubuntu_dk
install_ubuntu_dk
elif [[ "$(. /etc/os-release && echo "$ID")" == "debian" ]]; then
apt-get -y update
apt-get install -y sudo wget curl
[[ ${CHONGXIN} == "YES" ]] && uninstall_debian_dk
install_debian_dk
else
print_error "本一键安装docker脚本只支持(centos、ubuntu和debian)!"
exit 1
fi
}
function jiance_dk() {
if [[ -x "$(command -v docker)" ]]; then
ECHOY "检测到docker存在,是否重新安装?"
ECHOG "重新安装会把您现有的所有容器及镜像全部删除,请慎重!"
while :; do
export CHONGXIN=""
read -p " 输入[ N/n ]退出安装,输入[ Y/y ]回车继续: " ANDK
case $ANDK in
[Yy])
export CHONGXIN="YES"
break
;;
[Nn])
export CHONGXIN="NO"
ECHOG "您选择了退出安装程序!"
sleep 1
exit 1
break
;;
*)
ECHOB "提示:请输入正确的选择!"
;;
esac
done
fi
}
function install_centos_dk() {
ECHOY "正在安装docker,请耐心等候..."
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
sudo yum install -y docker-ce docker-ce-cli containerd.io
sed -i 's#ExecStart=/usr/bin/dockerd -H fd://#ExecStart=/usr/bin/dockerd#g' /lib/systemd/system/docker.service
docker_daemon
sudo systemctl daemon-reload
sudo systemctl enable docker
sudo systemctl restart docker
if [[ -x "$(command -v docker)" ]]; then
print_ok "docker安装完成"
else
print_error "docker安装失败"
exit 1
fi
}
function uninstall_centos_dk() {
ECHOY "正在御载docker..."
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
docker rmi $(docker images -q)
sudo yum -y remove docker-ce.x86_64
sudo yum -y remove docker-*
sudo rm -rf /var/lib/docker
sudo rm -rf /etc/docker /etc/systemd/system/docker.service.d
sudo rm -rf /lib/systemd/system/{docker.service,docker.socket}
rm /var/lib/dpkg/info/$nomdupaquet* -f
}
function install_ubuntu_dk() {
ECHOY "正在安装docker,请耐心等候..."
sudo apt-get install -y apt-transport-https ca-certificates curl gnupg2 software-properties-common
curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
if [[ $? -ne 0 ]];then
curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
fi
sudo apt-key fingerprint 0EBFCD88
if [[ `sudo apt-key fingerprint 0EBFCD88 | grep -c "0EBF CD88"` = '0' ]]; then
print_error "密匙验证出错,或者没下载到密匙了,请检查网络,或者源有问题"
exit 1
fi
sudo add-apt-repository -y "deb [arch=amd64] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get update
sudo apt-get install -y docker-ce docker-ce-cli containerd.io
sed -i 's#ExecStart=/usr/bin/dockerd -H fd://#ExecStart=/usr/bin/dockerd#g' /lib/systemd/system/docker.service
docker_daemon
sudo systemctl daemon-reload
sudo systemctl restart docker
if [[ -x "$(command -v docker)" ]]; then
print_ok "docker安装完成"
else
print_error "docker安装失败"
exit 1
fi
}
function uninstall_ubuntu_dk() {
ECHOY "正在御载docker..."
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
docker rmi $(docker images -q)
sudo apt-get -y autoremove docker-* --purge
sudo apt-get -y autoremove --purge
sudo apt-get -y clean
sudo rm -rf /var/lib/docker
sudo rm -rf /etc/docker /etc/systemd/system/docker.service.d
sudo rm -rf /lib/systemd/system/{docker.service,docker.socket}
rm /var/lib/dpkg/info/$nomdupaquet* -f
}
function install_debian_dk() {
ECHOY "正在安装docker,请耐心等候..."
sudo apt install -y apt-transport-https ca-certificates curl gnupg2 software-properties-common
curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/debian/gpg | sudo apt-key add -
if [[ $? -ne 0 ]];then
curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/debian/gpg | sudo apt-key add -
fi
sudo apt-key fingerprint 0EBFCD88
if [[ `sudo apt-key fingerprint 0EBFCD88 | grep -c "0EBF CD88"` = '0' ]]; then
print_error "密匙验证出错,或者没下载到密匙了,请检查网络,或者上游有问题"
exit 1
fi
sudo add-apt-repository -y "deb [arch=amd64] https://mirrors.ustc.edu.cn/docker-ce/linux/debian $(lsb_release -cs) stable"
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io
sed -i 's#ExecStart=/usr/bin/dockerd -H fd://#ExecStart=/usr/bin/dockerd#g' /lib/systemd/system/docker.service
docker_daemon
sudo systemctl daemon-reload
sudo systemctl restart docker
if [[ -x "$(command -v docker)" ]]; then
print_ok "docker安装完成"
else
print_error "docker安装失败"
exit 1
fi
}
function uninstall_debian_dk() {
ECHOY "正在御载docker..."
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
docker rmi $(docker images -q)
sudo apt -y autoremove docker-* --purge
sudo apt -y autoremove --purge
sudo apt -y clean
sudo rm -rf /var/lib/docker
sudo rm -rf /etc/docker /etc/systemd/system/docker.service.d
sudo rm -rf /lib/systemd/system/{docker.service,docker.socket}
rm /var/lib/dpkg/info/$nomdupaquet* -f
}
function hello_world() {
ECHOY "测试docker拉取镜像是否成功"
sudo docker run hello-world |tee build.log
if [[ `docker ps -a | grep -c "hello-world"` -ge '1' ]] && [[ `grep -c "hub.docker.com" build.log` -ge '1' ]]; then
ECHOG "测试镜像拉取成功,正在删除测试镜像..."
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
docker rmi $(docker images -q)
rm -fr build.log
ECHOY "测试镜像删除完毕"
print_ok "docker安装成功"
else
ECHOY "docker虽然安装成功但是拉取镜像失败,这个原因很多是因为以前的docker没御载完全造成的,或者容器网络问题"
ECHOY "重启服务器后,用 sudo docker run hello-world 命令测试吧,能拉取成功就成了"
rm -fr build.log
sleep 2
exit 1
fi
}
function docker_daemon() {
sudo mkdir -p /etc/docker
cat >/etc/docker/daemon.json <<-EOF
{
"registry-mirrors": ["https://qndprgwv.mirror.aliyuncs.com"],
"runtimes": {
"nvidia": {
"path": "/usr/bin/nvidia-container-runtime",
"runtimeArgs": []
}
}
}
EOF
chmod +x /etc/docker/daemon.json
}
memu() {
jiance_dk
system_check
hello_world
}
memu "$@"