linuxtool/Config/Docker/image.sh

33 lines
885 B
Bash
Raw Permalink Normal View History

2024-09-26 10:53:51 +08:00
#!/bin/bash
echo "1. 查看当前运行中的镜像"
echo "2. 停止镜像"
echo "3. 删除未使用的镜像"
2024-09-26 10:53:51 +08:00
declare user_choice
read -p "请输入选项:" user_choice
2024-09-26 10:53:51 +08:00
case $user_choice in
2024-09-26 10:53:51 +08:00
'1')
docker ps --format "{{.Names}}"
;;
'2')
declare -a running_images=($(docker ps --format "{{.Names}}"))
declare image_count=0
for image in "${running_images[@]}" ; do
image_count=$(( image_count+1 ))
echo "${image_count}.${image}"
2024-09-26 10:53:51 +08:00
done
echo "要停止的镜像序号,多个镜像用空格隔开"
read -p "请输入:" user_choice
for i in $user_choice ; do
if [[ $i =~ [1-$image_count] ]]; then
echo "正在停止 ${running_images[$(( i -1 ))]}"
docker stop "${running_images[$(( i -1 ))]}"
echo "${running_images[$(( i -1 ))]} 已经停止"
2024-09-26 10:53:51 +08:00
fi
done
;;
'3')
docker system prune -af
echo "清理完成"
esac