辉_少

docker命令脚本
第一版:

1
#!/bin/bash 2 #this is input docker continer shell! 3 #this is 2019-01-29 4 # v1.1.2 5 read -p "请输入要执行docker的名称:" NAME #此出输入要执行docker的名称 6 a=$1 7 case $a in 8 *images) 9 if [ $a = images ];then 10 docker images|grep $NAME|awk '{print $1,$3}' 11 elif [ $a = all-images ];then 12 docker images 13 else 14 echo "error" 15 fi 16 ;; 17 *status) 18 if [ $a = status ];then 19 docker ps -a|grep $NAME 20 elif [ $a = all-status ];then 21 docker ps -a 22 else 23 echo "error" 24 fi 25 ;; 26 run*) 27 if [ $a = run ];then 28 docker run -itd --restart=always --name $NAME $(docker images|grep $NAME|awk '{print $1}') 29 elif [ $a = run-v ];then 30 docker run -itd --restart=always -v /root/:/home/ --name $NAME $(docker images|grep $NAME|awk '{print $1}') 31 elif [ $a = run-p80 ];then 32 docker run -itd --restart=always -v /root/:/home/ -p 80:80 --name $NAME $(docker images|grep $NAMW|awk '{print $1}') 33 else 34 echo "error" 35 fi 36 ;; 37 *id) 38 if [ $a = id ];then 39 docker ps -a|grep $NAME |awk '{print $1}' 40 elif [ $a = all-id ];then 41 docker ps -a|awk '{print $1,$NF}' 42 else 43 echo "error" 44 fi 45 ;; 46 *restart) 47 if [ $a = restart ];then 48 docker restart $(docker ps -a|grep $NAME |awk '{print $1}') 49 elif [ $a = all-restart ];then 50 for i in `docker ps -a|awk '{print $1}'`;do 51 docker restart $i 52 done 53 else 54 echo "error" 55 fi 56 ;; 57 *stop) 58 if [ $a = stop ];then 59 docker stop $(docker ps -a|grep $NAME |awk '{print $1}') 60 elif [ $a = all-stop ];then 61 for s in `docker ps|awk '{print $1}'`;do 62 docker stop $s 63 done 64 else 65 echo "error" 66 fi 67 ;; 68 inspect) 69 docker inspect $(docker ps -a|grep $NAME |awk '{print $1}') 70 ;; 71 *runtime) 72 if [ $a = runtime ];then 73 docker ps -a|grep $NAME |awk '{print $1,$7,$8,$9,$10,$11}' 74 elif [ $a = all-runtime ];then 75 docker ps -a|awk '{print $1,$7,$8,$9,$10,$11}' 76 else 77 echo "error" 78 fi 79 ;; 80 *start) 81 if [ $a = start ];then 82 docker start $(docker ps -a|grep $NAME |awk '{print $1}') 83 elif [ $a = all-start ];then 84 for t in `docker ps -a|grep Exited |awk '{print $1}'`;do 85 docker start $t 86 done 87 else 88 echo "error" 89 fi 90 ;; 91 logs) 92 docker logs $(docker ps -a|grep $NAME|awk '{print $1}') 93 ;; 94 rm) 95 docker rm $(docker ps -a|grep $NAME|awk '{print $1}') 96 ;; 97 help) 98 echo -e "\timages \t--获取所需的镜像名称和ID" 99 echo -e "\tall-images\t--获取所有的镜像名称和ID" 100 echo -e "\tstatus \t--查看所需容器的运行状态" 101 echo -e "\tall-status\t--查看所有容器的运行状态" 102 echo -e "\trun \t--运行所需的容器" 103 echo -e "\trun-v \t--运行所需的容器,并挂在数据卷目录" 104 echo -e "\trun-p80 \t--运行所需的容器,并映射端口80" 105 echo -e "\tid \t--获取已经运行的所需容器的id" 106 echo -e "\tall-id \t--获取所有已经运行的所需容器的id" 107 echo -e "\trestart \t--重启所需容器" 108 echo -e "\tall-restart\t--重启所有运行的容器" 109 echo -e "\tstop \t--停止正在运行的所需容器" 110 echo -e "\tall-stop\t--停止所有正在运行的容器" 111 echo -e "\tinspect \t--获取所需容器的信息" 112 echo -e "\truntime \t--查看所需容器已经启动的时间" 113 echo -e "\tall-runtime\t--查看所有容器已经启动的时间" 114 echo -e "\tstart \t--启动未运行的所需容器" 115 echo -e "\tallstart\t--启动所有未运行的容器" 116 echo -e "\tlogs \t--查看所需容器的日志信息" 117 echo -e "\thelp \t--查看帮助信息" 118 echo -e "\trm \t--删除容器" 119 ;; 120 *) 121 echo "Please input {image|all-image|status|all-status|run|run-v|run-p80|id|all-id|restart|all-restart|stop|all-stop|inspect|runtime|all-runtime|start|all-start|logs|rm|help|}" 122 ;; 123 esac

 

