一键启动关闭删除docker容器或者images

时间:2022-05-30
本文章向大家介绍一键启动关闭删除docker容器或者images,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

脚本有待优化,由于时间问题,暂时先搁置优化,后期会慢慢再次优化到可以以每个容器或者images来操作!

#! /bin/bash

##start,stop,delete the docker containers
##written by zhdya_20170914

list=`docker ps -a |awk '{print $2}'| grep -v 'ID'`
echo "============================================================================"
echo -e "pls check the follow list of container: n$list"
read -p "pls choose an action which you want!<1.start 2.stop 3.rm 4.rmi > " act
echo "============================================================================"
echo -e "stopnstartnrmnrmi" > /tmp/docker.txt 
##judge if input the words or not!
if [ -z $act ]
then
	echo "you type was wrong,pls just input "start"."stop"."rm"."rmi"."
	exit
fi
##judge if input a wrong words!!
if grep -wq $act /tmp/docker.txt
then
	case $act in
	start)
		docker start $(docker ps -a | awk '{ print $1}' | tail -n +2)
		echo "already start all of docker containers,pls checking it.."
	;;
	stop)
		docker stop $(docker ps -a | awk '{ print $1}' | tail -n +2)
		echo "already stop all of docker containers,pls checking it.."
	;;
	rm)
		docker rm $(docker ps -a | awk '{ print $1}' | tail -n +2)
		echo "already remove all of docker containers,pls checking it.."
	;;
	*)
		docker rmi $(docker images | awk '{print $3}' |tail -n +2)
		echo "already remove all of docker images,pls checking it.."
	esac
else
	echo "you type was wrong,pls just input "start"."stop"."rm"."rmi"."
fi