Update Docker容器管理面板

This commit is contained in:
2025-10-20 21:50:52 +08:00
committed by GitHub
parent 654ceb1e43
commit 77fed34de4

View File

@@ -1,222 +1,63 @@
#!/bin/bash
# 实用 Docker 容器管理脚本
# 主要功能:容器检测、通过序列号管理容器
# 简单实用 Docker 容器管理脚本
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m'
# 日志函数
log() {
echo -e "${GREEN}[✓]${NC} $1"
}
error() {
echo -e "${RED}[✗]${NC} $1"
}
warning() {
echo -e "${YELLOW}[!]${NC} $1"
}
# 检查 Docker
check_docker() {
if ! command -v docker &> /dev/null; then
error "Docker 未安装"
echo -e "${RED}Docker 未安装${NC}"
exit 1
fi
}
# 获取容器状态中文显示
get_chinese_status() {
local status="$1"
case "$status" in
"Up"*)
echo "运行中"
;;
"Exited"*)
echo "已停止"
;;
"Created")
echo "已创建"
;;
"Restarting")
echo "重启中"
;;
"Paused")
echo "已暂停"
;;
"Dead")
echo "已死亡"
;;
*)
echo "$status"
;;
esac
}
# 显示容器列表和编号
# 显示容器列表
show_containers() {
echo -e "\n${CYAN}=== 容器列表 ===${NC}"
printf "%-4s | %-20s | %-10s | %s\n" "编号" "容器名称" "状态" "镜像"
echo "----|----------------------|----------|-------------------"
echo -e "\n${BLUE}=== 容器列表 ===${NC}"
echo "编号 | 容器名称 | 状态 | 镜像"
echo "----|----------|------|------"
local count=0
while IFS= read -r line; do
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/ $//')
# 获取中文状态
chinese_status=$(get_chinese_status "$status")
# 状态颜色
# 状态显示
if [[ "$status" == "Up"* ]]; then
status_display="${GREEN}$chinese_status${NC}"
status_display="${GREEN}运行中${NC}"
elif [[ "$status" == "Exited"* ]]; then
status_display="${RED}$chinese_status${NC}"
status_display="${RED}已停止${NC}"
else
status_display="${YELLOW}$chinese_status${NC}"
status_display="${YELLOW}$status${NC}"
fi
# 截断镜像名称
if [ ${#image} -gt 25 ]; then
image="${image:0:22}..."
fi
printf "%-4s | %-20s | %-10s | %s\n" "$count" "$name" "$status_display" "$image"
echo -e "$count | $name | $status_display | $image"
fi
done < <(docker ps -a --format "table {{.Names}}\t{{.Status}}\t{{.Image}}" | tail -n +2)
done
echo
return $count
}
# 通过编号获取容器名称
get_container_name() {
local number=$1
local count=0
while IFS= read -r line; do
if [ -n "$line" ]; then
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 $(echo "$line" | awk '{print $1}')
return 0
echo "$name"
fi
fi
done < <(docker ps -a --format "table {{.Names}}" | tail -n +2)
return 1
}
# 重启容器
restart_container() {
local container_name=$1
echo -e "\n${YELLOW}重启容器: $container_name${NC}"
if docker restart "$container_name" &> /dev/null; then
log "容器重启成功: $container_name"
else
error "容器重启失败: $container_name"
fi
}
# 停止容器
stop_container() {
local container_name=$1
echo -e "\n${YELLOW}停止容器: $container_name${NC}"
if docker stop "$container_name" &> /dev/null; then
log "容器停止成功: $container_name"
else
error "容器停止失败: $container_name"
fi
}
# 启动容器
start_container() {
local container_name=$1
echo -e "\n${YELLOW}启动容器: $container_name${NC}"
if docker start "$container_name" &> /dev/null; then
log "容器启动成功: $container_name"
else
error "容器启动失败: $container_name"
fi
}
# 删除容器
remove_container() {
local container_name=$1
echo -e "\n${RED}删除容器: $container_name${NC}"
# 检查容器是否在运行
if docker ps | grep -q "$container_name"; then
warning "容器正在运行,先停止再删除"
docker stop "$container_name" &> /dev/null
fi
if docker rm "$container_name" &> /dev/null; then
log "容器删除成功: $container_name"
else
error "容器删除失败: $container_name"
fi
}
# 强制删除容器
force_remove_container() {
local container_name=$1
echo -e "\n${RED}强制删除容器: $container_name${NC}"
if docker rm -f "$container_name" &> /dev/null; then
log "容器强制删除成功: $container_name"
else
error "容器强制删除失败: $container_name"
fi
}
# 查看容器日志
view_logs() {
local container_name=$1
echo -e "\n${BLUE}查看容器日志: $container_name${NC}"
echo "按 Ctrl+C 退出日志查看"
docker logs -f --tail=50 "$container_name"
}
# 查看容器详细信息
inspect_container() {
local container_name=$1
echo -e "\n${CYAN}容器详细信息: $container_name${NC}"
docker inspect "$container_name" | grep -E "(Name|Status|Running|IPAddress|Ports|Image)" | head -10
}
# 清理无用容器
cleanup_containers() {
echo -e "\n${YELLOW}清理已停止的容器...${NC}"
local stopped_count=$(docker ps -aq -f status=exited | wc -l)
if [ "$stopped_count" -gt 0 ]; then
docker container prune -f
log "已清理 $stopped_count 个已停止的容器"
else
log "没有已停止的容器需要清理"
fi
}
# 显示系统资源
show_resources() {
echo -e "\n${CYAN}=== 系统资源使用 ===${NC}"
docker system df
done
}
# 主菜单
@@ -225,85 +66,91 @@ main_menu() {
clear
echo -e "${BLUE}"
echo "========================================"
echo " Docker 容器管理工具"
echo " 简单 Docker 容器管理"
echo "========================================"
echo -e "${NC}"
# 显示容器列表
show_containers
local container_count=$?
echo -e "${BLUE}操作选项:${NC}"
echo "1. 启容器"
echo "1. 启容器"
echo "2. 停止容器"
echo "3. 启容器"
echo "3. 启容器"
echo "4. 删除容器"
echo "5. 强制删除容器"
echo "5. 批量删除已停止容器"
echo "6. 查看容器日志"
echo "7. 查看容器详情"
echo "8. 清理已停止容器"
echo "9. 查看系统资源"
echo "0. 退出"
echo
read -p "请选择操作 [0-9]: " choice
read -p "请选择操作: " choice
case $choice in
1|2|3|4|5|6|7)
if [ "$container_count" -eq 0 ]; then
error "没有可用的容器"
read -p "按回车键继续..."
continue
fi
1|2|3|4|6)
echo
read -p "请输入容器编号: " container_number
if ! [[ "$container_number" =~ ^[0-9]+$ ]]; then
error "无效的编号"
read -p "按回车键继续..."
continue
fi
container_name=$(get_container_name $container_number)
if [ -z "$container_name" ]; then
error "容器编号不存在"
echo -e "${RED}容器编号不存在${NC}"
read -p "按回车键继续..."
continue
fi
case $choice in
1) restart_container "$container_name" ;;
2) stop_container "$container_name" ;;
3) start_container "$container_name" ;;
4) remove_container "$container_name" ;;
5) force_remove_container "$container_name" ;;
6) view_logs "$container_name" ;;
7) inspect_container "$container_name" ;;
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
;;
8)
cleanup_containers
;;
9)
show_resources
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
log "再见!"
echo -e "${GREEN}再见!${NC}"
exit 0
;;
*)
error "无效选择"
echo -e "${RED}无效选择${NC}"
;;
esac
if [ "$choice" -ne 6 ]; then
echo
read -p "按回车键继续..."
fi
echo
read -p "按回车键继续..."
done
}
# 脚本入口
# 启动脚本
check_docker
main_menu