docker命令脚本

Posted hui-shao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 "	images    	--获取所需的镜像名称和ID"
 99         echo -e "	all-images	--获取所有的镜像名称和ID"
100         echo -e "	status    	--查看所需容器的运行状态"
101         echo -e "	all-status	--查看所有容器的运行状态"
102         echo -e "	run    	--运行所需的容器"
103         echo -e "	run-v    	--运行所需的容器,并挂在数据卷目录"
104         echo -e "	run-p80    	--运行所需的容器,并映射端口80"
105         echo -e "	id    	--获取已经运行的所需容器的id"
106         echo -e "	all-id    	--获取所有已经运行的所需容器的id"
107         echo -e "	restart    	--重启所需容器"
108         echo -e "	all-restart	--重启所有运行的容器"
109         echo -e "	stop    	--停止正在运行的所需容器"
110         echo -e "	all-stop	--停止所有正在运行的容器"
111         echo -e "	inspect    	--获取所需容器的信息"
112         echo -e "	runtime    	--查看所需容器已经启动的时间"
113         echo -e "	all-runtime	--查看所有容器已经启动的时间"
114         echo -e "	start    	--启动未运行的所需容器"
115         echo -e "	allstart	--启动所有未运行的容器"
116         echo -e "	logs    	--查看所需容器的日志信息"
117         echo -e "	help    	--查看帮助信息"
118         echo -e "	rm    	--删除容器"
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

 

以上是关于docker命令脚本的主要内容,如果未能解决你的问题,请参考以下文章

从命令行在脚本中运行的 Docker 命令

在脚本中运行命令到 docker 容器

docker命令脚本

常用python日期日志获取内容循环的代码片段

如何避免Docker容器启动脚本运行后自动退出

docker常用脚本执行命令(批量删除所有镜像容器,)