| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- #!/bin/bash
- # ============================================
- # 智能Docker镜像构建脚本
- # 根据网络环境自动选择最优镜像源
- # ============================================
- set -e
- # 配置
- SERVICE=${1:-gateway}
- TAG=${2:-latest}
- REGISTRY=${3:-aliyun} # aliyun, netease, tsinghua, ustc, distroless
- # 颜色输出
- RED='\033[0;31m'
- GREEN='\033[0;32m'
- YELLOW='\033[1;33m'
- NC='\033[0m'
- # 镜像源映射
- declare -A REGISTRIES=(
- [aliyun]="registry.aliyuncs.com/library"
- [netease]="hub.c.163.com/library"
- [tsinghua]="docker.mirrors.tsinghua.edu.cn/library"
- [ustc]="docker.mirrors.ustc.edu.cn/library"
- )
- # 服务映射
- declare -A SERVICES=(
- [gateway]="gateway"
- [order]="order-service"
- [payment]="payment-service"
- [web]="web"
- )
- # ============================================
- # 函数定义
- # ============================================
- print_usage() {
- cat <<EOF
- 使用方法: $0 [SERVICE] [TAG] [REGISTRY]
- 参数:
- SERVICE - 服务名称 (gateway|order|payment|web) [默认: gateway]
- TAG - 镜像标签 [默认: latest]
- REGISTRY - 镜像源 (aliyun|netease|tsinghua|ustc) [默认: aliyun]
- 示例:
- # 构建gateway镜像,使用阿里云源
- $0 gateway latest aliyun
- # 构建order-service镜像,使用网易云源
- $0 order 1.0.0 netease
- # 构建payment-service镜像,使用清华源
- $0 payment v1.0.0-001 tsinghua
- # 构建web前端镜像
- $0 web latest aliyun
- # 智能选择镜像源(自动检测最快源)
- $0 gateway latest auto
- EOF
- }
- check_registry() {
- local registry=$1
- local test_url="https://${registry}/library/maven"
-
- echo -e "${YELLOW}检查镜像源: ${registry}${NC}"
-
- if timeout 5 curl -I --silent "$test_url" > /dev/null 2>&1; then
- echo -e "${GREEN}✓ $registry 可用${NC}"
- return 0
- else
- echo -e "${RED}✗ $registry 不可用${NC}"
- return 1
- fi
- }
- select_best_registry() {
- echo -e "${YELLOW}开始检测可用镜像源...${NC}"
-
- local registries=("aliyun" "netease" "tsinghua" "ustc")
-
- for reg in "${registries[@]}"; do
- if check_registry "${REGISTRIES[$reg]}"; then
- echo -e "${GREEN}选择镜像源: $reg${NC}"
- echo "$reg"
- return 0
- fi
- done
-
- echo -e "${RED}所有镜像源都不可用,使用默认的阿里云${NC}"
- echo "aliyun"
- }
- build_image() {
- local service=$1
- local tag=$2
- local registry=$3
-
- local registry_url=${REGISTRIES[$registry]}
- local service_dir="shop-recycle-${service}"
-
- if [ ! -d "$service_dir" ]; then
- echo -e "${RED}错误: 目录 $service_dir 不存在${NC}"
- exit 1
- fi
-
- echo -e "${YELLOW}开始构建镜像:${NC}"
- echo " 服务: $service"
- echo " 标签: $tag"
- echo " 镜像源: $registry ($registry_url)"
- echo ""
-
- docker build \
- --build-arg REGISTRY="$registry_url" \
- --build-arg BUILD_NUMBER="$tag" \
- --cache-from "shop-recycle/$service:latest" \
- -t "shop-recycle/$service:$tag" \
- -t "shop-recycle/$service:latest" \
- -f "$service_dir/Dockerfile" \
- .
-
- echo -e "${GREEN}✓ 镜像构建完成${NC}"
-
- # 显示镜像信息
- echo ""
- echo -e "${YELLOW}镜像信息:${NC}"
- docker images | grep "shop-recycle/$service" | head -2
- }
- # ============================================
- # 主程序
- # ============================================
- if [ "$SERVICE" == "--help" ] || [ "$SERVICE" == "-h" ]; then
- print_usage
- exit 0
- fi
- # 验证SERVICE参数
- if [ ! -v SERVICES[$SERVICE] ]; then
- echo -e "${RED}错误: 未知的服务 '$SERVICE'${NC}"
- print_usage
- exit 1
- fi
- # 处理镜像源参数
- if [ "$REGISTRY" == "auto" ]; then
- REGISTRY=$(select_best_registry)
- elif [ ! -v REGISTRIES[$REGISTRY] ]; then
- echo -e "${RED}错误: 未知的镜像源 '$REGISTRY'${NC}"
- exit 1
- fi
- # 执行构建
- build_image "$SERVICE" "$TAG" "$REGISTRY"
- echo ""
- echo -e "${GREEN}====================================${NC}"
- echo -e "${GREEN}构建成功!${NC}"
- echo -e "${GREEN}镜像: shop-recycle/$SERVICE:$TAG${NC}"
- echo -e "${GREEN}====================================${NC}"
- echo ""
- echo -e "${YELLOW}下一步:${NC}"
- echo " # 推送镜像"
- echo " docker push shop-recycle/$SERVICE:$TAG"
- echo ""
- echo " # 运行容器"
- echo " docker run -d -p 8080:8080 shop-recycle/$SERVICE:$TAG"
- echo ""
- echo " # 查看日志"
- echo " docker logs -f \$(docker ps -q -f ancestor=shop-recycle/$SERVICE:$TAG)"
|