#!/bin/bash # 实用型 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 未安装" 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 "----|----------------------|----------|-------------------" local count=0 while IFS= read -r 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}" elif [[ "$status" == "Exited"* ]]; then status_display="${RED}$chinese_status${NC}" else status_display="${YELLOW}$chinese_status${NC}" fi # 截断镜像名称 if [ ${#image} -gt 25 ]; then image="${image:0:22}..." fi printf "%-4s | %-20s | %-10s | %s\n" "$count" "$name" "$status_display" "$image" fi done < <(docker ps -a --format "table {{.Names}}\t{{.Status}}\t{{.Image}}" | tail -n +2) echo return $count } # 通过编号获取容器名称 get_container_name() { local number=$1 local count=0 while IFS= read -r line; do if [ -n "$line" ]; then count=$((count + 1)) if [ $count -eq $number ]; then echo $(echo "$line" | awk '{print $1}') return 0 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 } # 主菜单 main_menu() { while true; do clear echo -e "${BLUE}" echo "========================================" echo " Docker 容器管理工具" echo "========================================" echo -e "${NC}" # 显示容器列表 show_containers local container_count=$? echo -e "${BLUE}操作选项:${NC}" echo "1. 重启容器" echo "2. 停止容器" echo "3. 启动容器" echo "4. 删除容器" echo "5. 强制删除容器" echo "6. 查看容器日志" echo "7. 查看容器详情" echo "8. 清理已停止容器" echo "9. 查看系统资源" echo "0. 退出" echo read -p "请选择操作 [0-9]: " choice case $choice in 1|2|3|4|5|6|7) if [ "$container_count" -eq 0 ]; then error "没有可用的容器" read -p "按回车键继续..." continue fi 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 "容器编号不存在" 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" ;; esac ;; 8) cleanup_containers ;; 9) show_resources ;; 0) echo log "再见!" exit 0 ;; *) error "无效选择" ;; esac if [ "$choice" -ne 6 ]; then echo read -p "按回车键继续..." fi done } # 脚本入口 check_docker main_menu