#!/bin/bash # Helper script for common Docker operations set -e # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # Function to print colored output print_info() { echo -e "${GREEN}[INFO]${NC} $1" } print_warn() { echo -e "${YELLOW}[WARN]${NC} $1" } print_error() { echo -e "${RED}[ERROR]${NC} $1" } # Check if .env file exists if [ ! -f .env ]; then print_warn ".env file not found. Creating from template..." cat > .env << EOF SECRET_KEY=$(python -c "from django.core.management.utils import get_random_secret_key; print(get_random_secret_key())") DEBUG=False ALLOWED_HOSTS=localhost,127.0.0.1,YOUR_SERVER_IP POSTGRES_DB=Zoneco_ORG POSTGRES_USER=postgres POSTGRES_PASSWORD=change-me POSTGRES_HOST=db POSTGRES_PORT=5432 DJANGO_PORT=8000 ADMIN_USERNAME= ADMIN_PASSWORD= CORS_ALLOWED_ORIGINS=http://localhost:5173,http://localhost:3000,http://YOUR_SERVER_IP:9123 EOF print_info ".env file created with generated SECRET_KEY" fi # Parse command case "$1" in up) print_info "Starting Docker containers..." docker-compose up -d print_info "Containers started. Use 'docker-compose logs -f' to view logs." ;; down) print_info "Stopping Docker containers..." docker-compose down ;; restart) print_info "Restarting Docker containers..." docker-compose restart ;; logs) print_info "Showing logs (Ctrl+C to exit)..." docker-compose logs -f ;; shell) print_info "Opening Django shell..." docker-compose exec web python manage.py shell ;; migrate) print_info "Running migrations..." docker-compose exec web python manage.py migrate ;; makemigrations) print_info "Creating migrations..." docker-compose exec web python manage.py makemigrations ;; createsuperuser) print_info "Creating superuser..." docker-compose exec web python manage.py createsuperuser ;; collectstatic) print_info "Collecting static files..." docker-compose exec web python manage.py collectstatic --noinput ;; rebuild) print_info "Rebuilding containers..." docker-compose build --no-cache print_info "Containers rebuilt. Use './docker-commands.sh up' to start." ;; clean) print_warn "This will remove all containers, volumes, and images. Are you sure? (y/N)" read -r response if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]; then print_info "Cleaning up..." docker-compose down -v docker system prune -f print_info "Cleanup complete." else print_info "Cleanup cancelled." fi ;; status) print_info "Container status:" docker-compose ps ;; *) echo "Usage: $0 {up|down|restart|logs|shell|migrate|makemigrations|createsuperuser|collectstatic|rebuild|clean|status}" echo "" echo "Commands:" echo " up - Start containers in detached mode" echo " down - Stop containers" echo " restart - Restart containers" echo " logs - Show logs (follow mode)" echo " shell - Open Django shell" echo " migrate - Run database migrations" echo " makemigrations - Create new migrations" echo " createsuperuser - Create Django superuser" echo " collectstatic - Collect static files" echo " rebuild - Rebuild containers (no cache)" echo " clean - Remove all containers and volumes" echo " status - Show container status" exit 1 ;; esac