#!/bin/bash set -e echo "🏔️ Building Multi-Architecture Alpine Docker Images" echo "====================================================" echo "" # Color codes GREEN='\033[0;32m' BLUE='\033[0;34m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # Check if buildx is available if ! docker buildx version &> /dev/null; then echo -e "${YELLOW}⚠️ docker buildx not found. Installing...${NC}" docker buildx create --use fi # Ensure buildx builder is running echo -e "${BLUE}🔧 Setting up buildx builder...${NC}" if ! docker buildx inspect default-builder &> /dev/null; then docker buildx create --name default-builder --use else docker buildx use default-builder fi echo -e "${GREEN}✅ Buildx ready${NC}" echo "" # Build function for multi-arch build_multiarch_image() { local dockerfile=$1 local tag=$2 local description=$3 echo -e "${BLUE}📦 Building Multi-Arch: ${NC}${description}" echo -e "${YELLOW} Dockerfile: ${NC}${dockerfile}" echo -e "${YELLOW} Tag: ${NC}${tag}" echo -e "${YELLOW} Platforms: ${NC}linux/amd64, linux/arm64" if docker buildx build \ --platform linux/amd64,linux/arm64 \ -f "${dockerfile}" \ -t "${tag}" \ --load \ .; then echo -e "${GREEN}✅ Success: ${NC}${tag} (amd64 + arm64)" echo "" else echo -e "\033[0;31m❌ Failed: ${NC}${tag}" echo "" echo -e "${YELLOW}💡 Note: Multi-arch builds with --load only work for single platform.${NC}" echo -e "${YELLOW} To test locally, build for your native platform:${NC}" echo -e "${YELLOW} docker buildx build --platform linux/amd64 -f ${dockerfile} -t ${tag} --load .${NC}" exit 1 fi } # Build function for single platform (for local testing) build_native_image() { local dockerfile=$1 local tag=$2 local description=$3 local platform=$(uname -m) # Convert platform name if [ "$platform" = "x86_64" ]; then platform="linux/amd64" elif [ "$platform" = "aarch64" ] || [ "$platform" = "arm64" ]; then platform="linux/arm64" fi echo -e "${BLUE}📦 Building Native: ${NC}${description}" echo -e "${YELLOW} Dockerfile: ${NC}${dockerfile}" echo -e "${YELLOW} Tag: ${NC}${tag}" echo -e "${YELLOW} Platform: ${NC}${platform} (native)" if docker buildx build \ --platform "${platform}" \ -f "${dockerfile}" \ -t "${tag}" \ --load \ .; then echo -e "${GREEN}✅ Success: ${NC}${tag} (${platform})" echo "" else echo -e "\033[0;31m❌ Failed: ${NC}${tag}" exit 1 fi } echo -e "${BLUE}════════════════════════════════════════${NC}" echo -e "${BLUE}Building Alpine Images${NC}" echo -e "${BLUE}════════════════════════════════════════${NC}" echo "" echo -e "${YELLOW}💡 Building for native platform only (for local testing)${NC}" echo -e "${YELLOW} To build multi-arch for push to registry, use:${NC}" echo -e "${YELLOW} docker buildx build --platform linux/amd64,linux/arm64 --push ...${NC}" echo "" # Build images for native platform (can be loaded for local testing) build_native_image "Dockerfile_alpine_node" \ "ht-docker-node:alpine-node" \ "Alpine with Node.js LTS + NVM + pnpm" build_native_image "Dockerfile_alpine_deno" \ "ht-docker-node:alpine-deno" \ "Alpine with Node.js LTS + NVM + Deno" build_native_image "Dockerfile_alpine_bun" \ "ht-docker-node:alpine-bun" \ "Alpine with Node.js LTS + NVM + Bun" # Summary echo -e "${GREEN}════════════════════════════════════════${NC}" echo -e "${GREEN}✨ All Alpine Images Built Successfully!${NC}" echo -e "${GREEN}════════════════════════════════════════${NC}" echo "" echo "📋 Built Images (Native Platform):" echo "" docker images | grep "ht-docker-node:alpine" | awk '{printf " ✅ %-30s %10s\n", $1":"$2, $7" "$8}' echo "" echo -e "${YELLOW}💡 These images are built for your native architecture for local testing.${NC}" echo -e "${YELLOW} In CI/CD, build with: docker buildx build --platform linux/amd64,linux/arm64 --push${NC}" echo ""