gcp-hockey-results/docker/build.sh
2025-09-29 20:06:07 +08:00

128 lines
3.7 KiB
Bash

#!/bin/bash
# Docker build script for Hockey Results Application
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Configuration
IMAGE_NAME="hockey-results"
IMAGE_TAG="latest"
FULL_IMAGE_NAME="${IMAGE_NAME}:${IMAGE_TAG}"
echo -e "${BLUE}🏒 Building Hockey Results Application Docker Image${NC}"
echo -e "${BLUE}================================================${NC}"
# Function to display usage
usage() {
echo "Usage: $0 [OPTIONS]"
echo ""
echo "Options:"
echo " -t, --tag TAG Set image tag (default: latest)"
echo " -n, --name NAME Set image name (default: hockey-results)"
echo " --no-cache Build without cache"
echo " --push Push image to registry after build"
echo " -h, --help Show this help message"
echo ""
echo "Examples:"
echo " $0 # Build with default settings"
echo " $0 -t v1.0.0 # Build with specific tag"
echo " $0 --no-cache # Build without cache"
echo " $0 -t v1.0.0 --push # Build and push to registry"
}
# Parse command line arguments
PUSH_IMAGE=false
NO_CACHE=""
REGISTRY=""
while [[ $# -gt 0 ]]; do
case $1 in
-t|--tag)
IMAGE_TAG="$2"
FULL_IMAGE_NAME="${IMAGE_NAME}:${IMAGE_TAG}"
shift 2
;;
-n|--name)
IMAGE_NAME="$2"
FULL_IMAGE_NAME="${IMAGE_NAME}:${IMAGE_TAG}"
shift 2
;;
--no-cache)
NO_CACHE="--no-cache"
shift
;;
--push)
PUSH_IMAGE=true
shift
;;
--registry)
REGISTRY="$2"
shift 2
;;
-h|--help)
usage
exit 0
;;
*)
echo -e "${RED}Unknown option: $1${NC}"
usage
exit 1
;;
esac
done
echo -e "${YELLOW}📋 Build Configuration:${NC}"
echo -e " Image Name: ${GREEN}${FULL_IMAGE_NAME}${NC}"
echo -e " No Cache: ${GREEN}${NO_CACHE:-false}${NC}"
echo -e " Push Image: ${GREEN}${PUSH_IMAGE}${NC}"
echo ""
# Check if Docker is running
if ! docker info > /dev/null 2>&1; then
echo -e "${RED}❌ Docker is not running. Please start Docker and try again.${NC}"
exit 1
fi
# Build the image
echo -e "${BLUE}🔨 Building Docker image...${NC}"
if docker build $NO_CACHE -t "$FULL_IMAGE_NAME" .; then
echo -e "${GREEN}✅ Docker image built successfully: ${FULL_IMAGE_NAME}${NC}"
else
echo -e "${RED}❌ Docker build failed${NC}"
exit 1
fi
# Show image information
echo -e "${BLUE}📊 Image Information:${NC}"
docker images "$FULL_IMAGE_NAME"
# Push image if requested
if [ "$PUSH_IMAGE" = true ]; then
echo -e "${BLUE}📤 Pushing image to registry...${NC}"
if [ -n "$REGISTRY" ]; then
FULL_REGISTRY_NAME="${REGISTRY}/${FULL_IMAGE_NAME}"
docker tag "$FULL_IMAGE_NAME" "$FULL_REGISTRY_NAME"
docker push "$FULL_REGISTRY_NAME"
echo -e "${GREEN}✅ Image pushed to registry: ${FULL_REGISTRY_NAME}${NC}"
else
docker push "$FULL_IMAGE_NAME"
echo -e "${GREEN}✅ Image pushed to registry: ${FULL_IMAGE_NAME}${NC}"
fi
fi
echo -e "${GREEN}🎉 Build process completed successfully!${NC}"
# Show next steps
echo -e "${YELLOW}📝 Next Steps:${NC}"
echo -e " To run the container: ${BLUE}docker run -p 5000:5000 ${FULL_IMAGE_NAME}${NC}"
echo -e " To run with docker-compose: ${BLUE}docker-compose up${NC}"
echo -e " To run with PostgreSQL: ${BLUE}docker-compose --profile postgres up${NC}"
echo -e " To run with MariaDB: ${BLUE}docker-compose --profile mariadb up${NC}"