第二版:

 

  1 #!/bin/bash
  2 #this is input docker continer shell!
  3 #this is 2019-02-13
  4 # v1.2.5
  5 ##########################################################################################
  6 #    功能:
  7 #            1、 将对docker的一些基本操作记录到数据库
  8 #       2、 实现预判断,创建库或表
  9 #       3、 完善查询与变更操作的写入数据库操作    
 10 #       4、 优化了创建容器时可以自定义容器名称,以及手动指定要挂载的宿主机路径
 11 #       5、 可以在容器里执行systemctl命令
 12 #       6、 容器启动则在宿主机/etc/hosts 文件里添加对应的ip地址和主机名称
 13 #########################################################################################
 14 #    前提条件:
 15 #        1、运行此脚本的机器上已经安装了mysql数据库,数据库的一切都是默认不变的;
 16 #        2、运行此脚本的机器上已经安装有docker
 17 #
 18 ########################################################################################
 19 read -p "请输入要执行docker的名称:" NAME        #此出输入要执行docker的名称
 20 z=$1
 21 case $z in
 22     *images)
 23         if [ $z = images ];then
 24             docker images|grep $NAME|awk '{print $1,$3}'
 25         elif [ $z = all-images ];then
 26             docker images
 27         else
 28             echo "error"
 29         fi
 30     ;;
 31     *status)
 32         if [ $z = status ];then
 33             docker ps -a|grep $NAME
 34         elif [ $z = all-status ];then
 35             docker ps -a
 36         else
 37             echo "error"
 38         fi
 39     ;;
 40     run*)
 41         read -p "请输入容器名称:" D_NAME
 42         read -p "请输入要挂载的宿主机路径:" Volume
 43         if [ $z = run ];then
 44             docker run -itd --restart=always -e LANG=en_US.UTF-8 -v /etc/localtime:/etc/localtime --name $D_NAME $(docker images|grep $NAME|awk '{print $1}') /usr/sbin/init
 45             ID=`docker ps -a|grep $D_NAME|awk '{print $1}'`
 46             IP=`docker exec -it $ID hostname -i`
 47             sleep 2
 48             sed -i "\$a$IP\t$ID" /etc/hosts
 49         elif [ $z = run-v ];then
 50             docker run -itd --restart=always -v $Volume:/home/ -e LANG=en_US.UTF-8 -v /etc/localtime:/etc/localtime --name $D_NAME $(docker images|grep $NAME|awk '{print $1}') /usr/sbin/init
 51             ID=$(docker ps -a|grep $D_NAME|awk '{print $1}')
 52             IP=$(docker exec -it $ID hostname -i)
 53             sleep 2
 54             sed -i "\$a$IP\t$ID" /etc/hosts
 55         elif [ $z = run-p80 ];then
 56             docker run -itd --restart=always -v $Volume:/home/ -e LANG=en_US.UTF-8 -v /etc/localtime:/etc/localtime -p 80:80 --name $D_NAME $(docker images|grep $NAMW|awk '{print $1}') /usr/sbin/init
 57             ID=$(docker ps -a|grep $D_NAME|awk '{print $1}')
 58             IP=$(docker exec -it $ID hostname -i)
 59             sleep 2
 60             sed -i "\$a$IP\t$ID" /etc/hosts
 61         else
 62             echo "error"
 63         fi
 64     ;;
 65     *id)
 66         if [ $z = id ];then
 67             docker ps -a|grep $NAME |awk '{print $1}'
 68         elif [ $z = all-id ];then
 69             docker ps -a|awk '{print $1,$NF}'
 70         else
 71             echo "error"
 72         fi
 73     ;;
 74     *restart)
 75         if [ $z = restart ];then
 76             docker restart $(docker ps -a|grep $NAME |awk '{print $1}')
 77         elif [ $z = all-restart ];then
 78             for i in `docker ps -a|awk '{print $1}'`;do
 79                 docker restart $i
 80             done
 81         else
 82             echo "error"
 83         fi
 84     ;;
 85     *stop)
 86         if [ $z = stop ];then
 87             docker stop $(docker ps -a|grep $NAME |awk '{print $1}')
 88         elif [ $z = all-stop ];then
 89             for s in `docker ps|awk 'NR!=1{print $1}'`;do
 90                 docker stop $s
 91             done
 92         else
 93             echo "error"
 94         fi
 95     ;;
 96     inspect)
 97         docker inspect $(docker ps -a|grep $NAME |awk '{print $1}')
 98     ;;
 99     search)
