ccccccc / docker-compare.sh
cnmksjs's picture
Upload 49 files
24fd742 verified
#!/bin/bash
# Docker镜像大小比较脚本
echo "🐳 Docker镜像构建和大小比较"
echo "=========================="
echo
# 检查Docker是否可用
if ! command -v docker &> /dev/null; then
echo "❌ Docker未安装"
exit 1
fi
echo "📦 构建不同版本的Docker镜像..."
echo
# 构建标准版本
echo "🔨 构建标准单容器版本..."
docker build -t chatapp:standard -f Dockerfile . --no-cache
echo
# 构建优化版本
echo "🔨 构建优化版本..."
docker build -t chatapp:optimized -f Dockerfile.optimized . --no-cache
echo
# 构建多容器版本
echo "🔨 构建前端容器..."
docker build -t chatapp:frontend ./client --no-cache
echo "🔨 构建后端容器..."
docker build -t chatapp:backend ./server --no-cache
echo
# 显示镜像大小比较
echo "📊 镜像大小比较:"
echo "=================="
echo
# 获取镜像信息
STANDARD_SIZE=$(docker images chatapp:standard --format "{{.Size}}")
OPTIMIZED_SIZE=$(docker images chatapp:optimized --format "{{.Size}}")
FRONTEND_SIZE=$(docker images chatapp:frontend --format "{{.Size}}")
BACKEND_SIZE=$(docker images chatapp:backend --format "{{.Size}}")
echo "📦 单容器版本:"
echo " 标准版本: $STANDARD_SIZE"
echo " 优化版本: $OPTIMIZED_SIZE"
echo
echo "📦 多容器版本:"
echo " 前端容器: $FRONTEND_SIZE"
echo " 后端容器: $BACKEND_SIZE"
echo
# 详细镜像信息
echo "📋 详细镜像信息:"
echo "=================="
docker images | grep chatapp | sort -k2
echo
echo "🔍 镜像层分析:"
echo "=============="
echo
echo "📊 标准版本镜像层:"
docker history chatapp:standard --format "table {{.CreatedBy}}\t{{.Size}}" | head -10
echo
echo "📊 优化版本镜像层:"
docker history chatapp:optimized --format "table {{.CreatedBy}}\t{{.Size}}" | head -10
echo
echo "💡 建议:"
echo "========"
echo "✅ 生产环境推荐使用优化版本 (chatapp:optimized)"
echo "✅ 开发环境可以使用多容器版本便于调试"
echo "✅ 如需最小镜像,考虑使用多阶段构建进一步优化"
echo
echo "🧪 性能测试建议:"
echo "================"
echo "1. 启动时间测试:"
echo " time docker run --rm chatapp:standard"
echo " time docker run --rm chatapp:optimized"
echo
echo "2. 内存使用测试:"
echo " docker stats --no-stream"
echo
echo "3. 网络性能测试:"
echo " 使用 ab 或 wrk 工具测试HTTP性能"
echo
echo "🧹 清理命令:"
echo "============"
echo "# 删除测试镜像"
echo "docker rmi chatapp:standard chatapp:optimized chatapp:frontend chatapp:backend"
echo
echo "# 清理构建缓存"
echo "docker builder prune -f"
# 询问是否清理
read -p "是否现在清理测试镜像? (y/N): " cleanup
if [[ $cleanup =~ ^[Yy]$ ]]; then
echo "🧹 清理镜像..."
docker rmi chatapp:standard chatapp:optimized chatapp:frontend chatapp:backend 2>/dev/null || true
echo "✅ 清理完成"
fi