Files
dock/Docker容器管理面板

157 lines
5.2 KiB
Bash

#!/bin/bash
# 简单实用的 Docker 容器管理脚本
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
# 检查 Docker
check_docker() {
if ! command -v docker &> /dev/null; then
echo -e "${RED}Docker 未安装${NC}"
exit 1
fi
}
# 显示容器列表
show_containers() {
echo -e "\n${BLUE}=== 容器列表 ===${NC}"
echo "编号 | 容器名称 | 状态 | 镜像"
echo "----|----------|------|------"
local count=0
docker ps -a --format "table {{.Names}}\t{{.Status}}\t{{.Image}}" | tail -n +2 | while read line; do
if [ -n "$line" ]; then
count=$((count + 1))
name=$(echo "$line" | awk '{print $1}')
status=$(echo "$line" | awk '{print $2}')
image=$(echo "$line" | awk '{for(i=3;i<=NF;i++) printf $i" "; print ""}' | sed 's/ $//')
# 状态显示
if [[ "$status" == "Up"* ]]; then
status_display="${GREEN}运行中${NC}"
elif [[ "$status" == "Exited"* ]]; then
status_display="${RED}已停止${NC}"
else
status_display="${YELLOW}$status${NC}"
fi
echo -e "$count | $name | $status_display | $image"
fi
done
echo
}
# 通过编号获取容器名称
get_container_name() {
local number=$1
local count=0
docker ps -a --format "table {{.Names}}" | tail -n +2 | while read name; do
if [ -n "$name" ]; then
count=$((count + 1))
if [ $count -eq $number ]; then
echo "$name"
fi
fi
done
}
# 主菜单
main_menu() {
while true; do
clear
echo -e "${BLUE}"
echo "========================================"
echo " 简单 Docker 容器管理"
echo "========================================"
echo -e "${NC}"
show_containers
echo -e "${BLUE}操作选项:${NC}"
echo "1. 启动容器"
echo "2. 停止容器"
echo "3. 重启容器"
echo "4. 删除容器"
echo "5. 批量删除已停止容器"
echo "6. 查看容器日志"
echo "0. 退出"
echo
read -p "请选择操作: " choice
case $choice in
1|2|3|4|6)
echo
read -p "请输入容器编号: " container_number
container_name=$(get_container_name $container_number)
if [ -z "$container_name" ]; then
echo -e "${RED}容器编号不存在${NC}"
read -p "按回车键继续..."
continue
fi
case $choice in
1)
echo -e "${YELLOW}启动容器: $container_name${NC}"
docker start "$container_name" && echo -e "${GREEN}启动成功${NC}" || echo -e "${RED}启动失败${NC}"
;;
2)
echo -e "${YELLOW}停止容器: $container_name${NC}"
docker stop "$container_name" && echo -e "${GREEN}停止成功${NC}" || echo -e "${RED}停止失败${NC}"
;;
3)
echo -e "${YELLOW}重启容器: $container_name${NC}"
docker restart "$container_name" && echo -e "${GREEN}重启成功${NC}" || echo -e "${RED}重启失败${NC}"
;;
4)
echo -e "${RED}删除容器: $container_name${NC}"
read -p "确认删除?(y/N): " confirm
if [[ "$confirm" == "y" || "$confirm" == "Y" ]]; then
docker rm -f "$container_name" && echo -e "${GREEN}删除成功${NC}" || echo -e "${RED}删除失败${NC}"
else
echo -e "${GREEN}取消删除${NC}"
fi
;;
6)
echo -e "${BLUE}查看容器日志: $container_name${NC}"
echo "按 Ctrl+C 退出"
docker logs -f "$container_name"
continue # 日志查看后直接继续,不等回车
;;
esac
;;
5)
echo -e "${YELLOW}批量删除已停止的容器...${NC}"
stopped_count=$(docker ps -aq -f status=exited | wc -l)
if [ $stopped_count -gt 0 ]; then
docker rm $(docker ps -aq -f status=exited)
echo -e "${GREEN}已删除 $stopped_count 个已停止容器${NC}"
else
echo -e "${GREEN}没有已停止的容器${NC}"
fi
;;
0)
echo
echo -e "${GREEN}再见!${NC}"
exit 0
;;
*)
echo -e "${RED}无效选择${NC}"
;;
esac
echo
read -p "按回车键继续..."
done
}
# 启动脚本
check_docker
main_menu