add docker for local and production

This commit is contained in:
2025-06-10 22:29:30 +07:00
parent 84fb7ffb52
commit f92655e3e2
1449 changed files with 3115 additions and 0 deletions

206
docker-start.sh Executable file
View File

@@ -0,0 +1,206 @@
#!/bin/bash
# Script untuk menjalankan CKB Laravel Application dengan Docker
# Usage: ./docker-start.sh [dev|prod] [up|down|build|logs]
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"
ACTION="up"
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
dev|development)
ENVIRONMENT="dev"
shift
;;
prod|production|staging)
ENVIRONMENT="prod"
shift
;;
up|start)
ACTION="up"
shift
;;
down|stop)
ACTION="down"
shift
;;
build)
ACTION="build"
shift
;;
logs)
ACTION="logs"
shift
;;
restart)
ACTION="restart"
shift
;;
*)
echo "Unknown option $1"
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 setup environment file
setup_env() {
if [[ ! -f .env ]]; then
if [[ -f docker/env.example ]]; then
print_status "Copying environment file..."
cp docker/env.example .env
print_warning "Please edit .env file to configure your application"
else
print_error "No environment template found. Please create .env file manually."
exit 1
fi
fi
}
# Function to generate application key
generate_key() {
if ! grep -q "APP_KEY=base64:" .env; then
print_status "Generating Laravel application key..."
if [[ $ENVIRONMENT == "dev" ]]; then
docker-compose exec app php artisan key:generate || true
else
docker-compose -f docker-compose.prod.yml exec app php artisan key:generate || true
fi
fi
}
# Function to run migrations
run_migrations() {
print_status "Running database migrations..."
if [[ $ENVIRONMENT == "dev" ]]; then
docker-compose exec app php artisan migrate --force
else
docker-compose -f docker-compose.prod.yml exec app php artisan migrate --force
fi
}
# Function to optimize for production
optimize_production() {
if [[ $ENVIRONMENT == "prod" ]]; then
print_status "Optimizing for production..."
docker-compose -f docker-compose.prod.yml exec app php artisan config:cache
docker-compose -f docker-compose.prod.yml exec app php artisan route:cache
docker-compose -f docker-compose.prod.yml exec app php artisan view:cache
fi
}
# Main execution
print_status "Starting CKB Laravel Application with Docker"
print_status "Environment: $ENVIRONMENT"
print_status "Action: $ACTION"
# Check prerequisites
check_docker
case $ACTION in
up|start)
setup_env
if [[ $ENVIRONMENT == "dev" ]]; then
print_status "Starting development environment..."
docker-compose up -d --build
print_success "Development environment started!"
echo ""
print_status "Access your application at:"
echo " - Web App: http://localhost:8000"
echo " - phpMyAdmin: http://localhost:8080"
echo " - MailHog: http://localhost:8025"
else
print_status "Starting production environment..."
docker-compose -f docker-compose.prod.yml up -d --build
print_success "Production environment started!"
echo ""
print_status "Application is running on port 80"
fi
# Wait for containers to be ready
sleep 10
generate_key
run_migrations
optimize_production
;;
down|stop)
print_status "Stopping containers..."
if [[ $ENVIRONMENT == "dev" ]]; then
docker-compose down
else
docker-compose -f docker-compose.prod.yml down
fi
print_success "Containers stopped!"
;;
build)
print_status "Building containers..."
if [[ $ENVIRONMENT == "dev" ]]; then
docker-compose build --no-cache
else
docker-compose -f docker-compose.prod.yml build --no-cache
fi
print_success "Build completed!"
;;
logs)
print_status "Showing logs..."
if [[ $ENVIRONMENT == "dev" ]]; then
docker-compose logs -f
else
docker-compose -f docker-compose.prod.yml logs -f
fi
;;
restart)
print_status "Restarting containers..."
if [[ $ENVIRONMENT == "dev" ]]; then
docker-compose restart
else
docker-compose -f docker-compose.prod.yml restart
fi
print_success "Containers restarted!"
;;
esac
echo ""
print_success "Operation completed successfully!"