231 lines
6.2 KiB
Bash
Executable File
231 lines
6.2 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# Script untuk rebuild Docker containers dari scratch
|
|
# Usage: ./docker-rebuild.sh [dev|prod]
|
|
|
|
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
|
|
|
|
# Default values
|
|
ENVIRONMENT="dev"
|
|
|
|
# Parse arguments
|
|
while [[ $# -gt 0 ]]; do
|
|
case $1 in
|
|
dev|development)
|
|
ENVIRONMENT="dev"
|
|
shift
|
|
;;
|
|
prod|production|staging)
|
|
ENVIRONMENT="prod"
|
|
shift
|
|
;;
|
|
*)
|
|
echo "Unknown option $1"
|
|
echo "Usage: $0 [dev|prod]"
|
|
exit 1
|
|
;;
|
|
esac
|
|
done
|
|
|
|
# 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. Please start Docker first."
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
# Function to clean up existing containers and images
|
|
cleanup_containers() {
|
|
print_status "Cleaning up existing containers and images..."
|
|
|
|
if [[ $ENVIRONMENT == "dev" ]]; then
|
|
COMPOSE_FILE="docker-compose.yml"
|
|
APP_CONTAINER="ckb-app-dev"
|
|
else
|
|
COMPOSE_FILE="docker-compose.prod.yml"
|
|
APP_CONTAINER="ckb-app-prod"
|
|
fi
|
|
|
|
# Stop and remove containers
|
|
print_status "Stopping containers..."
|
|
docker-compose -f "$COMPOSE_FILE" down || true
|
|
|
|
# Remove specific containers if they exist
|
|
if docker ps -a --format "table {{.Names}}" | grep -q "$APP_CONTAINER"; then
|
|
print_status "Removing existing app container..."
|
|
docker rm -f "$APP_CONTAINER" || true
|
|
fi
|
|
|
|
# Remove images related to this project
|
|
print_status "Removing existing images..."
|
|
docker images | grep "ckb" | awk '{print $3}' | xargs docker rmi -f || true
|
|
|
|
print_success "Cleanup completed!"
|
|
}
|
|
|
|
# Function to prune Docker system
|
|
prune_docker() {
|
|
print_status "Pruning Docker system to free up space..."
|
|
|
|
# Remove unused containers, networks, images
|
|
docker system prune -f
|
|
|
|
# Remove unused volumes (be careful with this)
|
|
print_warning "Removing unused Docker volumes..."
|
|
docker volume prune -f
|
|
|
|
print_success "Docker system pruned!"
|
|
}
|
|
|
|
# Function to build containers
|
|
build_containers() {
|
|
print_status "Building containers for $ENVIRONMENT environment..."
|
|
|
|
if [[ $ENVIRONMENT == "dev" ]]; then
|
|
print_status "Building development container..."
|
|
docker-compose build --no-cache --pull
|
|
print_success "Development container built successfully!"
|
|
else
|
|
print_status "Building production container..."
|
|
docker-compose -f docker-compose.prod.yml build --no-cache --pull
|
|
print_success "Production container built successfully!"
|
|
fi
|
|
}
|
|
|
|
# Function to test build
|
|
test_build() {
|
|
print_status "Testing the build..."
|
|
|
|
if [[ $ENVIRONMENT == "dev" ]]; then
|
|
COMPOSE_FILE="docker-compose.yml"
|
|
else
|
|
COMPOSE_FILE="docker-compose.prod.yml"
|
|
fi
|
|
|
|
# Start containers to test
|
|
print_status "Starting containers for testing..."
|
|
docker-compose -f "$COMPOSE_FILE" up -d
|
|
|
|
# Wait for containers to be ready
|
|
print_status "Waiting for containers to be ready..."
|
|
sleep 15
|
|
|
|
# Test PHP version and extensions
|
|
print_status "Testing PHP and extensions..."
|
|
if docker-compose -f "$COMPOSE_FILE" exec -T app php -v; then
|
|
print_success "PHP is working!"
|
|
else
|
|
print_error "PHP test failed!"
|
|
exit 1
|
|
fi
|
|
|
|
# Test PHP extensions
|
|
print_status "Checking PHP extensions..."
|
|
docker-compose -f "$COMPOSE_FILE" exec -T app php -m | grep -E "(curl|gd|zip|dom|mysql|redis)" || true
|
|
|
|
# Test Redis connection
|
|
print_status "Testing Redis connection..."
|
|
if docker-compose -f "$COMPOSE_FILE" exec -T app php -r "try { \$redis = new Redis(); \$redis->connect('redis', 6379); echo 'OK'; } catch (Exception \$e) { echo 'FAILED'; }" | grep -q "OK"; then
|
|
print_success "Redis connection test passed!"
|
|
else
|
|
print_warning "Redis connection test failed!"
|
|
fi
|
|
|
|
# Test Laravel
|
|
if docker-compose -f "$COMPOSE_FILE" exec -T app php artisan --version; then
|
|
print_success "Laravel is working!"
|
|
else
|
|
print_error "Laravel test failed!"
|
|
exit 1
|
|
fi
|
|
|
|
print_success "Build test completed successfully!"
|
|
}
|
|
|
|
# Function to show next steps
|
|
show_next_steps() {
|
|
echo ""
|
|
print_success "🎉 Rebuild completed successfully!"
|
|
echo ""
|
|
|
|
if [[ $ENVIRONMENT == "dev" ]]; then
|
|
print_status "Development environment is ready!"
|
|
echo ""
|
|
print_status "Next steps:"
|
|
echo " 1. Import your database:"
|
|
echo " ./docker-import-db.sh dev"
|
|
echo ""
|
|
echo " 2. Access your application:"
|
|
echo " - Web App: http://localhost:8000"
|
|
echo " - phpMyAdmin: http://localhost:8080"
|
|
echo ""
|
|
echo " 3. Or use quick setup:"
|
|
echo " ./docker-quick-setup.sh dev"
|
|
else
|
|
print_status "Production environment is ready!"
|
|
echo ""
|
|
print_status "Next steps:"
|
|
echo " 1. Import your database:"
|
|
echo " ./docker-import-db.sh prod"
|
|
echo ""
|
|
echo " 2. Access your application:"
|
|
echo " - Web App: http://localhost"
|
|
fi
|
|
}
|
|
|
|
# Main execution
|
|
echo "================================================"
|
|
print_status "🔄 Docker Clean Rebuild Script"
|
|
print_status "Environment: $ENVIRONMENT"
|
|
echo "================================================"
|
|
echo ""
|
|
|
|
# Ask for confirmation
|
|
print_warning "This will remove all existing containers, images, and volumes!"
|
|
print_warning "Any data not backed up will be lost!"
|
|
read -p "Are you sure you want to continue? (y/N): " -n 1 -r
|
|
echo ""
|
|
|
|
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
|
|
print_status "Rebuild cancelled."
|
|
exit 0
|
|
fi
|
|
|
|
# Check prerequisites
|
|
check_docker
|
|
|
|
# Execute rebuild process
|
|
cleanup_containers
|
|
prune_docker
|
|
build_containers
|
|
test_build
|
|
|
|
# Show final information
|
|
show_next_steps
|
|
|
|
print_success "✅ Clean rebuild completed successfully!" |