100         docker search $NAME
101     ;;
102     pull)
103         docker pull $NAME
104     ;;
105     *runtime)
106         if [ $z = only-runtime ];then
107             docker ps -a|grep $NAME |awk '{print $1,$7,$8,$9,$10,$11}'
108         elif [ $z = all-runtime ];then
109             docker ps -a|awk '{print $1,$7,$8,$9,$10,$11}'
110         else
111             echo "error"
112         fi
113     ;;
114     *start)
115         if [ $z = start ];then
116             docker start $(docker ps -a|grep $NAME |awk '{print $1}')
117         elif [ $z = all-start ];then
118             for t in `docker ps -a|grep Exited |awk '{print $1}'`;do
119                 docker start $t
120             done
121         else
122             echo "error"
123         fi
124     ;;
125     logs)
126         docker logs $(docker ps -a|grep $NAME|awk '{print $1}')
127     ;;
128     *rm*)
129         if [ $z = rm ];then
130             docker rm $(docker ps -a|grep $NAME|awk '{print $1}')
131         elif [ $z = rmi ];then
132             docker rmi $(docker images|grep $NAME|awk '{print $3}')
133         elif [ $z = all-rm ];then
134             for d in `docker ps -a|awk 'NR!=1{print $1}'`;do
135                 docker rm $d
136             done
137         else
138             echo "error"
139         fi
140     ;;
141     help)
142         echo -e "\timages    \t--获取所需的镜像名称和ID"
143         echo -e "\tall-images\t--获取所有的镜像名称和ID"
144         echo -e "\tstatus    \t--查看所需容器的运行状态"
145         echo -e "\tall-status\t--查看所有容器的运行状态"
146         echo -e "\trun    \t--运行所需的容器"
147         echo -e "\tpull    \t--下载所需镜像"
148         echo -e "\tsearch    \t--查询所需镜像"
149         echo -e "\trun-v    \t--运行所需的容器,并挂在数据卷目录"
150         echo -e "\trun-p80    \t--运行所需的容器,并映射端口80"
151         echo -e "\tid    \t--获取已经运行的所需容器的id"
152         echo -e "\tall-id    \t--获取所有已经运行的所需容器的id"
153         echo -e "\trestart    \t--重启所需容器"
154         echo -e "\tall-restart\t--重启所有运行的容器"
155         echo -e "\tstop    \t--停止正在运行的所需容器"
156         echo -e "\tall-stop\t--停止所有正在运行的容器"
157         echo -e "\tinspect    \t--获取所需容器的信息"
158         echo -e "\tonly-runtime    \t--查看所需容器已经启动的时间"
159         echo -e "\tall-runtime\t--查看所有容器已经启动的时间"
160         echo -e "\tstart    \t--启动未运行的所需容器"
161         echo -e "\tallstart\t--启动所有未运行的容器"
162         echo -e "\tlogs    \t--查看所需容器的日志信息"
163         echo -e "\thelp    \t--查看帮助信息"
164         echo -e "\trm    \t--删除容器"
165         echo -e "\tall-rm    \t--删除all容器"
166         echo -e "\trmi    \t--删除镜像"
167     ;;
168     *)
169         echo "Please input {image|all-image|status|all-status|pull|run|run-v|run-p80|id|all-id|restart|all-restart|stop|all-stop|inspect|runtime|all-runtime|start|all-start|logs|rm|rmi|help|}"
170     ;;
171 esac
172 
173 ####################################
174 #将执行查询操作的步骤记录到数据库
175 ###################################
176 
177 for b in images status id inspect runtime logs search help
178 do
179     mysql -e "show databases"|grep docker  >>/dev/null            #进入docker库,验证数据库是否创建有docker库
180         if [ $? = 0 ];then                    #如果查询有docker库,则执行下面操作
181             if [ $z = $b ];then
182                 mysql -D docker -e "show tables" |grep select>>/dev/null
183                     if [ $? = 0 ];then                    #查询表成功,则执行下面步骤
184                         mysql -e "insert into docker.select(name,date,number,action) values(\"$NAME\",\"$(date +%F-%T)\",1,\"select_$b\")"
185                     else
186                         mysql -e "create table docker.select (name char(64),date char(64),number char(64),action char(64))"            #查询无此表,则进行创建
187                         mysql -e "insert into docker.select(name,date,number,action) values(\"$NAME\",\"$(date +%F-%T)\",1,\"select_$b\")"
188                     fi
189             elif [ $z = all-$b ];then
190                 mysql -D docker -e "show tables" |grep select>>/dev/null
191                     if [ $? = 0 ];then                    #查询表成功,则执行下面步骤
192                         mysql -e "insert into docker.select(name,date,number,action) values(\"$NAME\",\"$(date +%F-%T)\",1,\"select_all-$b\")"
193                     else
194                         mysql -e "create table docker.select (name char(64),date char(64),number char(64),action char(64))"            #查询无此表,则进行创建
195                         mysql -e "insert into docker.select(name,date,number,action) values(\"$NAME\",\"$(date +%F-%T)\",1,\"select_$b\")"
196                     fi
197             fi
198         else
199             mysql -e "create database docker"            #如果没有docker库,则创建docker数据库
200                 if [ $? = 0 ];then                #数据库创建成功,则成功执行下面步骤
201                     if [ $z = $b ];then
202                         mysql -D docker -e "show tables" |grep select>>/dev/null
203                             if [ $? = 0 ];then                    #查询表成功,则执行下面步骤
204                                 mysql -e "insert into docker.select(name,date,number,action) values(\"$NAME\",\"$(date +%F-%T)\",1,\"select_$b\")"
205                             else
206                                 mysql -e "create table docker.select (name char(64),date char(64),number char(64),action char(64))"            #查询无此表,则进行创建
207                                 mysql -e "insert into docker.select(name,date,number,action) values(\"$NAME\",\"$(date +%F-%T)\",1,\"select_$b\")"
208                             fi
209                     elif [ $z = all-$b ];then
210                         mysql -D docker -e "show tables" grep select>>/dev/null
211                             if [ $? = 0 ];then                        #查询表成功,则执行下面步骤
212                                 mysql -e "insert into docker.select(name,date,number,action) values(\"$NAME\",\"$(date +%F-%T)\",1,\"select_all-$b\")"
213                             else
214                                 mysql -e "create table docker.select (name char(64),date char(64),number char(64),action char(64))"            #查询无此表,则进行创建
215                                 mysql -e "insert into docker.select(name,date,number,action) values(\"$NAME\",\"$(date +%F-%T)\",1,\"select_$b\")"
216                             fi
217                     fi
218                 else
219                     echo "error: don't unknow docker database !!!"            #创建docker数据库失败,则打印此提示错误
220                 fi
221         fi
222 done
223 
224 ###################################
225 #将变更的操作步骤记录到数据库
226 ##################################
227 
228 for c in run restart stop start rm pull
229 do
230     mysql -e "show databases" |grep docker >/dev/null
231         if [ $? = 0 ];then    
232             if [ $z = $c ];then
233                 mysql -e "select * from docker.add">/dev/null
234                     if [ $? = 0 ];then
235                         mysql -e "insert into docker.add(name,date,action) values(\"$NAME\",\"$(date +%F-%T)\",\"$z\")"
236                     else
237                         mysql -e "create table docker.add (name char(64),date char(64),action char(64))"        #查询无此表,则进行创建
238                         mysql -e "insert into docker.add(name,date,action) values(\"$NAME\",\"$(date +%F-%T)\",\"$z\")"
239                     fi
240             elif [ $z = all-$c ];then
241                 mysql -e "select * from docker.add">/dev/null
242                     if [ $? = 0 ];then
243                         mysql -e "insert into docker.add(name,date,action) values(\"$NAME\",\"$(date +%F-%T)\",\"all-$z\")"
244                     else
245                         mysql -e "create table docker.add (name char(64),date char(64),action char(64))"        #查询无此表,则进行创建
246                         mysql -e "insert into docker.add(name,date,action) values(\"$NAME\",\"$(date +%F-%T)\",\"all-$z\")"
247                     fi
248             fi
249         else
250             mysql -e "create database docker"
251                 if [ $? = 0 ];then
252                     if [ $z = $c ];then
253                         mysql -e "select * from docker.add">/dev/null
254                             if [ $? = 0 ];then
255                                 mysql -e "insert into docker.add(name,date,action) values(\"$NAME\",\"$(date +%F-%T)\",\"$z\")"
256                             else
257                                 mysql -e "create table docker.add (name char(64), date char(64), action char(64))"
258                                 mysql -e "insert into docker.add(name,date,action) values(\"$NAME\",\"$(date +%F-%T)\",\"$z\")"
259                             fi
260                     elif [ $z = *$c* ];then
261                         mysql -e "select * from docker.add">/dev/null
262                             if [ $? = 0 ];then
263                                 mysql -e "insert into docker.add(name,date,action) values(\"$NAME\",\"$(date +%F-%T)\",\"all-$z\")"
264                             else
265                                 mysql -e "create table docker.add (name char(64), date char(64), action char(64))"
266                                 mysql -e "insert into docker.add(name,date,action) values(\"$NAME\",\"$(date +%F-%T)\",\"all-$z\")"
267                             fi
268                     fi
269                 else
270                     echo "error: don't unknow docker database!!!!"
271                 fi
272         fi
273 done
274 
275 ################################
276 #将已经下载的镜像记录到数据库
277 ###############################
278 
279 if [ $z = pull ];then
280     mysql -e "insert into docker.images(name,date) values(\"$(docker images|grep $NAME|awk '{print $1}')\",\"$(date +%F-%T)\")"
281 fi
282 
283 
284 
285 #######################################################################################################
286 #       说明:
287 #               1、此后版本需添加docker 命令的操作记录;
288 #               2、此后版本添加linux系统对docker服务的一些动作,如:启动、重启、停止等等类似的一些操作
289 #
290 ########################################################################################################

 

posted on 2019-01-30 00:10  辉_少  阅读(1113)  评论(0编辑  收藏  举报