task_receipts/build-docker.sh

124 lines
3.0 KiB
Bash
Executable File

#!/bin/bash
# Build script for Task Receipts Docker images
# This script builds both the client and server Docker images
set -e # Exit on any error
# Configuration
PROJECT_NAME="task-receipts"
CLIENT_IMAGE_NAME="${PROJECT_NAME}-client"
SERVER_IMAGE_NAME="${PROJECT_NAME}-server"
VERSION=${1:-latest} # Use first argument as version, default to 'latest'
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to print colored output
print_status() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Function to check if Docker is running
check_docker() {
if ! docker info > /dev/null 2>&1; then
print_error "Docker is not running or not accessible"
exit 1
fi
}
# Function to build client image
build_client() {
print_status "Building client image..."
# Build the image from root context with client Dockerfile
docker build -t "${CLIENT_IMAGE_NAME}:${VERSION}" -f client/Dockerfile .
if [ $? -eq 0 ]; then
print_success "Client image built successfully: ${CLIENT_IMAGE_NAME}:${VERSION}"
else
print_error "Failed to build client image"
exit 1
fi
}
# Function to build server image
build_server() {
print_status "Building server image..."
# Build the image from root context with server Dockerfile
docker build -t "${SERVER_IMAGE_NAME}:${VERSION}" -f server/Dockerfile .
if [ $? -eq 0 ]; then
print_success "Server image built successfully: ${SERVER_IMAGE_NAME}:${VERSION}"
else
print_error "Failed to build server image"
exit 1
fi
}
# Function to show usage
show_usage() {
echo "Usage: $0 [VERSION]"
echo ""
echo "Arguments:"
echo " VERSION Version tag for the Docker images (default: latest)"
echo ""
echo "Examples:"
echo " $0 # Build with 'latest' tag"
echo " $0 v1.0.0 # Build with 'v1.0.0' tag"
echo " $0 dev # Build with 'dev' tag"
}
# Main execution
main() {
print_status "Starting Docker build process for Task Receipts"
print_status "Version: ${VERSION}"
print_status "Client image: ${CLIENT_IMAGE_NAME}:${VERSION}"
print_status "Server image: ${SERVER_IMAGE_NAME}:${VERSION}"
echo ""
# Check if Docker is available
check_docker
# Build client image
build_client
# Build server image
build_server
echo ""
print_success "All images built successfully!"
echo ""
print_status "Built images:"
docker images | grep -E "(${CLIENT_IMAGE_NAME}|${SERVER_IMAGE_NAME})" || true
echo ""
print_status "To run the containers:"
echo " docker run -p 80:80 ${CLIENT_IMAGE_NAME}:${VERSION}"
echo " docker run -p 4000:4000 ${SERVER_IMAGE_NAME}:${VERSION}"
}
# Handle help argument
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
show_usage
exit 0
fi
# Run main function
main