build-docker.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #!/bin/bash
  2. # ============================================
  3. # 智能Docker镜像构建脚本
  4. # 根据网络环境自动选择最优镜像源
  5. # ============================================
  6. set -e
  7. # 配置
  8. SERVICE=${1:-gateway}
  9. TAG=${2:-latest}
  10. REGISTRY=${3:-aliyun} # aliyun, netease, tsinghua, ustc, distroless
  11. # 颜色输出
  12. RED='\033[0;31m'
  13. GREEN='\033[0;32m'
  14. YELLOW='\033[1;33m'
  15. NC='\033[0m'
  16. # 镜像源映射
  17. declare -A REGISTRIES=(
  18. [aliyun]="registry.aliyuncs.com/library"
  19. [netease]="hub.c.163.com/library"
  20. [tsinghua]="docker.mirrors.tsinghua.edu.cn/library"
  21. [ustc]="docker.mirrors.ustc.edu.cn/library"
  22. )
  23. # 服务映射
  24. declare -A SERVICES=(
  25. [gateway]="gateway"
  26. [order]="order-service"
  27. [payment]="payment-service"
  28. )
  29. # ============================================
  30. # 函数定义
  31. # ============================================
  32. print_usage() {
  33. cat <<EOF
  34. 使用方法: $0 [SERVICE] [TAG] [REGISTRY]
  35. 参数:
  36. SERVICE - 服务名称 (gateway|order|payment) [默认: gateway]
  37. TAG - 镜像标签 [默认: latest]
  38. REGISTRY - 镜像源 (aliyun|netease|tsinghua|ustc) [默认: aliyun]
  39. 示例:
  40. # 构建gateway镜像,使用阿里云源
  41. $0 gateway latest aliyun
  42. # 构建order-service镜像,使用网易云源
  43. $0 order 1.0.0 netease
  44. # 构建payment-service镜像,使用清华源
  45. $0 payment v1.0.0-001 tsinghua
  46. # 智能选择镜像源(自动检测最快源)
  47. $0 gateway latest auto
  48. EOF
  49. }
  50. check_registry() {
  51. local registry=$1
  52. local test_url="https://${registry}/library/maven"
  53. echo -e "${YELLOW}检查镜像源: ${registry}${NC}"
  54. if timeout 5 curl -I --silent "$test_url" > /dev/null 2>&1; then
  55. echo -e "${GREEN}✓ $registry 可用${NC}"
  56. return 0
  57. else
  58. echo -e "${RED}✗ $registry 不可用${NC}"
  59. return 1
  60. fi
  61. }
  62. select_best_registry() {
  63. echo -e "${YELLOW}开始检测可用镜像源...${NC}"
  64. local registries=("aliyun" "netease" "tsinghua" "ustc")
  65. for reg in "${registries[@]}"; do
  66. if check_registry "${REGISTRIES[$reg]}"; then
  67. echo -e "${GREEN}选择镜像源: $reg${NC}"
  68. echo "$reg"
  69. return 0
  70. fi
  71. done
  72. echo -e "${RED}所有镜像源都不可用,使用默认的阿里云${NC}"
  73. echo "aliyun"
  74. }
  75. build_image() {
  76. local service=$1
  77. local tag=$2
  78. local registry=$3
  79. local registry_url=${REGISTRIES[$registry]}
  80. local service_dir="shop-recycle-${service}"
  81. if [ ! -d "$service_dir" ]; then
  82. echo -e "${RED}错误: 目录 $service_dir 不存在${NC}"
  83. exit 1
  84. fi
  85. echo -e "${YELLOW}开始构建镜像:${NC}"
  86. echo " 服务: $service"
  87. echo " 标签: $tag"
  88. echo " 镜像源: $registry ($registry_url)"
  89. echo ""
  90. docker build \
  91. --build-arg REGISTRY="$registry_url" \
  92. --build-arg BUILD_NUMBER="$tag" \
  93. --cache-from "shop-recycle/$service:latest" \
  94. -t "shop-recycle/$service:$tag" \
  95. -t "shop-recycle/$service:latest" \
  96. -f "$service_dir/Dockerfile" \
  97. .
  98. echo -e "${GREEN}✓ 镜像构建完成${NC}"
  99. # 显示镜像信息
  100. echo ""
  101. echo -e "${YELLOW}镜像信息:${NC}"
  102. docker images | grep "shop-recycle/$service" | head -2
  103. }
  104. # ============================================
  105. # 主程序
  106. # ============================================
  107. if [ "$SERVICE" == "--help" ] || [ "$SERVICE" == "-h" ]; then
  108. print_usage
  109. exit 0
  110. fi
  111. # 验证SERVICE参数
  112. if [ ! -v SERVICES[$SERVICE] ]; then
  113. echo -e "${RED}错误: 未知的服务 '$SERVICE'${NC}"
  114. print_usage
  115. exit 1
  116. fi
  117. # 处理镜像源参数
  118. if [ "$REGISTRY" == "auto" ]; then
  119. REGISTRY=$(select_best_registry)
  120. elif [ ! -v REGISTRIES[$REGISTRY] ]; then
  121. echo -e "${RED}错误: 未知的镜像源 '$REGISTRY'${NC}"
  122. exit 1
  123. fi
  124. # 执行构建
  125. build_image "$SERVICE" "$TAG" "$REGISTRY"
  126. echo ""
  127. echo -e "${GREEN}====================================${NC}"
  128. echo -e "${GREEN}构建成功!${NC}"
  129. echo -e "${GREEN}镜像: shop-recycle/$SERVICE:$TAG${NC}"
  130. echo -e "${GREEN}====================================${NC}"
  131. echo ""
  132. echo -e "${YELLOW}下一步:${NC}"
  133. echo " # 推送镜像"
  134. echo " docker push shop-recycle/$SERVICE:$TAG"
  135. echo ""
  136. echo " # 运行容器"
  137. echo " docker run -d -p 8080:8080 shop-recycle/$SERVICE:$TAG"
  138. echo ""
  139. echo " # 查看日志"
  140. echo " docker logs -f \$(docker ps -q -f ancestor=shop-recycle/$SERVICE:$TAG)"