#!/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 } # 批量删除容器 batch_delete_containers() { local numbers="$1" local confirm="$2" # 处理输入,支持空格和逗号分隔 numbers=$(echo "$numbers" | sed 's/,/ /g') local success_count=0 local fail_count=0 for number in $numbers; do if [[ "$number" =~ ^[0-9]+$ ]]; then container_name=$(get_container_name $number) if [ -n "$container_name" ]; then if [ "$confirm" != "y" ]; then echo -e "${YELLOW}删除容器: $container_name${NC}" read -p "确认删除?(Y/n): " confirm_delete confirm_delete=${confirm_delete:-Y} # 默认Y if [[ "$confirm_delete" != "y" && "$confirm_delete" != "Y" ]]; then echo -e "${GREEN}跳过: $container_name${NC}" continue fi fi if docker rm -f "$container_name" &> /dev/null; then echo -e "${GREEN}✓ 删除成功: $container_name${NC}" success_count=$((success_count + 1)) else echo -e "${RED}✗ 删除失败: $container_name${NC}" fail_count=$((fail_count + 1)) fi else echo -e "${RED}✗ 容器编号不存在: $number${NC}" fail_count=$((fail_count + 1)) fi else echo -e "${RED}✗ 无效编号: $number${NC}" fail_count=$((fail_count + 1)) fi done echo if [ $success_count -gt 0 ]; then echo -e "${GREEN}成功删除 $success_count 个容器${NC}" fi if [ $fail_count -gt 0 ]; then echo -e "${RED}删除失败 $fail_count 个${NC}" fi } # 主菜单 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. 删除容器 (支持批量: 1 2 3 或 1,2,3)" echo "5. 批量删除所有已停止容器" echo "6. 查看容器日志" echo "0. 退出" echo read -p "请选择操作: " choice case $choice in 1|2|3|6) echo read -p "请输入容器编号: " container_number if ! [[ "$container_number" =~ ^[0-9]+$ ]]; then echo -e "${RED}无效的编号${NC}" read -p "按回车键继续..." continue fi 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}" ;; 6) echo -e "${BLUE}查看容器日志: $container_name${NC}" echo "按 Ctrl+C 退出" docker logs -f "$container_name" continue # 日志查看后直接继续,不等回车 ;; esac ;; 4) echo echo -e "${YELLOW}删除容器 (支持批量删除)${NC}" echo "输入单个编号: 1" echo "输入多个编号: 1 2 3 或 1,2,3" echo "输入 all 删除所有容器" read -p "请输入容器编号: " input if [[ "$input" == "all" ]]; then echo -e "${RED}警告:将删除所有容器!${NC}" read -p "确认删除所有容器?(Y/n): " confirm_all confirm_all=${confirm_all:-Y} # 默认Y if [[ "$confirm_all" == "y" || "$confirm_all" == "Y" ]]; then total_count=$(docker ps -aq | wc -l) docker rm -f $(docker ps -aq) &> /dev/null echo -e "${GREEN}已删除所有 $total_count 个容器${NC}" else echo -e "${GREEN}取消删除${NC}" fi else batch_delete_containers "$input" "n" # 第二个参数"n"表示需要单独确认 fi ;; 5) echo -e "${YELLOW}批量删除所有已停止的容器...${NC}" stopped_count=$(docker ps -aq -f status=exited | wc -l) if [ $stopped_count -gt 0 ]; then read -p "确认删除 $stopped_count 个已停止容器?(Y/n): " confirm_stopped confirm_stopped=${confirm_stopped:-Y} # 默认Y if [[ "$confirm_stopped" == "y" || "$confirm_stopped" == "Y" ]]; then docker rm $(docker ps -aq -f status=exited) &> /dev/null echo -e "${GREEN}已删除 $stopped_count 个已停止容器${NC}" else echo -e "${GREEN}取消删除${NC}" fi 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